Mega Code Archive

 
Categories / Delphi / Graphic
 

Add an icon to task bar and recieve mouse messages from the icon

Title: Add an icon to task bar and recieve mouse messages from the icon Question: How to Add an icon to task bar and recieve mouse messages from the icon Answer: I have updated the component adding some more events and also fixed some bugs. I also added variables x and y to all events so it is easier to show a popup menu at the icon when it is clicked. You can add an icon to the taskbar by calling the function SHellNotifyIcon; the first parameter can take the following values NIM_ADD:adds an icon to taskbar NIM_MODIFY: modifies the icon on taskbar NIM_DELETE: removes the icon from taskbar; the second parameter is a pointer to the NOTIFYICONDATA structure NOTIFYICONDATA cbSize: size of structure; hWnd: handle of window to recieve messages; uID:id of icon uFlags: can take following values Members NIF_ICON :specifies if The hIcon member is valid. NIF_MESSAGE :specifies if The uCallbackMessage member is valid. NIF_TIP :specifies if The szTip member is valid. uCallbackMessage: Application-defined message identifier. The system uses the specified identifier for notification messages that it sends to the window identified by hWnd whenever a mouse event occurs in the bounding rectangle of the icon. hIcon: Handle of the icon to add, modify, or delete. szTip:Tooltip text to display for the icon. I have created a component that adds an icon to the task bar. Here is the code unit StatusIcon; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,shellapi; type TMouseEvent = procedure(x:integer;y:integer;sender:tobject) of object; type TStatusIcon = class(TWinControl) protected ficondata:TNotifyIconData; ficon:ticon; fid:integer; fhint:string; fadded:boolean; flclick:TMouseEvent; frclick:TMouseEvent; fldblclick:TMouseEvent; frdblclick:TMouseEvent; fmove:TMouseEvent; procedure seticon(aicon:ticon); procedure setid(aid:integer); procedure sethint(ahint:string); procedure SINotifyIcon(var msg:Tmessage);message WM_USER+1; procedure loaded;override; procedure WMPAINT(var msg:Tmessage);message WM_PAINT; public constructor create(Aowner:tcomponent);override; destructor destroy; override; function AddToTaskbar:boolean; function RemoveFromTaskbar:boolean; function Modify:boolean; property added:boolean read fadded; procedure setbounds(left:integer;top:integer;width:integer;height:integer);override; published property Icon:Ticon read ficon write seticon; property ID:integer read fid write setid; property OnLeftClick:TMouseEvent read flclick write flclick; property OnRightClick:TMousEEvent read frclick write frclick; property OnMouseMove:TMouseEvent read fmove write fmove; property OnLeftDoubleClick:TMouseEvent read fldblclick write fldblclick; property OnRightDoubleClick:TMouseEvent read frdblclick write frdblclick; property Hint read fhint write sethint; end; const sibase= WM_user+1; procedure Register; implementation procedure tstatusicon.sethint(ahint:string); var i:integer; begin inherited; fhint:=ahint; for i:=1 to length(hint) do ficondata.szTip[i-1]:=fhint[i]; end; procedure tstatusicon.setid(aid:integer); begin fid:=aid; ficondata.uID:=fid; end; procedure tstatusicon.setbounds(left:integer;top:integer;width:integer;height:integer); begin width:=28; height:=28; inherited; end; procedure tstatusicon.WMPAINT(var msg:Tmessage); var rec:trect; dc:hdc; begin if csdesigning in componentstate then begin dc:=getdc(handle); rec:=clientrect; drawedge(dc,rec,BDR_RAISEDINNER,BF_RECT); if assigned(ficon) then drawiconex(dc,rec.left+3,rec.top+3,ficon.Handle,20,20,0,0,DI_NORMAL or DI_DEFAULTSIZE); releasedc(handle,dc); end; inherited; end; function tstatusicon.modify:boolean; begin result:=false; if fadded=true then result:=Shell_NotifyIcon(NIM_MODIFY,@ficondata); end; procedure tstatusicon.loaded; begin inherited; ficondata.cbSize:=sizeof(ficondata); ficondata.Wnd:=handle; ficondata.hIcon:=ficon.Handle; ficondata.uID:=fid; ficondata.uFlags:=NIF_MESSAGE; ficondata.uCallbackMessage:=sibase; if length(trim(hint))0 then ficondata.uFlags:=ficondata.uFlags or NIF_TIP; if assigned(ficon) then ficondata.uFlags:=ficondata.uFlags or NIF_ICON; if not(csdesigning in componentstate) then visible:=false; end; function Tstatusicon.RemoveFromTaskbar:boolean; var tmp:boolean; begin tmp:=Shell_NotifyIcon(NIM_DELETE,@ficondata); if tmp=true then fadded:=false; result:=tmp; end; procedure Tstatusicon.SINotifyIcon(var msg:Tmessage); begin inherited; if (msg.wparam=fid) then begin if (msg.LParam=WM_LBUTTONDOWN) then if assigned(flclick) then flclick(mouse.CursorPos.x,mouse.Cursorpos.y,self); if (msg.lparam=WM_RBUTTONDOWN) then if assigned(frclick) then frclick(mouse.CursorPos.x,mouse.Cursorpos.y,self); if (msg.lparam=WM_MOUSEMOVE) then if assigned(fmove) then fmove(mouse.CursorPos.x,mouse.Cursorpos.y,self); if (msg.lparam=WM_LBUTTONDBLCLK) then if assigned(fldblclick) then fldblclick(mouse.CursorPos.x,mouse.Cursorpos.y,self); if (msg.lparam=WM_RBUTTONDBLCLK) then if assigned(frdblclick) then frdblclick(mouse.CursorPos.x,mouse.Cursorpos.y,self); end; end; constructor Tstatusicon.create(Aowner:tcomponent); begin inherited; ficon:=ticon.Create; fid:=1; fadded:=false; end; destructor tstatusicon.destroy; begin inherited; if fadded = true then RemoveFromTaskbar; end; procedure Tstatusicon.seticon(aicon:ticon); begin ficon.Assign(aicon); ficondata.hIcon:=ficon.handle; end; function Tstatusicon.addtotaskbar:boolean; begin if fadded=false then fadded:=Shell_NotifyIcon(NIM_ADD,@ficondata); result:=fadded; end; procedure Register; begin RegisterComponents('Samples', [TStatusIcon]); end; end. //copy code to .pas file and then install the component. call the Addtotaskbar function to put the icon on taskbar. call the removefromtaskbar function to remove the icon from taskbar. If after adding the icon you make changes to the hint property, id property or the icon property then call the modify function to see the changes made. you don't need to call the removefromtaskbar function when you close the application. It will automatically be removed from the taskbar.