一个小问题(50分)

  • 主题发起人 主题发起人 朱浩刚
  • 开始时间 开始时间

朱浩刚

Unregistered / Unconfirmed
GUEST, unregistred user!
一程序启动时我用shell_notifyicon将其在taskbar的右下角出现,现我自己定义<br>了一个消息和一个处理该消息的函数,当mouse左,右键分别按下的时候分别响应<br>不同的动作。现在我想知道如何来实现当mouse单击图标的时候,我定义的消息<br>函数被执行。
 
mainform.onmousedown事件, 或者application.onmessage事件
 
在调用Shell_NotifyIcon是指定了一个自定义消息和接收该消息的窗口句柄。当有消息时<br>shell会给该指定的窗口发消息。你应该接收并处理该消息,在消息的wParam里有鼠标动<br>作(Move,LClick,RClick,……)。<br><br>(是不是没人用Delphi4了,我有Delphi4的控件,使用方便,就是时间太长了,<br>原码不知道仍哪儿去了)
 
昨天我刚刚写了这个程序。<br>看看我的代码吧。(支持 nt)<br>我还接获了窗口极小化的消息。你可以自己修改。<br>需包含 shellAPI 单元。<br><br>const<br>&nbsp; WM_TRAYNOTIFY = WM_USER + 1234; &nbsp;//自定义trayICON消息<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; Show1: TMenuItem;<br>&nbsp; &nbsp; Exit1: TMenuItem;<br>&nbsp; &nbsp; About1: TMenuItem;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Show1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Exit1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; tnd: NOTIFYICONDATA;<br>&nbsp; &nbsp; procedure MyWindowProc(var Message: TMessage);<br>&nbsp; public<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.MyWindowProc(var Message: TMessage);<br>var<br>&nbsp; p:tpoint;<br>begin<br>&nbsp; case Message.Msg of<br>&nbsp; &nbsp; WM_TRAYNOTIFY: begin<br>&nbsp; &nbsp; &nbsp; case Message.LParam of<br>&nbsp; &nbsp; &nbsp; &nbsp; WM_RBUTTONDOWN: begin //鼠标右健click<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCursorPos(p);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PopupMenu1.Popup(p.x, p.y);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; WM_LBUTTONDBLCLK: // 左键双击<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Show1.Click;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; WM_SYSCOMMAND: begin<br>&nbsp; &nbsp; &nbsp; if (Message.WParam = SC_MINIMIZE) then // 如果是极小化<br>&nbsp; &nbsp; &nbsp; &nbsp; Button1.Click<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; Dispatch(Message);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Dispatch(Message);<br>&nbsp; end;<br>end;<br><br>// 隐藏窗口,建立TrayICON<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; tnd.cbSize:= sizeof(NOTIFYICONDATA);<br>&nbsp; tnd.Wnd:= self.Handle;<br>&nbsp; tnd.uID:= 0;<br>&nbsp; tnd.uFlags:= NIF_MESSAGE or NIF_ICON or NIF_TIP;<br>&nbsp; tnd.uCallbackMessage:= WM_TRAYNOTIFY;<br>&nbsp; tnd.hIcon:= self.Icon.Handle;<br>&nbsp; tnd.szTip:= '耙子的例子 93611.yeah.net';<br>&nbsp; if Shell_notifyIcon(NIM_ADD, @tnd) then<br>&nbsp; begin<br>&nbsp; &nbsp; Self.Hide;<br>&nbsp; end;<br>end;<br><br>// Popup &nbsp;show 菜单<br>procedure TForm1.Show1Click(Sender: TObject);<br>begin<br>&nbsp; Self.Show;<br>&nbsp; Shell_notifyIcon(NIM_DELETE, @tnd);<br>end;<br><br>procedure TForm1.Exit1Click(Sender: TObject);<br>begin<br>&nbsp; Close;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; // 替换 TFrom 的 WindowProc<br>&nbsp; WindowProc:= MyWindowProc;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; Shell_NotifyIcon(NIM_DELETE, @TND);<br>end;<br><br>
 
多人接受答案了。
 
后退
顶部