Board index » delphi » Popup menu won't disappear

Popup menu won't disappear

I'm using a tray icon component that shows a popup menu on right click.
The popup menu is not a property of the tray icon but shows up using

var
   Pnt: TPoint;
begin
   GetCursorPos(Pnt);
   PopupMenu1.Popup(Pos.x, Pos.y);
end;

So far no problems.
But clicking outside the popup menu it doesn't disappear. In Windows 98
it disappears when moving the mouse over it, in Windows 2000 and XP you
have to click a menu item to make it disappear.

I'm using D5 and D6.

Any ideas what could be wrong?

Thanx in advance
Kerstin

 

Re:Popup menu won't disappear


Quote
Kerstin Thaler <kers...@sdcomputers.net> wrote:
>I'm using a tray icon component that shows a popup menu on right click.
>The popup menu is not a property of the tray icon but shows up using

>var
>   Pnt: TPoint;
>begin
>   GetCursorPos(Pnt);
>   PopupMenu1.Popup(Pos.x, Pos.y);
>end;

>So far no problems.
>But clicking outside the popup menu it doesn't disappear. In Windows 98
>it disappears when moving the mouse over it, in Windows 2000 and XP you
>have to click a menu item to make it disappear.

>I'm using D5 and D6.

>Any ideas what could be wrong?

Yes. From the Microsoft Knowledge Base:

Q135788:
When you display a context menu for a notification icon (see
Shell_NotifyIcon), clicking anywhere besides the menu or the window
that created the menu (if it is visible) doesn't cause the menu to
disappear. When this behavior is corrected, the second time this menu
is displayed, it displays and then immediately disappears.

RESOLUTION
To correct the first behavior, you need to make the current window the
foreground window before calling TrackPopupMenu or TrackPopupMenuEx.

The second problem is caused by a problem with TrackPopupMenu. It is
necessary to force a task switch to the application that called
TrackPopupMenu at some time in the near future. This can be
accomplished by posting a benign message to the window or thread.

The following code will take care of all of this:

   SetForegroundWindow(hDlg);
   // Display the menu
   TrackPopupMenu(   hSubMenu,
                     TPM_RIGHTBUTTON,
                     pt.x,
                     pt.y,
                     0,
                     hDlg,
                     NULL);
   PostMessage(hDlg, WM_NULL, 0, 0);

[End of citation]

The Delphi version of this is a bit simpler:

    SetForegroundWindow(Handle);
    GetCursorPos(Point);
    with Point do PopupPMu.Popup(X, Y);
    PostMessage(Handle, WM_NULL, 0, 0);

Good luck.

Kurt

Re:Popup menu won't disappear


Thanks a lot, it works  :)

Kerstin

Other Threads