Delphi “Tray” icon (NotifyIcon) context-menu does not disappear when clicking outside it

雨燕双飞 提交于 2019-12-04 05:45:57

问题


I'm trying to show a popup menu when right clicking my Notification Icon, which works fine; But I want the menu to disappear when the user clicks outside of that menu.

It's supposedly by design as per KB135788 - PRB: Menus for Notification Icons Do Not Work Correctly (archive).

But no decent app I know of behaves like this. I've tried calling SetForegroundWindow using the popup menu's handle to no avail. I'm sure it's possible to work around this, as I've done it years ago but don't remember how.

Anyone know how to achieve the expected behaviour?


回答1:


I've found a solution!

I was calling SetForegroundWindow(PopupMenu1.Handle);

Which doesn't work, but changing this in the OnPopup event handler to

procedure TForm1.PopupMenu1Popup(Sender: TObject);
begin
  // Hack to fix the "by design" behaviour of popups from notification area icons. 
  // See: http://support.microsoft.com/kb/135788
  BringToFront();
end;

Works!

Obviously, if the form is visible when this is called, your app will jump foremost, but if it's hidden (as mine is), then it'll work.

I'd be interested to know if there's a way to make the menu work right without the window jumping foremost, though.



来源:https://stackoverflow.com/questions/1188905/delphi-tray-icon-notifyicon-context-menu-does-not-disappear-when-clicking-ou

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!