unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ShellAPI;
// 自已加入 ShellAPI
// 自定义 TrayICON 的消息
Const
WM_BARICON=WM_USER+200;
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
N1: TMenuItem; // 退出
N2: TMenuItem; // 打开/隐藏
procedure N1Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure FormShow(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 }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.N1Click(Sender: TObject);
begin // 退出
Application.Terminate ;
end;
procedure TForm1.N2Click(Sender: TObject);
begin // 打开/隐藏
Form1.Visible := Not(Form1.Visible) ;
end;
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.FormShow(Sender: TObject);
begin
// 开始托盘
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);
// 结束托盘
//---------------------------------------------------------
end;
end.