0

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?

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Drarok
  • 3,612
  • 2
  • 31
  • 48

1 Answers1

0

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.

Drarok
  • 3,612
  • 2
  • 31
  • 48
  • 1
    The reason your `SetForegroundWindow` call didn't work is that you were giving it a *menu* handle instead of a *window* handle. They're not interchangeable. You should have used just `Handle` (a.k.a. `Self.Handle`) instead. – Rob Kennedy Jul 27 '09 at 16:58
  • Agreed. In all of my systray apps, I use SetForegroundWindow() to focus the TForm before displaying the popup menu. I also issue a WM_NULL message to the TForm after the popup menu closes as well. – Remy Lebeau Aug 01 '09 at 01:59
  • never checked SetForegroundWindow status, allowing it to fail silently? – OnTheFly Nov 11 '11 at 12:54