unit NotifyIcon;<br><br>interface<br><br>uses<br> Messages, Windows, SysUtils, Classes, shellapi,StrUtils,<br> Forms, Menus, Graphics;<br>const NotifyMsg = WM_USER+1; //托盘自定义消息<br>type<br> TNotifyIcon = class(TComponent) //自TComponent 派生<br> private<br> FData: TnotifyIconData; //系统托盘所需的数据<br> FEnabled: Bool;<br> FOnClick: TNotifyEvent; <br> FOnDblClick: TNotifyEvent;<br> FPopupMenu: TPopupMenu;<br> FIcon: TIcon; //托盘图标<br> FTimer: Bool;<br> FDblClickTime: Cardinal ;<br> procedure SetEnabled(Flag: Bool); <br> procedure SetIcon(h: TICON); <br> procedure SetTip(s: string);<br> function GetTip: string;<br> procedure WndProc(var Msg: TMessage);<br> public<br> constructor Create(AOwner: TComponent); override;<br> destructor Destroy; override;<br> procedure Show; //显标图标<br> procedure Hide; //隐藏图标<br> published<br> property Handle: HWND read FData.Wnd; //托盘的句柄<br> property Enabled : Bool read FEnabled Write SetEnabled default False; //显示或隐藏<br> property OnClick : TNotifyEvent read FOnClick Write FOnClick; //单击左键时响应<br> property OnDblClick : TNotifyEvent read FOnDblClick Write FOnDblClick; //双击左键时响应<br> property PopupMenu : TPopupMenu read FPopupMenu Write FPopupMenu; //单击右键时响应,弹出菜单<br> property Icon : TICON read FIcon Write SetIcon; // 图标<br> property Tip : String read GetTip write settip ; //提示<br> end;<br>procedure Register;<br><br>implementation<br><br>constructor TNotifyIcon.Create(AOwner: TComponent); //构造函数,初始化变量<br>begin<br> inherited Create(AOwner);<br> FTimer := False;<br> FDblClickTime := GetDoubleClickTime; //获取双击时间间隔<br> FEnabled := False; <br> FData.Wnd := Classes.AllocateHWnd(WndProc); // 设置回调函数<br> FData.cbSize := sizeof(TNotifyIconData); <br> FData.uCallbackMessage :=NotifyMsg; <br> FData.szTip := '';<br> FData.uID := UINT(self);<br> FData.uFlags := nif_icon + nif_tip + nif_message;<br> Icon := application.Icon ; //默认初始化图标为主程序图标<br>end;<br><br>destructor TNotifyIcon.Destroy; //析构函数<br>begin<br> if FEnabled then Enabled :=false;<br> Classes.DeallocateHWnd(FData.Wnd);<br> inherited Destroy;<br>end;<br><br>procedure TNotifyIcon.SetIcon(h:TICON) ; //设置图标<br>begin<br> FIcon := h; <br> FData.hIcon := FIcon.Handle ;<br> if Enabled then Shell_NotifyIcon(NIM_MODIFY,@FData);<br>end;<br><br>procedure TNotifyIcon.SetTip(s:string); //设置提示语<br>begin<br> if length(s)>63 then s:=midstr(s,1,63); //最长为64字节,所以超过63字节需要截断<br> strcopy(Fdata.szTip ,pchar(s));<br> if Enabled then Shell_NotifyIcon(NIM_MODIFY,@FData);<br>end;<br><br>function TNotifyIcon.GetTip :string; //获取提示语<br>begin<br> Result := Fdata.sztip;<br>end;<br><br>procedure TNotifyIcon.Show ; //在托盘显示图标<br>begin<br> Enabled :=True;<br>end;<br><br>procedure TNotifyIcon.Hide ; //隐藏图标<br>begin<br> Enabled :=False;<br>end;<br><br>procedure TNotifyIcon.SetEnabled(Flag:Bool) ; //显示或隐藏图标<br>begin<br> if Flag = Fenabled then exit;<br> if Flag then shell_notifyicon(NIM_ADD,@FData)<br> else shell_notifyicon(NIM_Delete,@FData);<br> FEnabled := not FEnabled;<br>end;<br><br>procedure TNotifyIcon.WndProc(var Msg: TMessage) ; //回调函数<br>var p: TPoint;<br>begin<br> if Msg.Msg = NotifyMsg then begin<br> case msg.LParam of<br> WM_LBUTTONUP:begin //左键弹起,如果只定义OnClick 则立即响应<br> if assigned(FOnClick) and not assigned(FOnDblClick) then OnClick(self);<br> end;<br> WM_LBUTTONDOWN:begin //左键落下,如果单击和双击同时定义,则启用定时器,<br> if assigned(FOnClick) and assigned(FOnDblClick) then begin //以判断是单击还是双击<br> SetTimer(Handle,1,FDblClickTime,nil); //如果不是同时定义则不响应<br> FTimer :=True;<br> end;<br> end;<br> WM_LBUTTONDBLCLK :begin //左键双击,<br> if FTimer then begin //如果有定时器,则取消定时器<br> KillTimer(Handle,1);<br> FTimer:=False;<br> end ;<br> if assigned(FOnDblClick) then OnDblClick(self); //如果定义双击则响应<br> end;<br> WM_RBUTTONUP : begin //右键弹起,响应菜单<br> if assigned(FPopupMenu) then begin<br> GetCursorPos(p);<br> FPopupMenu.Popup(p.x,p.Y);<br> end;<br> end;<br> end;<br> msg.Result :=0;<br> end else if msg.Msg = WM_TIMER then begin //定时器结束,响应单击。<br> KillTimer(Handle,1); //在单击、双击同时定义的情况下,没有双击<br> FTimer := False;<br> FOnClick(self);<br> end;<br>end;<br><br>procedure Register;<br>begin<br> RegisterComponents('System', [TNotifyIcon]);<br>end;<br><br>end.<br>你也可以直接下载CoolTrayIcon这类的控件