NSMenu 的鼠标悬停事件
我使用 NSMenu 和 NSStatusItem 在状态栏上显示自定义菜单很长一段时间是这样的:
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:18]retain];
[statusItem setImage:[NSImage imageNamed:@"myIcon"]];
[statusItem setMenu:myMenu];
[statusItem setHighlightMode:NO];
...
通过开始单击其图像图标,它可以正常工作。 但我注意到苹果位于状态栏上的菜单可以通过鼠标移动简单地触发,例如机场菜单、电源菜单、语言菜单和日期/时间菜单。 当您将鼠标移过时,它们都会自动弹出。 他们是怎么得到的?
我已经用“mouseEntered”事件检查了 NSView 的“添加跟踪矩形”,但事情并不像我想象的那么困难。
有什么建议吗?
I used NSMenu and NSStatusItem to display custom menu on status bar for a long time in this way:
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:18] retain];
[statusItem setImage:[NSImage imageNamed:@"myIcon"]];
[statusItem setMenu:myMenu];
[statusItem setHighlightMode:NO];
...
It works fine by start clicking its image icon.
But I noticed that apple's menu located on status bar can be triggered simple by mouse move over, like the airport menu, power menu, language menu and date/time menu.
They are all auto pop up when you mouse walk over.
How did they get it?
I have checked the "add tracking rect" for NSView with "mouseEntered" event, but things is not as difficult as that I think.
Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 statusItem 不支持鼠标事件,但是您可以为 statusItem 设置视图并重写 mouse Even 方法来支持鼠标事件,就像这样:
这里只是一个为 statusItem 设置视图的示例,如果您愿意的话为了支持鼠标事件,您的视图必须扩展相同的视图并创建 NSTrackingArea 并实现 moveMoved、mouseEntered 和 mouseExited 方法(或您想要的任何方法)
I think the statusItem is not a supported mouse event,but you can set a view to the statusItem and override the mouse even method to support the mouse event, just like this:
here just an example to set a view to statusitem, if you want to support the mouse event , your view must extend same view and create NSTrackingArea and implement the moveMoved, mouseEntered and mouseExited methods (or whichever ones you want)