unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, ShellAPI, Menus, StdCtrls, ExtCtrls;<br><br>const<br> WM_BARICON = WM_USER + 200;<br><br>type<br> TForm1 = class(TForm)<br> PopupMenu1: TPopupMenu;<br> show1: TMenuItem;<br> Timer1: TTimer;<br> Label1: TLabel;<br> exit1: TMenuItem;<br> procedure show1Click(Sender: TObject);<br> procedure FormShow(Sender: TObject);<br> procedure Timer1Timer(Sender: TObject);<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> procedure exit1Click(Sender: TObject);<br> private<br> lpData: PNotifyIconData;<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.WMSysCommand(var Message: TMessage);<br>begin<br> if Message.WParam = SC_ICON then<br> begin<br> //如果用户最小化窗口则将窗口隐藏并在任务栏上添加图标<br> lpData := New(PNotifyIconDataA);<br> lpData.cbSize := 88;<br> lpData.Wnd := Form1.Handle;<br> lpData.hIcon := application.Icon.Handle;<br> lpData.uCallbackMessage := WM_BARICON;<br> lpData.uID :=0;<br> lpData.szTip := 'Samples';<br> lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;<br> Shell_NotifyIcon(NIM_ADD,lpData);<br> dispose(lpData);<br> Form1.Visible := False;<br> end<br> else<br> begin<br> //如果是其它的SystemCommand消息则调用系统缺省处理函数处理之。<br> DefWindowProc(Form1.Handle, Message.Msg, Message.WParam, Message.LParam);<br> end;<br>end;<br><br>procedure TForm1.WMBarIcon(var Message: TMessage);<br>begin<br> if (Message.LParam = WM_RBUTTONDOWN) then<br> begin<br> PopupMenu1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);<br> end;<br>end;<br><br>procedure TForm1.show1Click(Sender: TObject);<br>begin<br> //如果用户点击任务栏图标则将图标删除并回复窗口。<br> lpData := new(PNotifyIconDataA);<br> lpData.cbSize := 88;//SizeOf(PNotifyIconDataA);<br> lpData.Wnd := Form1.Handle;<br> lpData.hIcon := Form1.Icon.Handle;<br> lpData.uCallbackMessage := WM_BARICON;<br> lpData.uID :=0;<br> lpData.szTip := 'Samples';<br> lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;<br> Shell_NotifyIcon(NIM_DELETE,lpData);<br> dispose(lpData);<br> Form1.Visible := True;<br>end;<br><br>procedure TForm1.FormShow(Sender: TObject);<br>begin<br> ShowWindow(application.Handle, sw_hide);<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br> MeMsg: TMessage;<br>begin<br> Timer1.Enabled := False;<br> MeMsg.WParam := SC_ICON;<br> WMSysCommand(MeMsg);<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> action := cafree;<br>end;<br><br>procedure TForm1.exit1Click(Sender: TObject);<br>begin<br> show1Click(sender);<br> application.Terminate;<br>end;<br><br>end.