unit TrayIcon;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls, Menus,shellapi;<br><br>type<br> //----------------------------------------------------------------------<br> PNotifyIconData = ^TNotifyIconDataA;<br> TNotifyIconDataA = record<br> cbSize : DWORD;<br> Wnd : HWND;<br> uID : UINT;<br> uFlags : UINT;<br> uCallbackMessage : UINT;<br> hIcon : HICON;<br> szTip : array [0..63] of AnsiChar;<br> end;<br> //----------------------------------------------------------------------<br> TForm1 = class(TForm)<br> PopupMenu1: TPopupMenu;<br> open1: TMenuItem;<br> close1: TMenuItem;<br> N1: TMenuItem;<br> about1: TMenuItem;<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> procedure open1Click(Sender: TObject);<br> procedure close1Click(Sender: TObject);<br> procedure FormShow(Sender: TObject);<br> procedure FormCreate(Sender: TObject);<br> private<br> { Private declarations }<br> //-------------------------------------------------------------<br> IconData: TNotifyIconData;<br> procedure ShowIcon;<br> procedure IconOnClick(var message:TMessage); message WM_USER+1;<br> Procedure WMSysCommand(Var message : TMessage) ; Message WM_SYSCOMMAND ;<br> //-------------------------------------------------------------<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.IconOnClick( var message: Tmessage);<br>var p : TPoint;<br>begin<br> if (message.lParam = WM_LBUTTONDOWN) then<br> begin<br> ShowWindow(Handle, SW_SHOW );<br> end;<br> <br> if (message.lParam = WM_RBUTTONDOWN) then<br> begin<br> GetCursorPos(p);<br> SetForegroundWindow (Application.Handle); //<---------------------------------------注意这两句。<br> Application.ProcessMessages;//<------------------------------------------------------<br> popupmenu1.Popup( p.x ,p.y );<br> end;<br>end;<br><br>Procedure TForm1.WMSysCommand(Var Message : TMessage) ;<br>begin<br><br> if (Message.WParam = SC_MINIMIZE) then<br> begin<br> ShowIcon;<br> end<br> else<br> Inherited;<br><br>end;<br><br>procedure TForm1.ShowIcon;<br>begin<br> IconData.cbSize := SizeOf( IconData );<br> IconData.Wnd := Handle;<br> IconData.uID := 1;<br> IconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;<br> IconData.uCallBackMessage := WM_USER+1;<br> IconData.hIcon := application.Icon.Handle;<br> IconData.szTip := 'LANChat';<br> Shell_NotifyIcon( NIM_ADD, @IconData );<br> ShowWindow(Handle, SW_HIDE);<br> hide;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> Shell_NotifyIcon( NIM_DELETE, @IconData );<br>end;<br><br>procedure TForm1.open1Click(Sender: TObject);<br>begin<br> Form1.Show;<br>end;<br><br>procedure TForm1.close1Click(Sender: TObject);<br>begin<br> Form1.close;<br>end;<br><br>procedure TForm1.FormShow(Sender: TObject);<br>begin<br> <br> showwindow(application.handle,sw_hide);<br> <br><br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> showicon;<br>end;<br><br>end.