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?
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