寻找 popUpMenuPositioningItem:atLocation:inView: 相当于 10.5

发布于 2024-08-20 11:49:59 字数 361 浏览 8 评论 0原文

我正在开发一个需要在各种场景下在屏幕上显示上下文菜单的应用程序。在我正在编写的函数中,我无权访问任何 NSWindows 或 NSView。我想使用 popUpMenuPositioningItem:atLocation:inView 因为这个函数在 10.6 中非常适合我。但是,我们要求支持10.5,所以这个功能对我来说不可用。

正如文档中所述,我最感兴趣的功能是:

如果视图为零,则位置为 在屏幕坐标中解释 系统。这允许您弹出一个 菜单与任何窗口断开连接。

基本上,我需要显示屏幕上给定位置的上下文菜单,但没有任何关联的视图。

在10.5上有什么办法可以实现这一点吗?

I'm working on an application that needs to display a context menu on screen in various scenarios. In the function I'm writing, I don't have access to any NSWindows or NSViews. I'd like to use popUpMenuPositioningItem:atLocation:inView as this function works perfectly for me in 10.6. However, we have a requirement to support 10.5, so this function isn't available to me.

The feature I'm most interested in, as stated in the documentation is:

If view is nil, the location is
interpreted in the screen coordinate
system. This allows you to pop up a
menu disconnected from any window.

Basically, I need to display the context menu given a location on screen, but without any associated view.

Is there any way to achieve this on 10.5?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拧巴小姐 2024-08-27 11:50:00
// Set up the button cell, converting to NSView coordinates. The menu is
// positioned such that the currently selected menu item appears over the
// popup button, which is the expected Mac popup menu behavior.
NSPopUpButtonCell* button = [[NSPopUpButtonCell alloc] initTextCell:@""
                                                          pullsDown:NO];
[button autorelease];
[button setMenu:menu_];
// We use selectItemWithTag below so if the index is out-of-bounds nothing
// bad happens.
[button selectItemWithTag:index];
[button setFont:[NSFont menuFontOfSize:fontSize_]];

// Create a dummy view to associate the popup with, since the OS will use
// that view for positioning the menu.
NSView* dummyView = [[[NSView alloc] initWithFrame:bounds] autorelease];
[view addSubview:dummyView];
NSRect dummyBounds = [dummyView convertRect:bounds fromView:view];

// Display the menu, and set a flag if a menu item was chosen.
[button performClickWithFrame:dummyBounds inView:dummyView];

if ([self menuItemWasChosen])
  index_ = [button indexOfSelectedItem];

[dummyView removeFromSuperview];
// Set up the button cell, converting to NSView coordinates. The menu is
// positioned such that the currently selected menu item appears over the
// popup button, which is the expected Mac popup menu behavior.
NSPopUpButtonCell* button = [[NSPopUpButtonCell alloc] initTextCell:@""
                                                          pullsDown:NO];
[button autorelease];
[button setMenu:menu_];
// We use selectItemWithTag below so if the index is out-of-bounds nothing
// bad happens.
[button selectItemWithTag:index];
[button setFont:[NSFont menuFontOfSize:fontSize_]];

// Create a dummy view to associate the popup with, since the OS will use
// that view for positioning the menu.
NSView* dummyView = [[[NSView alloc] initWithFrame:bounds] autorelease];
[view addSubview:dummyView];
NSRect dummyBounds = [dummyView convertRect:bounds fromView:view];

// Display the menu, and set a flag if a menu item was chosen.
[button performClickWithFrame:dummyBounds inView:dummyView];

if ([self menuItemWasChosen])
  index_ = [button indexOfSelectedItem];

[dummyView removeFromSuperview];
疯狂的代价 2024-08-27 11:50:00

我不知道如何在 Cocoa 中做到这一点,但你也许可以使用 Carbon 函数 PopUpMenuSelect。

I don't know how to do it in Cocoa, but you could maybe use the Carbon function PopUpMenuSelect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文