关于将程序加载到系统托盘后,如何使用右键菜单的问题(200分)

  • 主题发起人 主题发起人 DJ6674
  • 开始时间 开始时间
D

DJ6674

Unregistered / Unconfirmed
GUEST, unregistred user!
我编译时,提示IconOnClick定义了但没有使用.在托盘图标上单击或者右击时,没有反应.<br>代码如下:<br><br>unit main;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls,setting, ExtCtrls, jpeg, Menus,shellapi;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Image1: TImage;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; N1: TMenuItem;<br>&nbsp; &nbsp; N2: TMenuItem;<br>&nbsp; &nbsp; N3: TMenuItem;<br>&nbsp; &nbsp; N4: TMenuItem;<br>&nbsp; &nbsp; N5: TMenuItem;<br>&nbsp; &nbsp; N6: TMenuItem;<br>&nbsp; &nbsp; N7: TMenuItem;<br>&nbsp; &nbsp; N8: TMenuItem;<br>&nbsp; &nbsp; N9: TMenuItem;<br>&nbsp; &nbsp; X1: TMenuItem;<br>&nbsp; &nbsp; procedure FormClick(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure N7Click(Sender: TObject);<br>&nbsp; &nbsp; procedure X1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure N5Click(Sender: TObject);<br>&nbsp; &nbsp; procedure N3Click(Sender: TObject);<br>&nbsp; &nbsp; procedure N8Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; procedure IconOnClick(var message:TMessage);<br><br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br><br><br>var<br>&nbsp; Form1: TForm1;<br><br><br>implementation<br><br>{$R *.dfm}<br>//程序没有问题,但是下面一个过程应有的功能没有实现<br>procedure TForm1.IconOnClick(var message:Tmessage); &nbsp;//设置左右键功能<br>var p:Tpoint;<br>begin<br>&nbsp; &nbsp; &nbsp;if (message.LParam=WM_LBUTTONDOWN) then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Form2.Show;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;if (message.LParam=WM_RBUTTONDOWN) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetCursorPos(p);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PopupMenu1.Popup(p.X,p.Y);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>end;<br><br><br><br>&nbsp;//以下是装载图标的过程<br>procedure InstIcon(ToyIcon:TIcon;WinHandle:THandle;cbMessage:Integer);<br>const ICON_ID=3;<br>var IconData:TNotifyIconData;<br>begin<br>&nbsp; &nbsp; &nbsp;IconData.cbSize:=Sizeof(IconData);<br>&nbsp; &nbsp; &nbsp;IconData.Wnd:=WinHandle;<br>&nbsp; &nbsp; &nbsp;IconData.uID:=ICON_ID;<br>&nbsp; &nbsp; &nbsp;IconData.uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br>&nbsp; &nbsp; &nbsp;IconData.uCallbackMessage:=cbMessage;<br>&nbsp; &nbsp; &nbsp;IconData.hIcon:=ToyIcon.Handle;<br>&nbsp; &nbsp; &nbsp;IconData.szTip:='欢迎使用';<br>&nbsp; &nbsp; &nbsp;Shell_NotifyIcon(NIM_ADD,@IconData);<br>end;<br><br>//删除图标<br>procedure DeleIcon(winHandle:THandle);<br>const ICON_ID=3;<br>var IconData:TNotifyIconData;<br>begin<br>&nbsp; &nbsp; &nbsp;IconData.cbSize:=SizeOf(IconData);<br>&nbsp; &nbsp; &nbsp;IconData.Wnd:=winHandle;<br>&nbsp; &nbsp; &nbsp;IconData.uID:=ICON_ID;<br>&nbsp; &nbsp; &nbsp;Shell_NotifyIcon(NIM_DELETE,@IconData);<br>end;<br><br>procedure TForm1.FormClick(Sender: TObject);<br>begin<br>Form1.Hide;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>DeleIcon(Handle);<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>const<br>MI_ICONEVENT=WM_USER+1;<br>begin<br>InstIcon(Application.Icon,Application.Handle,MI_ICONEVENT);<br>end;<br><br>procedure TForm1.N7Click(Sender: TObject);<br>begin<br>end;<br><br>procedure TForm1.X1Click(Sender: TObject);<br>begin<br>end;<br><br>procedure TForm1.N5Click(Sender: TObject);<br>begin<br>end;<br><br>procedure TForm1.N3Click(Sender: TObject);<br>begin<br>end;<br><br>procedure TForm1.N8Click(Sender: TObject);<br>begin<br>end;<br><br><br>end.
 
IconOnClick事件是用户自己定义的事件,需要说明一下。<br>首先要声明一个常量:<br>const<br>&nbsp; WM_ICONCLICK=WM_USER+1;<br><br>然后:<br>private<br>&nbsp; &nbsp;procedure IconOnClick(var message:TMessage);message WM_ICONCLICK;<br><br>在<br>procedure InstIcon(ToyIcon:TIcon;WinHandle:THandle;cbMessage:Integer);<br>中:<br>IconData.uCallbackMessage:=WM_ICONCLICK;<br>
 
标题:如何让程序最小化时跑到系统托盘里面? (50分) <br>西凉老猫 (2003-5-22 9:47) 1884320 <br>在线等! <br>zhu_jy (2003-5-22 10:0) <br>网上能找到一个系统托盘控件,再在里面小改一下就能达到你的要求。 <br>zhu_jy (2003-5-24 10:23) <br>unit TrayIcon;<br><br>interface<br><br>{ A component to make it easier to create a system tray icon.<br>&nbsp; Drop this component on a form, and the application automatically<br>&nbsp; becomes a tray icon. This means that when the application is<br>&nbsp; minimized, it does not minimize to a normal taskbar icon, but<br>&nbsp; to the little system tray on the side of the taskbar. A popup<br>&nbsp; menu is available from the system tray icon, and your application<br>&nbsp; can process mouse events as the user moves the mouse over<br>&nbsp; the system tray icon, clicks on the icon, etc.<br><br>&nbsp; Copyright ?1996 Tempest Software. All rights reserved.<br>&nbsp; You may use this software in an application without fee or royalty,<br>&nbsp; provided this copyright notice remains intact.<br>}<br><br>uses<br>&nbsp; Windows, Messages, ShellApi, SysUtils, Classes, Graphics, Controls,<br>&nbsp; Forms, Dialogs, Menus;<br><br>{ This message is sent to the special, hidden window for shell<br>&nbsp; notification messages. Only derived classes might need to<br>&nbsp; know about it. }<br>const<br>&nbsp; Wm_Callback_Message = Wm_User + 1;<br><br>type<br>&nbsp; TTrayIcon = class(TComponent)<br>&nbsp; private<br>&nbsp; &nbsp; fData: TNotifyIconData;<br>&nbsp; &nbsp; fIcon: TIcon;<br>&nbsp; &nbsp; fHint: string;<br>&nbsp; &nbsp; fPopupMenu: TPopupMenu;<br>&nbsp; &nbsp; fClicked: Boolean;<br>&nbsp; &nbsp; fOnClick: TNotifyEvent;<br>&nbsp; &nbsp; fOnDblClick: TNotifyEvent;<br>&nbsp; &nbsp; fOnMinimize: TNotifyEvent;<br>&nbsp; &nbsp; fOnMouseMove: TMouseMoveEvent;<br>&nbsp; &nbsp; fOnMouseDown: TMouseEvent;<br>&nbsp; &nbsp; fOnMouseUp: TMouseEvent;<br>&nbsp; &nbsp; fOnRestore: TNotifyEvent;<br>&nbsp; protected<br>&nbsp; &nbsp; procedure SetHint(const Hint: string); virtual;<br>&nbsp; &nbsp; procedure SetIcon(Icon: TIcon); virtual;<br>&nbsp; &nbsp; procedure AppMinimize(Sender: TObject);<br>&nbsp; &nbsp; procedure AppRestore(Sender: TObject);<br>&nbsp; &nbsp; procedure DoMenu; virtual;<br>&nbsp; &nbsp; procedure Click; virtual;<br>&nbsp; &nbsp; procedure DblClick; virtual;<br>&nbsp; &nbsp; procedure EndSession; virtual;<br>&nbsp; &nbsp; procedure DoMouseMove(Shift: TShiftState; X, Y: Integer); virtual;<br>&nbsp; &nbsp; procedure DoMouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); virtual;<br>&nbsp; &nbsp; procedure DoMouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); virtual;<br>&nbsp; &nbsp; procedure OnMessage(var Msg: TMessage); virtual;<br>&nbsp; &nbsp; procedure Changed; virtual;<br>&nbsp; &nbsp; property Data: TNotifyIconData read fData;<br>&nbsp; public<br>&nbsp; &nbsp; constructor Create(Owner: TComponent); override;<br>&nbsp; &nbsp; destructor Destroy; override;<br>&nbsp; &nbsp; procedure Minimize; virtual;<br>&nbsp; &nbsp; procedure Restore; virtual;<br>&nbsp; published<br>&nbsp; &nbsp; property Hint: string read fHint write SetHint;<br>&nbsp; &nbsp; property Icon: TIcon read fIcon write SetIcon;<br>&nbsp; &nbsp; property PopupMenu: TPopupMenu read fPopupMenu write fPopupMenu;<br>&nbsp; &nbsp; property OnClick: TNotifyEvent read fOnClick write fOnClick;<br>&nbsp; &nbsp; property OnDblClick: TNotifyEvent read fOnDblClick write fOnDblClick;<br>&nbsp; &nbsp; property OnMinimize: TNotifyEvent read fOnMinimize write fOnMinimize;<br>&nbsp; &nbsp; property OnMouseMove: TMouseMoveEvent read fOnMouseMove write fOnMouseMove;<br>&nbsp; &nbsp; property OnMouseDown: TMouseEvent read fOnMouseDown write fOnMouseDown;<br>&nbsp; &nbsp; property OnMouseUp: TMouseEvent read fOnMouseUp write fOnMouseUp;<br>&nbsp; &nbsp; property OnRestore: TNotifyEvent read fOnRestore write fOnRestore;<br>&nbsp; end;<br><br>procedure Register;<br><br>implementation<br><br>{ Create the component. At run-time, automatically add a tray icon<br>&nbsp; with a callback to a hidden window. Use the application icon and title. }<br>constructor TTrayIcon.Create(Owner: TComponent);<br>begin<br>&nbsp; inherited Create(Owner);<br>&nbsp; fIcon := TIcon.Create;<br>&nbsp; fIcon.Assign(Application.Icon);<br>&nbsp; if not (csDesigning in ComponentState) then<br>&nbsp; begin<br>&nbsp; &nbsp; FillChar(fData, SizeOf(fData), 0);<br>&nbsp; &nbsp; fData.cbSize := SizeOf(fData);<br>&nbsp; &nbsp; fData.Wnd := AllocateHwnd(OnMessage);<br>&nbsp; &nbsp; fData.hIcon := Icon.Handle;<br>&nbsp; &nbsp; StrPLCopy(fData.szTip, Application.Title, SizeOf(fData.szTip)-1);<br>&nbsp; &nbsp; fData.uFlags := Nif_Icon or Nif_Message;<br>&nbsp; &nbsp; if Application.Title &amp;lt;&amp;gt; '' then<br>&nbsp; &nbsp; &nbsp; fData.uFlags := fData.uFlags or Nif_Tip;<br>&nbsp; &nbsp; fData.uCallbackMessage := Wm_Callback_Message;<br>&nbsp; &nbsp; if not Shell_NotifyIcon(Nim_Add, @fData) then<br>&nbsp; &nbsp; &nbsp; raise EOutOfResources.Create('Cannot create shell notification icon');<br>&nbsp; &nbsp; { Replace the application's minimize and restore handlers with<br>&nbsp; &nbsp; &nbsp; special ones for the tray. The TrayIcon component has its own<br>&nbsp; &nbsp; &nbsp; OnMinimize <br>任豆豆 (2003-5-24 10:33) <br>1。留下email给你发一个相关的控件<br>2。稍等我正在帮你写用api的代码<br>&nbsp;<br>任豆豆 (2003-5-24 10:42) <br>//用api的例子,你可以根据需要修改<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs,shellApi, ExtCtrls;<br>const<br>&nbsp; WM_MY_Notify = WM_USER + 100;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Image1: TImage;<br>&nbsp; private<br>&nbsp; &nbsp; MyIcon : PNOTIFYICONDATA;<br>&nbsp; &nbsp; Icon : TIcon;<br>&nbsp; &nbsp; procedure WMSysCommand(var Message: TWMSysCommand);message WM_SYSCOMMAND;//截获最小化的消息<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; procedure NotifyIconClick(var msg : TMessage);message WM_My_Notify;//系统托盘图标的鼠标消息<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.NotifyIconClick(var msg: TMessage);<br>//var<br>// &nbsp;ClickPoint :TPoint;<br>begin<br>&nbsp; &nbsp; case Msg.LParam of<br>&nbsp; &nbsp; &nbsp; WM_RBUTTONDOWN://鼠标右键<br>&nbsp; &nbsp; &nbsp; begin<br>// &nbsp; &nbsp; &nbsp; &nbsp;GetCursorPos(ClickPoint);<br>// &nbsp; &nbsp; &nbsp; &nbsp;NPopExit.Active := True;<br>// &nbsp; &nbsp; &nbsp; &nbsp;coolPopMain.ShowMenu(ClickPoint.x,ClickPoint.y);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; WM_LBUTTONDBLCLK:<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //。。。。。。。<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>end;<br>&nbsp;<br>
 
参考一些较成熟的TrayIcon控件吧
 
其实,如果你不反对三方控件的话,用RX控件组中的<br>RxTrayIcon1就能很方便的解决问题了。<br>http://www.51delphi.com/delphi/soft?cx=RXLIB<br>这就有的
 
CoolTray多好用啊,功能又全
 
handel 为 form1的handle;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>const<br>MI_ICONEVENT=WM_USER+1;<br>begin<br>&nbsp; InstIcon(Application.Icon,Handle,MI_ICONEVENT); <br>end;<br>
 
你的程序中已经有了:<br>&nbsp; &nbsp; IconData.uCallbackMessage:=cbMessage;// 回调函数消息<br><br>但是没有定义cbMessage,所以要先定义消息。<br>{自定义消息,当小图标捕捉到鼠标事件时Windows向回调函数发送此消息}<br>const cbMessage = WM_USER + 100;<br><br>另外miaofeng,说的对,再修改下面这一行代码,andy263给的帖子也说的清楚了,呵呵<br>private<br>&nbsp; procedure IconOnClick(var message:TMessage);message cbMessage;<br><br><br>
 
先声明:<br>public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; procedure WndProc(var Msg: TMessage); override;<br><br>然后:<br>procedure tform1.WndProc(var Msg: TMessage);<br>var<br>IconID:integer;<br>pt:TPOINT;<br>begin<br>if msg.Msg = WM_TRAYNOTIFY then<br>begin<br>iconID := msg.WParam;<br>//获取鼠标在屏幕上的位置<br>GetCursorPos(pt);<br>WM_LBUTTONDOWN:<br> begin<br> //鼠标左键被按下<br> end;<br>WM_RBUTTONDOWN:<br> begin<br> //鼠标右键被按下<br>&nbsp; end;<br>WM_LBUTTONUP:<br> begin<br> //松开鼠标左键<br> end;<br>WM_RBUTTONUP:<br> begin<br> //松开鼠标右键<br> end;<br>WM_MOUSEMOVE:<br> begin<br> //鼠标在图标上移动<br> end;<br>WM_LBUTTONDBLCLK:<br> begin<br> //左键双击<br> end;<br>WM_RBUTTONDBLCLK:<br> begin<br> //鼠标右键双击<br>&nbsp; end;<br>end;<br>end<br>else//调用父类的wndproc处理其他消息<br>inherited;<br><br>end;
 
后退
顶部