Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close custom positioned PopupMenu in delphi?

Tags:

popup

delphi

I have a project with CoolTrayIcon and PopupMenu with disabled AutoPopup property. I would like to position the PopupMenu and show it for the user. The position is OK but menu doesn't close if the user clicks away or press ESC button. I have not found any property like Active which could help if the menu is used or not.

Here I position the menu:

procedure TForm1.CoolTrayIcon1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  pnt: TPoint; yy:integer;
begin

GetCursorPos(pnt);
yy:=pnt.y; yy:=yy-500;

if (Button=mbRight) then begin
    PopupMenu1.Popup(pnt.X, yy);
end;

end;

How could I manage to close menu if it is needed?

like image 620
roll Avatar asked Feb 07 '23 19:02

roll


1 Answers

This is a known issue that is discussed here:

PRB: Menus for Notification Icons Do Not Work Correctly

You need to wrap the call to Popup() as follows:

SetForegroundWindow(Handle);
PopupMenu1.Popup(pnt.X, yy);
PostMessage(Handle, WM_NULL, 0, 0);

In this code, Handle is the window handle of the form associated with the notification icon.

like image 146
David Heffernan Avatar answered Feb 09 '23 07:02

David Heffernan