如何使任务栏托盘区程序快捷菜单快速消失(100分)

  • 主题发起人 主题发起人 Micro Whaight
  • 开始时间 开始时间
M

Micro Whaight

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp; &nbsp;现在已经按照Shell_NotifyIcon建立了托盘区程序, 同时也写了相应的事件<br>处理(代码片断如下). 但是根据这种方式建立起来的左/右键菜单, 在点击其它窗<br>口或者桌面时, 并不会马上消失, 而是要过一段时间. 而Windows的那些个托盘程<br>序, 如输入法, 音量调整等程序, 在通过左/右键弹出相应的菜单后, 如果不选, <br>而是直接点击桌面或者其它窗口, 那些菜单会很快消失. 不知道这需要处理那个消<br>息?<br><br>&nbsp; &nbsp; 是不是和我的程序存在一个可见的窗口有关?<br>&nbsp; &nbsp; <br>附:代码片断<br><br>...<br>type<br>&nbsp; TfrmDisplay = class(TForm)<br>&nbsp; ...<br>&nbsp; public<br>&nbsp; &nbsp; procedure WndProc(var Msg: TMessage); override;<br>end;<br><br><br>implementation<br>...<br>const<br>&nbsp; WM_TRAYNOTIFY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= WM_USER + 1;<br>...<br>var<br>&nbsp; NotifyData &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : NOTIFYICONDATA;<br>...<br>procedure TfrmDisplay.FormCreate(Sender: TObject);<br>begin<br>&nbsp; ...<br>&nbsp; //注册托盘图标<br>&nbsp; with NotifyData do begin<br>&nbsp; &nbsp; cbSize := SizeOf(NotifyData);<br>&nbsp; &nbsp; Wnd := Handle;<br>&nbsp; &nbsp; uID := 0;<br>&nbsp; &nbsp; uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;<br>&nbsp; &nbsp; uCallbackMessage := WM_TRAYNOTIFY;<br>&nbsp; &nbsp; hIcon := IconArray[FaceNormal];<br>&nbsp; &nbsp; StrPLCopy(szTip, '托盘区', 6);<br>&nbsp; end;<br>&nbsp; Shell_NotifyIcon(NIM_ADD, @NotifyData);<br>end;<br><br>procedure TfrmDisplay.WndProc(var Msg: TMessage);<br>var<br>&nbsp; pt &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : TPoint;<br>begin<br>&nbsp; if Msg.Msg = WM_TRAYNOTIFY then begin<br>&nbsp; &nbsp; if Msg.LParam = WM_LBUTTONDOWN then begin<br>&nbsp; &nbsp; &nbsp; GetCursorPos(pt);<br>&nbsp; &nbsp; &nbsp; pmSet.Popup(pt.x, pt.y);<br>&nbsp; &nbsp; &nbsp;////---&gt;是一个TPopupMenu.<br>&nbsp; &nbsp; end;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; inherited;<br>end;<br><br>
 
在你的 procedure TfrmDisplay.WndProc(var Msg: TMessage);中:<br>....... <br>&nbsp; &nbsp; &nbsp;if Message.LParam=WM_RBUTTONDOWN then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetCursorPos(pt);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetForegroundWindow(handle);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;popupmenu1.Popup(pt.x,pt.y);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PostMessage(Application.MainForm.Handle, WM_NULL, 0, 0);//加上这一句<br>//这一句最关键,向应用程序发出一个空的消息就可以了。<br>&nbsp; &nbsp; &nbsp; end;<br>//sendmessage(0,0,0,0)可能也可以吧,没试过。我用的是上面的语句,你可以试试看。<br>
 
{点击拖盘区图标}<br>procedure TForm.TrayIconClick(Sender: TObject; Button: TMouseButton;<br>&nbsp; Shift: TShiftState; X, Y: Integer);<br>begin<br>&nbsp; SetForeGroundWindow(Handle);<br>&nbsp; PopMenu.Popup(X, Y);//你的弹出式菜单<br>&nbsp; PostMessage(0, 0, 0, 0);<br>end;<br>
 
&nbsp;实测结果: 只要用SetForegroundWindow(Handle);即可以达到效果.<br>应该是因为把窗口提到了最前端.不过, 如果要做的是完全没有窗口类<br>型的程序, 就不知道如何处理了.比如完全和那个调整音量的图标一样<br>的程序...<br><br>&nbsp; &nbsp;谢谢两位!
 
后退
顶部