德尔福(Delphi)“托盘"图标(NotifyIcon)上下文菜单在其外部单击时不会消失 [英] Delphi "Tray" icon (NotifyIcon) context-menu does not disappear when clicking outside it

查看:65
本文介绍了德尔福(Delphi)“托盘"图标(NotifyIcon)上下文菜单在其外部单击时不会消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我右键单击通知图标"时,我试图显示一个弹出菜单,效果很好;但是我希望菜单在用户单击菜单之外时消失.

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.

据推测是根据 KB135788-PRB:通知图标的菜单无法正常工作 (存档).

It's supposedly by design as per KB135788 - PRB: Menus for Notification Icons Do Not Work Correctly (archive).

但是我所认识的没有一个像样的应用程序具有这种行为.我尝试调用 SetForegroundWindow ,则无法使用弹出菜单的手柄.我敢肯定有可能解决此问题,就像我几年前所做的一样,但不记得怎么做.

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?

推荐答案

我找到了解决方案!

我在打电话SetForegroundWindow(PopupMenu1.Handle);

I was calling SetForegroundWindow(PopupMenu1.Handle);

这不起作用,但是在OnPopup事件处理程序中将其更改为

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;

有效!

很明显,如果在调用此表单时可见,则您的应用程序将首先跳转,但是如果它是隐藏的(就像我的一样),那么它将起作用.

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.

这篇关于德尔福(Delphi)“托盘"图标(NotifyIcon)上下文菜单在其外部单击时不会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆