您的位置:首页 > 编程语言 > Delphi

Delphi中使用托盘图标

2006-04-26 11:31 309 查看
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ADODB, ShellApi;
Const ICON_MESSAGE = WM_USER + 100;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure InitIcon;
procedure OnIconNotify(var Message: TMessage); message ICON_MESSAGE;
procedure WMSysCommand(var Sysss: TWMSysCommand);message WM_SYSCOMMAND;
public
{ Public declarations }
end;

var
Form1: TForm1;
nid: TNotifyIconData;
implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.WMSysCommand(var Sysss: TWMSysCommand);
begin
with Sysss do
begin
if (CmdType and $FFF0 = SC_MINIMIZE) then
begin
if not Shell_NotifyIcon(NIM_ADD,@nid) then
ShowMessage('Failed!');
ShowWindow(Self.Handle,SW_HIDE);
Exit;
end;
inherited;
end;
end;

procedure TForm1.OnIconNotify(var Message: TMessage);
begin
if Message.LParam=WM_RBUTTONDOWN then
begin
ShowWindow(Self.Handle, SW_RESTORE);
Shell_NotifyIcon(NIM_DELETE, @nid);
end;
end;

procedure TForm1.InitIcon;
begin
nid.cbSize := sizeof(nid);
nid.Wnd := Self.Handle;
nid.hIcon := Application.Icon.Handle;
nid.szTip := 'Test';
nid.uCallbackMessage := ICON_MESSAGE;
nid.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE;

SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Self.InitIcon;
end;

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: