W
wxc211
Unregistered / Unconfirmed
GUEST, unregistred user!
如题,而且退出时托盘图标不能自动消失。<br><br><br> type<br> TForm1 = class(TForm)<br> PopupMenu1: TPopupMenu;<br> N1: TMenuItem;<br> N2: TMenuItem;<br> PopupMenu2: TPopupMenu;<br> N3: TMenuItem;<br> N21: TMenuItem;<br> procedure FormCreate(Sender: TObject);<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> private<br> //托盘图标<br> FIcon:TIcon;<br> //定义TNotifyIconData类型的变量<br> NID:TNotifyIconData;<br> public<br> procedure TrayIconMessage(var MSG:TMessage);message WM_USER+100;<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> FIcon:=TIcon.Create;<br> FIcon.Handle:=LoadIcon(HInstance,'Icon1');<br> if FIcon.Handle=0 then<br> begin<br> Application.MessageBox('无法打开图标1','找不到图标文件1',MB_OK or MB_IconError);<br> Close;<br> Exit;<br> end;<br> with NID do<br> begin<br> //cbSize为本记录的大小<br> cbSize:=SizeOf(TNotifyIconData);<br> //Wnd为本窗口句柄<br> Wnd:=Handle;<br> //uID是程序员定义的唯一标识符,如一个应用程序有多个托盘图标,则每个图标需要有唯一的uID<br> uID:=1;<br> uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br> uCallbackMessage:=WM_USER+100;<br> hIcon:=FIcon.Handle;<br> szTip:='图标1';<br> end;<br> Shell_NotifyIcon(NIM_ADD,@NID);<br> //在任务栏上隐藏程序<br> SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> Shell_NotifyIcon(NIM_DELETE,@NID);<br> FIcon.Free;<br>end;<br><br>procedure TForm1.TrayIconMessage(var MSG: TMessage);<br>var<br> Point:TPoint;<br>begin<br> if MSG.LParam=WM_LBUTTONDOWN then<br> begin<br> Visible:=not Visible;<br> if MSG.WParam=1 then<br> begin<br> FIcon.Handle:=LoadIcon(HInstance,'Icon1');<br> if FIcon.Handle=0 then<br> begin<br> Application.MessageBox('无法打开图标1','找不到图标文件1',MB_OK or MB_IconError);<br> Close;<br> Exit;<br> end;<br> with NID do<br> begin<br> //cbSize为本记录的大小<br> cbSize:=SizeOf(TNotifyIconData);<br> //Wnd为本窗口句柄<br> Wnd:=Handle;<br> //uID是程序员定义的唯一标识符,如一个应用程序有多个托盘图标,则每个图标需要有唯一的uID<br> uID:=2;<br> uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br> uCallbackMessage:=WM_USER+100;<br> hIcon:=FIcon.Handle;<br> szTip:='图标2';<br> end;<br> end<br> else<br> begin<br> FIcon.Handle:=LoadIcon(HInstance,'Icon2');<br> if FIcon.Handle=0 then<br> begin<br> Application.MessageBox('无法打开图标2','找不到图标文件2',MB_OK or MB_IconError);<br> Close;<br> Exit;<br> end;<br> with NID do<br> begin<br> //cbSize为本记录的大小<br> cbSize:=SizeOf(TNotifyIconData);<br> //Wnd为本窗口句柄<br> Wnd:=Handle;<br> //uID是程序员定义的唯一标识符,如一个应用程序有多个托盘图标,则每个图标需要有唯一的uID<br> uID:=1;<br> uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br> uCallbackMessage:=WM_USER+100;<br> hIcon:=FIcon.Handle;<br> szTip:='图标1';<br> end;<br> end;<br> Shell_NotifyIcon(NIM_MODIFY,@NID);<br> end;<br>end;