// 一个完整的例子
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ShellAPI;
Const
WM_BARICON=WM_USER+200;
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
procedure N2Click(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
R_lpData : PNotifyIconData
// 2004-06-03 用于托盘
procedure WMSysCommand(var Message: TMessage)
message WM_SYSCOMMAND;
procedure WMBarIcon(var Message:TMessage)
message WM_BARICON
{ Private declarations }
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.WMBarIcon(var Message: TMessage);
var
pos : TPoint;
Begin
Case Message.LParam Of
WM_LBUTTONDBLCLK:
begin
//如果用户双击击任务栏图标则将图标删除并回复窗口。
Form1.Visible := True;
end;
WM_RBUTTONDOWN: //用户单击右键,则弹出菜单
begin
GetCursorPos(pos);
PopupMenu1.Popup(pos.x,pos.y);
end;
end
// end case Message.LParam of
end;
procedure TForm1.WMSysCommand(var Message: TMessage);
begin
if Message.WParam = SC_ICON then
begin
Form1.Visible := False;
end
else
if Message.WParam = SC_CLOSE then
begin
Form1.Visible := False;
end
else
begin
DefWindowProc(Form1.Handle,Message.Msg,Message.WParam,Message.LParam);
end;
end;
procedure TForm1.N2Click(Sender: TObject);
begin
Form1.Visible := Not(Form1.Visible);
end;
procedure TForm1.N1Click(Sender: TObject);
begin
Application.Terminate;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE,R_lpData);
Dispose(R_lpData);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
nDevs, TapiVersion : LongInt;
begin
SetWindowLong(Application.Handle, GWL_EXSTYLE,WS_EX_TOOLWINDOW);
//总在最上
SetWindowPos(Form1.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOMOVE);
Self.Icon.Handle := Application.Icon.Handle;
R_lpData := New(PNotifyIconDataA);
R_lpData.cbSize := SizeOf(PNotifyIconDataA)
// 88;
R_lpData.Wnd := Form1.Handle;
R_lpData.hIcon := Form1.Icon.Handle ;
R_lpData.uCallbackMessage := WM_BARICON;
R_lpData.uID := 0;
R_lpData.szTip := 'QSecurity';
R_lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
Shell_NotifyIcon(NIM_ADD,R_lpData);
Form1.Show;
end;
end.