来自:gooodlife, 时间:2005-9-11 15:21:02, ID:3201010<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, Menus, ShellAPI;<br> // 自已加入 ShellAPI<br><br>// 自定义 TrayICON 的消息 <br>Const<br> WM_BARICON=WM_USER+200;<br><br><br>type<br> TForm1 = class(TForm)<br> PopupMenu1: TPopupMenu;<br> N1: TMenuItem; // 退出<br> N2: TMenuItem; // 打开/隐藏<br> procedure N1Click(Sender: TObject);<br> procedure N2Click(Sender: TObject);<br> procedure FormShow(Sender: TObject);<br> private<br> R_lpData : PNotifyIconData; // 2004-06-03 用于托盘<br> procedure WMSysCommand(var Message: TMessage); message WM_SYSCOMMAND;<br> procedure WMBarIcon(var Message:TMessage); message WM_BARICON; <br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.N1Click(Sender: TObject);<br>begin // 退出<br> Application.Terminate ;<br>end;<br><br>procedure TForm1.N2Click(Sender: TObject);<br>begin // 打开/隐藏<br> Form1.Visible := Not(Form1.Visible) ;<br>end;<br><br>procedure TForm1.WMBarIcon(var Message: TMessage);<br>var<br> pos : TPoint;<br>Begin<br> Case Message.LParam Of<br> WM_LBUTTONDBLCLK:<br> begin<br> //如果用户双击击任务栏图标则将图标删除并回复窗口。<br> Form1.Visible := True;<br> end;<br> WM_RBUTTONDOWN: //用户单击右键,则弹出菜单<br> begin<br> GetCursorPos(pos);<br> PopupMenu1.Popup(pos.x,pos.y);<br> end;<br> end; // end case Message.LParam of<br>end;<br><br>procedure TForm1.WMSysCommand(var Message: TMessage);<br>begin<br> if Message.WParam = SC_ICON then<br> begin<br> Form1.Visible := False;<br> end<br> else<br> if Message.WParam = SC_CLOSE then<br> begin<br> Form1.Visible := False;<br> end<br> else<br> begin<br> DefWindowProc(Form1.Handle, Message.Msg, Message.WParam, Message.LParam);<br> end;<br>end;<br><br>procedure TForm1.FormShow(Sender: TObject);<br>begin<br> // 开始托盘<br><br> Self.Icon.Handle := Application.Icon.Handle;<br><br> R_lpData := New(PNotifyIconDataA);<br> R_lpData.cbSize := SizeOf(PNotifyIconDataA) ; // 88;<br> R_lpData.Wnd := Form1.Handle;<br> R_lpData.hIcon := Form1.Icon.Handle ;<br> R_lpData.uCallbackMessage := WM_BARICON;<br> R_lpData.uID := 0;<br> R_lpData.szTip := 'QSecurity';<br> R_lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;<br><br> Shell_NotifyIcon(NIM_ADD,R_lpData);<br><br> // 结束托盘<br> //---------------------------------------------------------<br>end;<br><br>end.