将最小化图标放入系统托盘的Windows API 在Delphi中的声明(50分)

  • 主题发起人 主题发起人 Eliot
  • 开始时间 开始时间
E

Eliot

Unregistered / Unconfirmed
GUEST, unregistred user!
本人近日看杂志,突然想到此问题,还望各位大虾赐教!
 
function Shell_NotifyIcon; external shell32 name 'Shell_NotifyIconA';<br><br>在大富翁中搜索'托盘图标'或 'NotifyIcon',答案太多了.
 
看看这个控件的源码<br><br>{ TTrayIcon VCL. Version 1.3<br><br>&nbsp; Requires: &nbsp;Delphi 2.0 32 bit.<br><br>&nbsp; Function: Adds an icon to the Windows 95 Tool Tray and<br>&nbsp; &nbsp; &nbsp; has events to respond to mouse clicks.<br><br>&nbsp; This component is based on the TToolIcon VCL that was written by<br>&nbsp; Derek Stutsman (dereks@metronet.com). &nbsp;He based his component on<br>&nbsp; TWinControl, so it showed up as a clear, blank, resizable window at<br>&nbsp; design time and also had more properties than the component actually<br>&nbsp; needed. &nbsp;This made it really hard to find on a busy form sometimes.<br><br>&nbsp; I changed it so it would be based on TComponent so that it was readily<br>&nbsp; visible at design time and also did not cover anything at run-time.<br>&nbsp; The additional Top, left, width, etc. properties are also no longer<br>&nbsp; necessary. &nbsp;I added a ShowDesigning property so that you could test<br>&nbsp; it at design time, but then turn it off so that TWO icons weren't shown<br>&nbsp; on the tool tray when developing and testing.<br><br>&nbsp; One strange anomaly that I worked around but don't know why it happens -<br>&nbsp; if a ToolTip is not specified, then at run-time the icon shows up as<br>&nbsp; blank. &nbsp;If a ToolTip is specified, everything works fine. &nbsp;To fix this,<br>&nbsp; I set up another windows message that set the tool tip if it was blank -<br>&nbsp; this ensures proper operation at all times, but I don't know why this<br>&nbsp; is necessary. &nbsp;If you can figure it out, send me some mail and let me<br>&nbsp; know! (4/17/96 note - still no solution for this!)<br><br>&nbsp; This is freeware (as was the original). &nbsp;If you make cool changes to it,<br>&nbsp; please send them to me.<br><br>&nbsp; Enjoy!<br><br>&nbsp; Pete Ness<br>&nbsp; Compuserve ID: 102347,710<br>&nbsp; Internet: 102347.710@compuserve.com<br>&nbsp; http://ourworld.compuserve.com/homepages/peteness<br><br>&nbsp; Release history:<br><br>&nbsp; 3/8/96 - Version 1.0<br>&nbsp; &nbsp; &nbsp;Release by Derek Stutsman of TToolIcon version 1.0<br><br>&nbsp; 3/12/96 - Version 1.1<br><br>&nbsp; &nbsp; &nbsp;Changed as outlined above by me (Pete Ness) and renamed to TTrayIcon.<br><br>&nbsp; 3/29/96 - Version 1.2<br>&nbsp; &nbsp; &nbsp;Add default window handling to allow closing when Win95 shutdown.<br>&nbsp; &nbsp; &nbsp;Previously, you had to manually close your application before closing<br>&nbsp; &nbsp; &nbsp;Windows 95.<br><br>&nbsp; 4/17/96 - Version 1.3<br>&nbsp; &nbsp; &nbsp;Added a PopupMenu property to automatically handle right clicking on<br>&nbsp; &nbsp; &nbsp;the tray icon.<br>&nbsp; &nbsp; &nbsp;Fixed bug that would not allow you to instantiate a TTrayIcon instance<br>&nbsp; &nbsp; &nbsp;at run-time.<br>&nbsp; &nbsp; &nbsp;Added an example program to show how to do some of the things I've<br>&nbsp; &nbsp; &nbsp;gotten the most questions on.<br>&nbsp; &nbsp; &nbsp;This version is available from my super lame web page - see above for<br>&nbsp; &nbsp; &nbsp;the address.<br><br>&nbsp; }<br><br><br><br>unit TrayIcon;<br><br>interface<br><br>uses<br>&nbsp; SysUtils, Windows, Messages, Classes, Graphics, Controls, ShellAPI, Forms, menus;<br><br>const WM_TOOLTRAYICON = WM_USER+1;<br>&nbsp; &nbsp; &nbsp; WM_RESETTOOLTIP = WM_USER+2;<br><br>type<br><br>&nbsp; TTrayIcon = class(TComponent)<br><br>&nbsp; private<br><br>&nbsp; { Field Variables }<br><br>&nbsp; &nbsp; IconData: TNOTIFYICONDATA;<br>&nbsp; &nbsp; fIcon : TIcon;<br>&nbsp; &nbsp; fToolTip : String;<br>&nbsp; &nbsp; fWindowHandle : HWND;<br>&nbsp; &nbsp; fActive : boolean;<br>&nbsp; &nbsp; fShowDesigning : Boolean;<br><br>&nbsp; { Events }<br><br>&nbsp; &nbsp; fOnClick &nbsp; &nbsp; : TNotifyEvent;<br>&nbsp; &nbsp; fOnDblClick &nbsp;: TNotifyEvent;<br>&nbsp; &nbsp; fOnRightClick : TMouseEvent;<br>&nbsp; &nbsp; fPopupMenu &nbsp; : TPopupMenu;<br><br>&nbsp; &nbsp; function AddIcon : boolean;<br>&nbsp; &nbsp; function ModifyIcon : boolean;<br>&nbsp; &nbsp; function DeleteIcon : boolean;<br><br>&nbsp; &nbsp; procedure SetActive(Value : boolean);<br>&nbsp; &nbsp; procedure SetShowDesigning(Value : boolean);<br>&nbsp; &nbsp; procedure SetIcon(Value : TIcon);<br>&nbsp; &nbsp; procedure SetToolTip(Value : String);<br>&nbsp; &nbsp; procedure WndProc(var msg : TMessage);<br><br>&nbsp; &nbsp; procedure FillDataStructure;<br>&nbsp; &nbsp; procedure DoRightClick( Sender : TObject ); &nbsp; &nbsp;<br><br>&nbsp; protected<br><br>&nbsp; public<br><br>&nbsp; &nbsp; constructor create(aOwner : TComponent); override;<br>&nbsp; &nbsp; destructor destroy; override;<br><br>&nbsp; published<br><br>&nbsp; &nbsp; property Active : boolean read fActive write SetActive;<br>&nbsp; &nbsp; property ShowDesigning : boolean read fShowDesigning write SetShowDesigning;<br>&nbsp; &nbsp; property Icon : TIcon read fIcon write SetIcon;<br>&nbsp; &nbsp; property ToolTip : string read fTooltip write SetToolTip;<br><br>&nbsp; &nbsp; property OnClick &nbsp; &nbsp; : TNotifyEvent read FOnClick write FOnClick;<br>&nbsp; &nbsp; property OnDblClick &nbsp;: TNotifyEvent read FOnDblClick write FOnDblClick;<br>&nbsp; &nbsp; property OnRightClick : TMouseEvent &nbsp;read FOnRightClick write FonRightClick;<br>&nbsp; &nbsp; property PopupMenu : TPopupMenu read fPopupMenu write fPopupMenu;<br><br>&nbsp; end;<br><br>procedure Register;<br><br>implementation<br><br>{$R TrayIcon.res}<br><br>procedure TTrayIcon.SetActive(Value : boolean);<br>begin<br>&nbsp; &nbsp;if value &lt;&gt; fActive then begin<br>&nbsp; &nbsp; &nbsp;fActive := Value;<br>&nbsp; &nbsp; &nbsp;if not (csdesigning in ComponentState) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if Value then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AddIcon;<br>&nbsp; &nbsp; &nbsp; &nbsp; end else begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DeleteIcon;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; end;<br>end;<br><br>procedure TTrayIcon.SetShowDesigning(Value : boolean);<br>begin<br>&nbsp; if csdesigning in ComponentState then begin<br>&nbsp; &nbsp; &nbsp;if value &lt;&gt; fShowDesigning then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; fShowDesigning := Value;<br>&nbsp; &nbsp; &nbsp; &nbsp; if Value then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AddIcon;<br>&nbsp; &nbsp; &nbsp; &nbsp; end else begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DeleteIcon;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; end;<br>end;<br><br>procedure TTrayIcon.SetIcon(Value : Ticon);<br>begin<br>&nbsp; if Value &lt;&gt; fIcon then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; fIcon.Assign(value);<br>&nbsp; &nbsp; &nbsp; ModifyIcon;<br>&nbsp; &nbsp; end;<br>end;<br><br>procedure TTrayIcon.SetToolTip(Value : string);<br>begin<br><br>&nbsp; &nbsp;// This routine ALWAYS re-sets the field value and re-loads the<br>&nbsp; &nbsp;// icon. &nbsp;This is so the ToolTip can be set blank when the component<br>&nbsp; &nbsp;// is first loaded. &nbsp;If this is changed, the icon will be blank on<br>&nbsp; &nbsp;// the tray when no ToolTip is specified.<br><br>&nbsp; &nbsp;if length( Value ) &gt; 62 then<br>&nbsp; &nbsp; &nbsp; Value := copy(Value,1,62);<br>&nbsp; &nbsp;fToolTip := value;<br>&nbsp; &nbsp;ModifyIcon;<br><br>end;<br><br>constructor TTrayIcon.create(aOwner : Tcomponent);<br>begin<br>&nbsp; inherited create(aOwner);<br>&nbsp; FWindowHandle := AllocateHWnd( WndProc );<br>&nbsp; FIcon := TIcon.Create;<br>end;<br><br>destructor TTrayIcon.destroy;<br>begin<br><br>&nbsp; if (not (csDesigning in ComponentState) and fActive)<br>&nbsp; &nbsp; &nbsp;or ((csDesigning in ComponentState) and fShowDesigning) then<br>&nbsp; &nbsp; &nbsp; &nbsp; DeleteIcon;<br><br>&nbsp; FIcon.Free;<br>&nbsp; DeAllocateHWnd( FWindowHandle );<br>&nbsp; inherited destroy;<br><br>end;<br><br>procedure TTrayIcon.FillDataStructure;<br>begin<br><br>&nbsp; with IconData do begin<br><br>&nbsp; &nbsp; &nbsp;cbSize := sizeof(TNOTIFYICONDATA);<br>&nbsp; &nbsp; &nbsp;wnd := FWindowHandle;<br>&nbsp; &nbsp; &nbsp;uID := 0; // is not passed in with message so make it 0<br>&nbsp; &nbsp; &nbsp;uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;<br>&nbsp; &nbsp; &nbsp;hIcon := fIcon.Handle;<br>&nbsp; &nbsp; &nbsp;StrPCopy(szTip,fToolTip);<br>&nbsp; &nbsp; &nbsp;uCallbackMessage := WM_TOOLTRAYICON;<br><br>&nbsp; end;<br><br>end;<br><br>function TTrayIcon.AddIcon : boolean;<br>begin<br>&nbsp; &nbsp;FillDataStructure;<br>&nbsp; &nbsp;result := Shell_NotifyIcon(NIM_ADD,@IconData);<br><br>&nbsp; &nbsp;// For some reason, if there is no tool tip set up, then the icon<br>&nbsp; &nbsp;// doesn't display. &nbsp;This fixes that.<br><br>&nbsp; &nbsp;if fToolTip = '' then<br>&nbsp; &nbsp; &nbsp; PostMessage( fWindowHandle, WM_RESETTOOLTIP,0,0 );<br><br>end;<br><br>function TTrayIcon.ModifyIcon : boolean;<br>begin<br><br>&nbsp; &nbsp;FillDataStructure;<br>&nbsp; &nbsp;if fActive then<br>&nbsp; &nbsp; &nbsp; result := Shell_NotifyIcon(NIM_MODIFY,@IconData)<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; result := True;<br><br>end;<br><br>procedure TTrayIcon.DoRightClick( Sender : TObject );<br>var MouseCo: Tpoint;<br>begin<br><br>&nbsp; &nbsp;GetCursorPos(MouseCo);<br><br>&nbsp; &nbsp;if assigned( fPopupMenu ) then begin<br>&nbsp; &nbsp; &nbsp; SetForegroundWindow( Application.Handle );<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages; <br>&nbsp; &nbsp; &nbsp; fPopupmenu.Popup( Mouseco.X, Mouseco.Y );<br>&nbsp; &nbsp;end;<br><br>&nbsp; &nbsp;if assigned( FOnRightClick ) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FOnRightClick(self,mbRight,[],MouseCo.x,MouseCo.y);<br>&nbsp; &nbsp; &nbsp; end;<br>end;<br><br>function TTrayIcon.DeleteIcon : boolean;<br>begin<br>&nbsp; &nbsp;result := Shell_NotifyIcon(NIM_DELETE,@IconData);<br>end;<br><br>procedure TTrayIcon.WndProc(var msg : TMessage);<br>begin<br>&nbsp; &nbsp;with msg do<br>&nbsp; &nbsp; &nbsp;if (msg = WM_RESETTOOLTIP) then<br>&nbsp; &nbsp; &nbsp; &nbsp; SetToolTip( fToolTip )<br>&nbsp; &nbsp; &nbsp;else if (msg = WM_TOOLTRAYICON) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; case lParam of<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WM_LBUTTONDBLCLK &nbsp; : if assigned (FOnDblClick) then FOnDblClick(self);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WM_LBUTTONUP &nbsp; &nbsp; &nbsp; : if assigned(FOnClick)then FOnClick(self);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WM_RBUTTONUP &nbsp; &nbsp; &nbsp; : DoRightClick(self);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else // Handle all messages with the default handler<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := DefWindowProc(FWindowHandle, Msg, wParam, lParam);<br><br>end;<br><br>procedure Register;<br>begin<br>&nbsp; RegisterComponents('Win95', [TTrayIcon]);<br>end;<br><br>end.<br>
 
Thank you all very much!
 
声明在 shellapi<br><br>uses shellapi &nbsp;就可以了。
 
很长时间没来了,前几日试了一下TTrayIcon,可能不符合我的要求,它<br>不能去掉任务栏的图标,不过还是很有意思的。
 
后退
顶部