如何在程序退出时删除TrayIcon?(100分)

  • 主题发起人 主题发起人 lb2000
  • 开始时间 开始时间
L

lb2000

Unregistered / Unconfirmed
GUEST, unregistred user!
我在启动程序时用下面的程序显示TrayIcon。<br>const<br>&nbsp; wm_notifyIcon = wm_user + 100;<br>type<br>&nbsp; private<br>&nbsp; &nbsp; procedure pNotifyIcon(var msg: TMessage); <br>&nbsp; &nbsp; message wm_notifyIcon;<br>&nbsp; end;<br>var<br>&nbsp; nid: TNotifyIconData;<br><br>&nbsp; nid.cbSize &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := sizeof (nid);<br>&nbsp; nid.Wnd &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= Handle;<br>&nbsp; nid.uID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= 1;<br>&nbsp; nid.uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;<br>&nbsp; nid.uCallbackMessage := wm_notifyicon;<br>&nbsp; nid.hIcon &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= Application.Icon.Handle;<br>&nbsp; lstrcpy (nid.szTip,' TrayIcon Examples');<br>&nbsp; Shell_NotifyIcon (nim_Add, @nid);<br>但是退出时无法将TrayIcon删除。<br>&nbsp; nid.cbSize &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := sizeof (nid);<br>&nbsp; nid.Wnd &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= Handle;<br>&nbsp; nid.uID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= 1;<br>&nbsp; nid.uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;<br>&nbsp; nid.uCallbackMessage := wm_notifyicon;<br>&nbsp; nid.hIcon &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= Application.Icon.Handle;<br>&nbsp; lstrcpy (nid.szTip,' TrayIcon Examples');<br>&nbsp; Shell_NotifyIcon (nim_Delete, @nid);<br>请问如何解决?<br>
 
哇,高手,因为我一向是用控件来完成这个事情的所以只能告诉你,请改用<br>更专业的控件。而此问题的解决办法他的代码里一定有。
 
没问题呀.<br>Shell_NotifyIcon (nim_Delete, @nid);<br>前面的全省了看看.<br>
 
大概朝来朝去
 
lb2000,你定义的<br>“var &nbsp;nid: TNotifyIconData;”<br><br>nid是全局变量吗?如果是,在程序结束时执行<br>&nbsp; Shell_NotifyIcon (NIM_Delete, @nid);<br>就可以了。<br><br><br>
 
你确信<br>退出 Shell_NotifyIcon (nim_Delete, @nid)运行了吗?设置断点单步执行看<br>看, &nbsp;aa.uID:=uint(IDI_TRAYICON);为什么你用aa.uID:=1呢??<br><br>这是我的一段程序<br><br>const IDI_TRAYICON=WM_USER+10;<br>&nbsp; &nbsp; &nbsp; TRAY_CALLBACK=WM_USER+40;<br><br>var<br>&nbsp; &nbsp; aa:tnotifyicondata;<br>&nbsp; &nbsp; tubiao:hicon;<br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br>&nbsp; aa.uFlags:=0;<br>&nbsp; aa.uID:=uint(IDI_TRAYICON);<br>&nbsp; aa.cbSize:=sizeof(tnotifyicondata);<br>&nbsp; aa.Wnd:=self.handle;<br>&nbsp; aa.hIcon:=tubiao;<br>&nbsp; aa.uCallbackMessage:=tray_callback;<br>&nbsp; aa.sztip:='我的程序';<br>&nbsp; shell_notifyicon(nim_delete,@aa);<br>end;<br><br>procedure TForm1.activeicon;<br>begin<br>&nbsp; tubiao:=application.Icon.handle;<br>&nbsp; aa.uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br>&nbsp; aa.uID:=uint(IDI_TRAYICON);<br>&nbsp; aa.cbSize:=sizeof(tnotifyicondata);<br>&nbsp; aa.uCallbackMessage:=tray_callback;<br>&nbsp; aa.Wnd:=self.handle;<br>&nbsp; aa.hIcon:=tubiao;<br>&nbsp; aa.sztip:='我的程序';<br>&nbsp; shell_notifyicon(nim_add,@aa);<br>end;<br>
 
liuq 有道理, nid要定义成Form1的成员.<br>另外,我的一个用到TrayIcon的程序,有时退出时,Icon也不消失,<br>拿鼠标在Icon上晃几下,Icon就消失了.
 
Rxlib中就有一个TRAYICON,可以参考一下它的源代码
 
删除时只需要。 &nbsp;<br>nid.cbSize、 nid.Wnd、nid.uID 3个数据<br>其他设为0;<br>另外删除trayicon前,nid.Wnd指示的窗口应该还没被destroy才行
 
多人接受答案了。
 
后退
顶部