简体   繁体   English

如何为具有嵌入式NSProgressIndicator的菜单项创建NSMenuItem视图?

[英]How to create the NSMenuItem view for a menu item with an embedded NSProgressIndicator?

I would like to create a menu item with an embedded NSProgressIndicator , similar to the "Wi-Fi: Looking for Networks…" menu item of the Wi-Fi status menu: 我想创建一个带有嵌入式NSProgressIndicator的菜单项,类似于Wi-Fi状态菜单的“Wi-Fi:寻找网络......”菜单项:

Mac上打开的Wi-Fi状态菜单的屏幕截图显示“Wi-Fi:正在寻找网络...”菜单项

I think that I will need to use the setView: method as discussed at: Views in Menu Items . 我认为我需要使用setView:方法,如下所述: 菜单项中的视图 However, when I call setView: and pass an NSProgressIndicator , only the NSProgressIndicator is displayed. 但是,当我调用setView:并传递NSProgressIndicator ,只显示NSProgressIndicator

How can the custom menu item view be created to get a similar result as the "Wi-Fi: Looking for Networks…" menu item of the Wi-Fi status menu? 如何创建自定义菜单项视图以获得与Wi-Fi状态菜单的“Wi-Fi:寻找网络...”菜单项类似的结果?

As noted in the document to which you linked: 如您链接的文档中所述:

A menu item with a view does not draw its title, state, font, or other standard drawing attributes, and assigns drawing responsibility entirely to the view. 带有视图的菜单项不会绘制其标题,状态,字体或其他标准图形属性,并将绘图责任完全分配给视图。

So, if you want your menu item to look like a normal menu item plus some other stuff, your view has to draw the normal menu item features in addition to the other stuff. 因此,如果您希望您的菜单项看起来像普通菜单项以及其他一些东西,那么除了其他内容之外,您的视图还必须绘制正常的菜单项功能。 So, you could set the menu item's view to a custom view. 因此,您可以将菜单项的视图设置为自定义视图。 The progress indicator would be one subview of that custom view, but you would need other subviews for the other features. 进度指示器将是该自定义视图的一个子视图,但您需要其他功能的其他子视图。 For example, a text field for the text of the menu item and an image view for a state indicator (if your item shows state). 例如,菜单项文本的文本字段和状态指示器的图像视图(如果项目显示状态)。

You'll have to draw highlighting as the item is selected or tracked. 在选择或跟踪项目时,您必须绘制突出显示。 You'll also have to do mouse tracking. 您还必须进行鼠标跟踪。 Apple has some sample code which demonstrates various parts of this: Apple有一些示例代码,演示了以下各个部分:

MenuItemView MenuItemView
CustomMenus CustomMenus
GridMenu GridMenu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM