托盘图标挂载右键菜单 ( 积分: 100 )

  • 主题发起人 主题发起人 wgliang2004
  • 开始时间 开始时间
W

wgliang2004

Unregistered / Unconfirmed
GUEST, unregistred user!
将应用程序缩小为一个托盘图标后,<br>希望能够通过右键点出菜单,请各位大虾指教<br>小弟是位初学者,现暂无相关资料可以查,先谢谢了!~~
 
将应用程序缩小为一个托盘图标后,<br>希望能够通过右键点出菜单,请各位大虾指教<br>小弟是位初学者,现暂无相关资料可以查,先谢谢了!~~
 
TRzTrayIcon<br>raize 控件包 自己 下载
 
unit TrayIcon;<br><br>interface<br><br>uses<br> &nbsp; &nbsp;Menus,windows,Types,Forms,Messages,SysUtils, Classes ,Graphics,ExtCtrls,ShellAPI,Controls;<br><br><br>const WM_SYSTEM_TRAY_NOTIFY = WM_USER + 100;<br>type<br>TTrayIconMessage=(imClick, imDoubleClick, imMouseDown,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imMouseUp, imLeftClickUp, imLeftDoubleClick,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imRightClickUp, imRightDoubleClick, imNone);<br><br> &nbsp;TTrayIcon = class(TComponent)<br> &nbsp;private<br> &nbsp; &nbsp; &nbsp; FVisible:boolean;<br> &nbsp; &nbsp; &nbsp; FHide:boolean;<br> &nbsp; &nbsp; &nbsp; FIconList:TImageList;<br> &nbsp; &nbsp; &nbsp; FData:TNotifyIconData;<br> &nbsp; &nbsp; &nbsp; FIsClicked:boolean;<br> &nbsp; &nbsp; &nbsp; FIcon:Ticon;<br> &nbsp; &nbsp; &nbsp; FTimer:TTimer;<br> &nbsp; &nbsp; &nbsp; FAnimate:boolean;<br> &nbsp; &nbsp; &nbsp; FIconIndex:integer;<br> &nbsp; &nbsp; &nbsp; FPopupMenu:TPopupMenu;<br> &nbsp; &nbsp; &nbsp; FHint:String ;<br><br> &nbsp; &nbsp; &nbsp; FAppRestore:TTrayIconMessage;<br> &nbsp; &nbsp; &nbsp; FPopupMenuShow:TTrayIconMessage;<br> &nbsp; &nbsp; &nbsp; FApplicationHook:TWindowHook;<br><br> &nbsp; &nbsp; &nbsp; FOnClick:TNotifyEvent;<br> &nbsp; &nbsp; &nbsp; FOnMinimize:TNotifyEvent;<br> &nbsp; &nbsp; &nbsp; FOnRestore:TNotifyEvent;<br> &nbsp; &nbsp; &nbsp; FOnMouseMove:TMouseMoveEvent;<br> &nbsp; &nbsp; &nbsp; FOnMouseExit:TMouseMoveEvent;<br> &nbsp; &nbsp; &nbsp; FOnMouseEnter:TMouseMoveEvent;<br> &nbsp; &nbsp; &nbsp; FOnDblClick:TNotifyEvent;<br> &nbsp; &nbsp; &nbsp; FOnMouseDown:TMouseEvent;<br> &nbsp; &nbsp; &nbsp; FOnMouseUp:TMouseEvent;<br> &nbsp; &nbsp; &nbsp; FOnAnimate:TNotifyEvent;<br> &nbsp; &nbsp; &nbsp; FOnCreate:TNotifyEvent;<br> &nbsp; &nbsp; &nbsp; FOnDestroy:TNotifyEvent;<br> &nbsp; &nbsp; &nbsp; FOnActivate:TNotifyEvent;<br> &nbsp; &nbsp; &nbsp; FOnDeactivate:TNotifyEvent;<br><br> &nbsp; &nbsp; &nbsp; procedure SetHint(Hint:String);<br> &nbsp; &nbsp; &nbsp; procedure SetHide(Value:boolean);<br> &nbsp; &nbsp; &nbsp; procedure EndSession;<br> &nbsp; &nbsp; &nbsp; function GetAnimateInterval:integer;<br> &nbsp; &nbsp; &nbsp; procedure SetAnimateInterval(Value:integer);<br> &nbsp; &nbsp; &nbsp; function GetAnimate:boolean;<br> &nbsp; &nbsp; &nbsp; procedure SetAnimate(Value:boolean);<br> &nbsp; &nbsp; &nbsp; function ShiftState:TShiftState;<br><br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;protected<br> &nbsp; &nbsp; &nbsp; procedure DoClick();<br> &nbsp; &nbsp; &nbsp; procedure DoDblClick();<br> &nbsp; &nbsp; &nbsp; procedure DoOnAnimate(Sender:TObject);virtual;<br> &nbsp; &nbsp; &nbsp; procedure DoMouseMove(Shift:TShiftState;X:integer;Y:integer);virtual;<br> &nbsp; &nbsp; &nbsp; procedure DoMouseDown(Button:TMouseButton;Shift:TShiftState;X:integer;Y:integer);<br> &nbsp; &nbsp; &nbsp; procedure DoMouseUp(Button:TMouseButton;Shift:TShiftState;X:integer;Y:integer);<br> &nbsp; &nbsp; &nbsp; procedure SetVisible(Value:boolean);virtual;<br> &nbsp; &nbsp; &nbsp; procedure DoMessage(var Message: TMessage);//message WM_SYSTEM_TRAY_NOTIFY;<br> &nbsp; &nbsp; &nbsp; function ApplicationHookProc(var Message: TMessage):boolean;<br> &nbsp; &nbsp; &nbsp; procedure Notification(AComponent:TComponent;Operation: TOperation);override;<br> &nbsp; &nbsp; &nbsp; procedure Loaded; override;<br><br> &nbsp; &nbsp; &nbsp; property Data:TNotifyIconData read FData;<br> &nbsp; &nbsp;{ Protected declarations }<br> &nbsp;public<br> &nbsp; &nbsp; &nbsp; constructor Create(AOwner: TComponent); override;<br> &nbsp; &nbsp; &nbsp; destructor Destroy; override;<br> &nbsp; &nbsp; &nbsp; procedure Update;virtual;<br> &nbsp; &nbsp; &nbsp; procedure Minimize;virtual;<br> &nbsp; &nbsp; &nbsp; procedure Restore;virtual;<br> &nbsp; &nbsp; &nbsp; procedure ShowMenu;virtual;<br> &nbsp; &nbsp; &nbsp; procedure SetIconIndex(Value:integer);virtual;<br> &nbsp; &nbsp; &nbsp; procedure SetDefaultIcon;virtual;<br> &nbsp; &nbsp; &nbsp; function GetHandle:HWND;<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;published<br> &nbsp; &nbsp; &nbsp;property IconIndex:integer read FIconIndex write SetIconIndex default 0;<br> &nbsp; &nbsp; &nbsp;property Visible:boolean read FVisible write SetVisible default false;<br> &nbsp; &nbsp; &nbsp;property Hide:boolean read FHide write SetHide;<br> &nbsp; &nbsp; &nbsp;property Hint:String read FHint write SetHint;<br> &nbsp; &nbsp; &nbsp;property PopupMenu:TPopupMenu read FPopupMenu write FPopupMenu;<br> &nbsp; &nbsp; &nbsp;property RestoreOn:TTrayIconMessage read FAppRestore write FAppRestore;<br> &nbsp; &nbsp; &nbsp;property PopupMenuOn:TTrayIconMessage read FPopupMenuShow write FPopupMenuShow;<br> &nbsp; &nbsp; &nbsp;property Icons:TImageList read FIconList write FIconList;<br> &nbsp; &nbsp; &nbsp;property AnimateInterval:integer read GetAnimateInterval write SetAnimateInterval default 1000;<br> &nbsp; &nbsp; &nbsp;property Animate:boolean read GetAnimate write SetAnimate default false;<br> &nbsp; &nbsp; &nbsp;property Handle:HWND read GetHandle;<br><br> &nbsp; // Events<br> &nbsp; &nbsp; &nbsp;property OnMinimize:TNotifyEvent read FOnMinimize write FOnMinimize;<br> &nbsp; &nbsp; &nbsp;property OnRestore:TNotifyEvent read FOnRestore write FOnRestore;<br> &nbsp; &nbsp; &nbsp;property OnClick:TNotifyEvent read FOnClick write FOnClick;<br> &nbsp; &nbsp; &nbsp;property OnMouseEnter:TMouseMoveEvent read FOnMouseEnter write FOnMouseEnter;<br> &nbsp; &nbsp; &nbsp;property OnMouseExit:TMouseMoveEvent read FOnMouseExit write FOnMouseExit;<br> &nbsp; &nbsp; &nbsp;property OnMouseMove:TMouseMoveEvent read FOnMouseMove write FOnMouseMove;<br> &nbsp; &nbsp; &nbsp;property OnMouseUp:TMouseEvent read FOnMouseUp write FOnMouseUp;<br> &nbsp; &nbsp; &nbsp;property OnMouseDown:TMouseEvent read FOnMouseDown write FOnMouseDown;<br> &nbsp; &nbsp; &nbsp;property OnAnimate:TNotifyEvent read FOnAnimate write FOnAnimate;<br> &nbsp; &nbsp; &nbsp;property OnCreate:TNotifyEvent read FOnCreate write FOnCreate;<br> &nbsp; &nbsp; &nbsp;property OnDestroy:TNotifyEvent read FOnDestroy write FOnDestroy;<br> &nbsp; &nbsp; &nbsp;property OnActivate:TNotifyEvent read FOnActivate write FOnActivate;<br> &nbsp; &nbsp; &nbsp;property OnDeactivate:TNotifyEvent read FOnDeactivate write FOnDeactivate;<br><br> &nbsp; &nbsp;{ Published declarations }<br> &nbsp;end;<br><br>procedure Register;<br><br>implementation<br><br>procedure Register;<br>begin<br> &nbsp;RegisterComponents('Samples', [TTrayIcon]);<br>end;<br><br>procedure TTrayIcon.DoOnAnimate(Sender:TObject);<br>begin<br> &nbsp; if (IconIndex &lt; FIconList.Count-1) then<br> &nbsp; &nbsp; &nbsp;inc(FIconIndex)<br> &nbsp; else<br> &nbsp; &nbsp; &nbsp;FIconIndex := 0;<br><br> &nbsp; SetIconIndex(FIconIndex);<br><br> &nbsp; Update;<br>end;<br><br>procedure TTrayIcon.SetHide(Value:boolean);<br>begin<br> &nbsp; FHide := Value;<br>end;<br><br><br>procedure TTrayIcon.SetIconIndex(Value:integer);<br>begin<br> &nbsp; FIconIndex := Value;<br><br> &nbsp; if assigned(FIconList) then<br> &nbsp; &nbsp; &nbsp;FIconList.GetIcon(FIconIndex, FIcon);<br><br> &nbsp; Update;<br>end;<br><br><br>constructor TTrayIcon.Create(AOwner: TComponent);<br>begin<br> &nbsp; inherited;<br> &nbsp; FIcon := TIcon.CREATE ;<br> &nbsp; FTimer := TTimer.Create(nil);<br><br> &nbsp; FIconIndex:= 0;<br> &nbsp; FIcon.Assign(Application.Icon);<br> &nbsp; FAppRestore := imDoubleClick;<br> &nbsp; FOnAnimate := DoOnAnimate;<br> &nbsp; FPopupMenuShow := imNone;<br> &nbsp; FVisible := false;<br> &nbsp; FHide := true;<br> &nbsp; FTimer.Enabled := false;<br> &nbsp; FTimer.OnTimer := OnAnimate;<br> &nbsp; FTimer.Interval := 1000;<br><br> &nbsp; if (not (csDesigning in ComponentState)) then<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;FData.cbSize := sizeof(TNotifyIconData);<br> &nbsp; &nbsp; &nbsp;FData.Wnd := AllocateHWnd(DoMessage);<br> &nbsp; &nbsp; &nbsp;FData.uID := integer(self);<br> &nbsp; &nbsp; &nbsp;FData.hIcon := FIcon.Handle;<br> &nbsp; &nbsp; &nbsp;FData.uFlags := NIF_ICON or NIF_MESSAGE;<br> &nbsp; &nbsp; &nbsp;FData.uCallbackMessage := WM_SYSTEM_TRAY_NOTIFY;<br><br> &nbsp; &nbsp; &nbsp;FApplicationHook := ApplicationHookProc;<br> &nbsp; &nbsp; &nbsp;Update;<br> &nbsp; end;<br>end;<br><br><br><br>destructor TTrayIcon.Destroy;<br>begin<br> &nbsp; if (not (csDesigning in ComponentState)) then<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;Shell_NotifyIcon(NIM_DELETE, @FData);<br> &nbsp; &nbsp; &nbsp;DeallocateHWnd(FData.Wnd);<br> &nbsp; end;<br><br> &nbsp; if assigned(FIcon) then<br> &nbsp; &nbsp; &nbsp;FIcon.free;<br><br> &nbsp; if assigned(FTimer) then<br> &nbsp; &nbsp; &nbsp;FTimer.free;<br> &nbsp; inherited;<br>end;<br><br><br>procedure TTrayIcon.Update;<br>begin<br> &nbsp; if (not (csDesigning in ComponentState)) &nbsp;then<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;FData.hIcon := FIcon.Handle;<br><br> &nbsp; &nbsp; &nbsp;if (Visible = true) then<br> &nbsp; &nbsp; &nbsp; &nbsp; Shell_NotifyIcon(NIM_MODIFY, @FData);<br> &nbsp; end;<br>end;<br><br>procedure TTrayIcon.SetVisible(Value:boolean);<br>begin<br> &nbsp; FVisible := Value;<br><br> &nbsp; if (not (csDesigning in ComponentState)) then<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;if (FVisible) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; if (not Shell_NotifyIcon(NIM_ADD, @FData)) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise EOutOfResources.Create('Cannot create!');<br><br> &nbsp; &nbsp; &nbsp; &nbsp; Hide := true;<br> &nbsp; &nbsp; &nbsp; &nbsp; Application.HookMainWindow(FApplicationHook);<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; if (not Shell_NotifyIcon(NIM_DELETE, @FData)) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise EOutOfResources.Create('Cannot move!');;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; Hide := false;<br> &nbsp; &nbsp; &nbsp; &nbsp; Application.UnhookMainWindow(FApplicationHook);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; end;<br>end;<br><br><br>procedure TTrayIcon.EndSession;<br>begin<br> &nbsp; Shell_NotifyIcon(NIM_DELETE, @FData);<br>end;<br><br><br>procedure TTrayIcon.DoMessage(var Message:TMessage);<br>var<br> &nbsp; point:TPoint;<br> &nbsp; shift:TShiftState;<br>begin<br> &nbsp; case Message.Msg of<br> &nbsp; WM_QUERYENDSESSION:<br> &nbsp; &nbsp; &nbsp; &nbsp; Message.Result := 1;<br> &nbsp; WM_ENDSESSION:<br> &nbsp; &nbsp; &nbsp; &nbsp; EndSession;<br> &nbsp; WM_SYSTEM_TRAY_NOTIFY:<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp;case Message.LParam of<br> &nbsp; &nbsp; &nbsp; &nbsp;WM_MOUSEMOVE:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if assigned(FOnClick) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shift := ShiftState;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetCursorPos(point);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DoMouseMove(shift, point.x, point.y);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp;WM_LBUTTONDOWN:<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift := ShiftState;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift:= shift+[ssLeft];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCursorPos(point);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoMouseDown(mbLeft, shift, point.x, point.y);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FIsClicked := true;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;WM_LBUTTONUP:<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift := ShiftState;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift :=shift+[ssLeft];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCursorPos(point);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if assigned(FOnClick) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DoClick;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoMouseUp(mbLeft, shift, point.x, point.y);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (FAppRestore = imLeftClickUp) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Restore;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (FPopupMenuShow = imLeftClickUp) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowMenu;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;WM_LBUTTONDBLCLK:<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoDblClick;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (FAppRestore = imLeftDoubleClick) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Restore;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (FPopupMenuShow = imLeftDoubleClick) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowMenu;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;WM_RBUTTONDOWN:<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift := ShiftState;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift:= shift+[ssRight];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCursorPos(point);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoMouseDown(mbRight, shift, point.x, point.y);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;WM_RBUTTONUP:<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift:= ShiftState;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift:=shift+[ssRight];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCursorPos(point);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoMouseUp(mbRight, shift, point.x, point.y);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (FAppRestore = imRightClickUp) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Restore;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (FPopupMenuShow = imRightClickUp) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowMenu;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;WM_RBUTTONDBLCLK:<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoDblClick;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (FAppRestore = imRightDoubleClick) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Restore;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (FPopupMenuShow = imRightDoubleClick) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowMenu;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;WM_MBUTTONDOWN:<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift := ShiftState;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift:=shift+[ssMiddle];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCursorPos(point);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoMouseDown(mbMiddle, shift, point.x, point.y);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;WM_MBUTTONUP:<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift := ShiftState;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift:=shift+[ssMiddle];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCursorPos(point);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoMouseUp(mbMiddle, shift, point.x, point.y);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;WM_MBUTTONDBLCLK:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoDblClick();<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; end;<br> &nbsp; end;<br><br> &nbsp; inherited Dispatch(Message);<br>end;<br><br>procedure TTrayIcon.DoClick;<br>begin<br> &nbsp; if (FAppRestore = imClick) then<br> &nbsp; &nbsp; &nbsp;Restore;<br> &nbsp; if (FPopupMenuShow = imClick) then<br> &nbsp; &nbsp; &nbsp;ShowMenu;<br><br> &nbsp; if assigned(FOnClick) then<br> &nbsp; &nbsp; &nbsp;FOnClick(self);<br>end;<br><br>procedure TTrayIcon.DoDblClick;<br>begin<br> &nbsp; if (FAppRestore = imDoubleClick) then<br> &nbsp; &nbsp; &nbsp;Restore;<br> &nbsp; if (FPopupMenuShow = imDoubleClick) then<br> &nbsp; &nbsp; &nbsp;ShowMenu;<br><br> &nbsp; if assigned(FOnDblClick) then<br> &nbsp; &nbsp; &nbsp;FOnDblClick(self);<br>end;<br><br>procedure TTrayIcon.DoMouseMove(Shift:TShiftState;X:integer;Y:integer);<br>begin<br> &nbsp; if assigned(FOnMouseMove) then<br> &nbsp; &nbsp; &nbsp;FOnMouseMove(self, Shift, X, Y);<br>end;<br><br>procedure TTrayIcon.DoMouseDown(Button:TMouseButton;Shift:TShiftState;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; X:integer;Y:integer);<br>begin<br> &nbsp; if (FAppRestore = imMouseDown) then<br> &nbsp; &nbsp; &nbsp;Restore;<br> &nbsp; if (FPopupMenuShow = imMouseDown) then<br> &nbsp; &nbsp; &nbsp;ShowMenu;<br><br> &nbsp; if assigned(FOnMouseDown) then<br> &nbsp; &nbsp; &nbsp;FOnMouseDown(self, Button, Shift, X, Y);<br>end;<br><br>procedure TTrayIcon.DoMouseUp(Button:TMouseButton ;Shift:TShiftState;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; X:integer;Y:integer);<br>begin<br> &nbsp; if (FAppRestore = imMouseDown) then<br> &nbsp; &nbsp; &nbsp;Restore;<br> &nbsp; if (FPopupMenuShow = imMouseDown) then<br> &nbsp; &nbsp; &nbsp;ShowMenu;<br><br> &nbsp; if assigned(FOnMouseUp) then<br> &nbsp; &nbsp; &nbsp;FOnMouseUp(self, Button, Shift, X, Y);<br>end;<br><br>function TTrayIcon.ApplicationHookProc(var Message:TMessage):boolean;<br>begin<br> &nbsp; if (Message.Msg = WM_SYSCOMMAND) then<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;if (Message.WParam = SC_MINIMIZE) then<br> &nbsp; &nbsp; &nbsp; &nbsp; Minimize;<br> &nbsp; &nbsp; &nbsp;if (Message.WParam = SC_RESTORE) then<br> &nbsp; &nbsp; &nbsp; &nbsp; Restore;<br> &nbsp; end;<br> &nbsp; result:= false;<br>end;<br><br>procedure TTrayIcon.Minimize;<br>begin<br> &nbsp; Application.Minimize;<br> &nbsp; ShowWindow(Application.Handle, SW_HIDE);<br><br> &nbsp; if assigned(FOnMinimize) then<br> &nbsp; &nbsp; &nbsp;FOnMinimize(self);<br>end;<br><br>procedure TTrayIcon.Restore;<br>begin<br> &nbsp; Application.Restore;<br> &nbsp; ShowWindow(Application.Handle, SW_RESTORE);<br> &nbsp; SetForegroundWindow(Application.Handle);<br><br> &nbsp; if assigned(FOnRestore) then<br> &nbsp; &nbsp; &nbsp;FOnRestore(self);<br>end;<br><br>procedure TTrayIcon.ShowMenu;<br>var<br>Point:Tpoint;<br>begin<br> &nbsp; GetCursorPos(point);<br><br> &nbsp; try<br> &nbsp; &nbsp; &nbsp;if (Screen.ActiveForm.Handle &lt;&gt;0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; SetForegroundWindow(Screen.ActiveForm.Handle);<br> &nbsp; except<br> &nbsp; end;<br><br> &nbsp; FPopupMenu.Popup(point.x, point.y);<br>end;<br><br>procedure TTrayIcon.SetDefaultIcon;<br>begin<br> &nbsp;FIcon.Assign(Application.Icon);<br> &nbsp;Update;<br>end;<br><br><br>procedure TTrayIcon.Notification(AComponent:TComponent;Operation:TOperation);<br>begin<br> &nbsp;inherited Notification(AComponent, Operation);<br> &nbsp;if (Operation = opRemove) then<br> &nbsp;begin<br> &nbsp; &nbsp; if (AComponent = FIconList) then<br> &nbsp; &nbsp; &nbsp; FIconList := nil<br> &nbsp; &nbsp; else if (AComponent = FPopupMenu) then<br> &nbsp; &nbsp; &nbsp; FPopupMenu := nil;<br> &nbsp;end;<br>end;<br><br>procedure TTrayIcon.Loaded;<br>begin<br> &nbsp; inherited Loaded;<br><br> &nbsp; if not assigned( FIconList) then<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;FAnimate := false;<br> &nbsp; &nbsp; &nbsp;FIcon.Assign(Application.Icon);<br> &nbsp; end<br> &nbsp; else<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;FTimer.Enabled := FAnimate;<br> &nbsp; &nbsp; &nbsp;FIconList.GetIcon(FIconIndex, FIcon);<br> &nbsp; end;<br><br> &nbsp; Update;<br>end;<br><br><br>procedure TTrayIcon.SetHint(Hint:String);<br>begin<br> &nbsp; // The new hint must be different than the previous hint and less than<br> &nbsp; // 64 characters to be modified. 64 is an operating system limit.<br> &nbsp; if ((FHint &lt;&gt; Hint) and (length(Hint) &lt; 64)) then<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;FHint := Hint;<br> &nbsp; &nbsp; &nbsp;StrPLCopy(FData.szTip, Hint, sizeof(FData.szTip) - 1);<br><br> &nbsp; &nbsp; &nbsp;// If there is no hint then there is no tool tip.<br> &nbsp; &nbsp; &nbsp;if Length(Hint)&gt;0 then<br> &nbsp; &nbsp; &nbsp; &nbsp; FData.uFlags := FData.uFlags or NIF_TIP<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; FData.uFlags := FData.uFlags and (not NIF_TIP);<br><br> &nbsp; &nbsp; &nbsp;Update;<br> &nbsp; end;<br>end;<br><br>function TTrayIcon.GetAnimateInterval:integer;<br>begin<br> &nbsp; result:= FTimer.Interval;<br>end;<br><br>procedure TTrayIcon.SetAnimateInterval(Value:integer);<br>begin<br> &nbsp; FTimer.Interval := Value;<br>end;<br><br>function TTrayIcon.GetAnimate:boolean;<br>begin<br> &nbsp; result:=FAnimate;<br>end;<br><br>procedure TTrayIcon.SetAnimate(Value:boolean);<br>begin<br> &nbsp; if (assigned(FIconList) or (csLoading in ComponentState)) then<br> &nbsp; &nbsp; &nbsp;FAnimate := Value;<br><br> &nbsp; if (assigned(FIconList) and (not (csDesigning in ComponentState))) then<br> &nbsp; &nbsp; &nbsp;FTimer.Enabled := Value;<br>end;<br><br><br>function TTrayIcon.ShiftState:TShiftState;<br>begin<br> &nbsp; if (GetKeyState(VK_SHIFT) &lt; 0) then<br> &nbsp; &nbsp; &nbsp;result:=result+[ssShift];<br> &nbsp; if (GetKeyState(VK_CONTROL) &lt; 0) then<br> &nbsp; &nbsp; &nbsp;result:=result+[ssCtrl];<br> &nbsp; if (GetKeyState(VK_MENU) &lt; 0) then<br> &nbsp; &nbsp; &nbsp;result:=result+[ssAlt];<br>end;<br><br><br>function TTrayIcon.GetHandle:HWND;<br>begin<br> &nbsp; result:= FData.Wnd;<br>end;<br><br><br><br>end.
 
TrayIcon1.PopupMenu := PopupMenu1;
 
学习中。。。<br>谢谢!
 
//函数声明:<br>procedure IconOnClick(var Msg: TMessage); message MI_ICONEVENT;<br>procedure WMSysCommand(var msg: TWMSysCommand); message WM_SysCommand;<br><br>procedure TFrmMain.WMSysCommand(var msg: TWMSysCommand);<br>begin<br> &nbsp;if msg.CmdType and $FFF0 = SC_MINIMIZE then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;FrmMain.WindowState:=wsMinimized;<br> &nbsp; &nbsp; &nbsp;hide;<br> &nbsp; &nbsp;end<br> &nbsp;else<br> &nbsp; &nbsp;inherited;<br>end;<br><br>//图标的响应函数<br>procedure TFrmMain.IconOnClick;<br>var<br> &nbsp;pt: TPoint;<br>begin<br> &nbsp;if (Msg.LParam= WM_LBUTTONDOWN) then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; FrmMain.Show;<br> &nbsp; &nbsp; &nbsp; FrmMain.WindowState:=wsNormal;<br> &nbsp; &nbsp; &nbsp; SetForegroundWindow(FrmMain.Handle);<br> &nbsp; &nbsp; end;<br> &nbsp;if &nbsp;(Msg.LParam= WM_RBUTTONDOWN) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;GetCursorPos(pt);<br> &nbsp; &nbsp; &nbsp;TMenu.Popup(pt.x, pt.y);<br> &nbsp; &nbsp;end;<br>end;
 
谢谢 wangminqi&amp;mysirius两位大虾,<br>我的问题已经解决了,呵~~
 
后退
顶部