How to close custom positioned PopupMenu in delphi?

◇◆丶佛笑我妖孽 提交于 2019-12-07 08:56:52

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/37789882/how-to-close-custom-positioned-popupmenu-in-delphi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!