tags:

views:

706

answers:

1

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 this document: http://support.microsoft.com/kb/135788), 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?

A: 

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