代码:<br><br>unit Unit1;<br>interface<br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, Buttons, ExtCtrls, Spin,shellAPI, Menus, ImgList;<br><br>const<br> WM_BARICON=WM_USER+200;<br><br>type<br> TForm1 = class(TForm)<br> SpinEdit1: TSpinEdit;<br> StaticText1: TStaticText;<br> BitBtn1: TBitBtn;<br> BitBtn2: TBitBtn;<br> PopupMenu1: TPopupMenu;<br> N1: TMenuItem;<br> N2: TMenuItem;<br><br> procedure BitBtn1Click(Sender: TObject);<br> procedure BitBtn2Click(Sender: TObject);<br> procedure SpinEdit1Change(Sender: TObject);<br> procedure N2Click(Sender: TObject);<br> procedure N1Click(Sender: TObject);<br> private<br> procedure WMSysCommand(var Message: TMessage); message WM_SYSCOMMAND;<br> procedure WMBarIcon(var Message:TMessage);message WM_BARICON; <br> Private declarations }<br> <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>{ TForm1 }<br> <br><br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>begin<br>end;<br> <br>procedure TForm1.BitBtn2Click(Sender: TObject);<br>begin<br> close;<br>end;<br> <br>procedure TForm1.SpinEdit1Change(Sender: TObject);<br>begin<br> Timer1.Interval:=strtoint(SpinEdit1.Text)*1000;<br>end;<br> <br>procedure TForm1.WMBarIcon(var Message: TMessage);<br>var<br> lpData
NotifyIconData;<br> pos:TPoint;<br>begin<br> case Message.LParam of<br> WM_LBUTTONDBLCLK:<br> begin<br> //如果用户双击击任务栏图标则将图标删除并回复窗口。<br> lpData := new(PNotifyIconDataA);<br> lpData.cbSize := 88;//SizeOf(PNotifyIconDataA);<br> lpData.Wnd := Form1.Handle;<br> lpData.hIcon := application.Icon.Handle;// 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> WM_RBUTTONDOWN: //用户单击右键,则弹出菜单<br> begin<br> getcursorpos(pos);<br> popupmenu1.Popup(pos.x,pos.y);<br> end;<br> end;<br>end;<br> <br>procedure TForm1.WMSysCommand(var Message: TMessage);<br>var<br> lpData
NotifyIconData;<br>begin<br> if Message.WParam =SC_ICON then<br> begin//如果用户最小化窗口则将窗口隐藏并在任务栏上添加图标<br> lpData:= new(PNotifyIconDataA);<br> lpData.cbSize := 88; //SizeOf(PNotifyIconDataA);<br> lpData.Wnd := Form1.Handle;<br> lpData.hIcon :=application.Icon.Handle;// Form1.Icon.Handle;<br> lpData.uCallbackMessage := WM_BARICON;<br> lpData.uID :=0;<br> lpData.szTip := 'Hello';<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 //如果是其它的SystemCommand消息则调用系统缺省处理函数处理之。<br> DefWindowProc(Form1.Handle,Message.Msg,Message.WParam,Message.LParam);<br> end;<br>end;<br> <br>procedure TForm1.N2Click(Sender: TObject);<br>var<br> lpData
NotifyIconData;<br>begin<br> lpData := new(PNotifyIconDataA);<br> lpData.cbSize := 88;//SizeOf(PNotifyIconDataA);<br> lpData.Wnd :=Form1.Handle;<br> lpData.hIcon :=application.Icon.Handle;// 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> <br>end;<br> <br>procedure TForm1.N1Click(Sender: TObject);<br>begin<br> close;<br>end;<br> <br>end.<br> <br>如果在退出时一起删除图标,则:<br> <br>procedure TForm1.N2Click(Sender: TObject);<br>var<br> lpData
NotifyIconData;<br>begin<br> lpData := new(PNotifyIconDataA);<br> lpData.Wnd :=Form1.Handle;<br> lpData.uID :=0;<br> Shell_NotifyIcon(NIM_DELETE,lpData);<br> dispose(lpData);<br> close;<br><br>end;<br> <br>在TNotifyIconData结构中最基本的元素就是Wnd和uID,由这两个元素确立托盘图标在系统托盘中的唯一性(其实这跟操作系统中用全局原子区别它是一样的),uID是自定义的托盘标识,当一个窗口有多个托盘图标时才显得有意义。<br>您说用Shell_NotifyIcon(NIM_DELETE,@lpData);不行,则可能是您设置TNotifyIconData时没做好,要用NIM_DELETE删除图标,正常应该有三个元素是必须设置的:除上面两个外,还有一个就是:cbSize !而且它必须在最前设置,否则无效。这跟new(PNotifyIconDataA);是一样的意思。如:<br><br>procedure TForm1.N1Click(Sender: TObject);<br>var<br> lpData : TNotifyIconData;<br>begin<br> //下面SizeOf()不能是PNotifyIconData,否则变成指针的大小了<br> lpData.cbSize := SizeOf(TNotifyIconData);<br> lpData.Wnd :=Form1.Handle;<br> lpData.uID :=0;<br> Shell_NotifyIcon(NIM_DELETE,@lpData);<br><br> close;<br>end;<br> 其实这样就已经可以完全正常删除一个托盘图标,当然用NIM_ADD添加就不一样了