gpt4 book ai didi

cocoa - 寻找 popUpMenuPositioningItem :atLocation:inView: equivalent for 10. 5

转载 作者:行者123 更新时间:2023-12-03 17:17:49 24 4
gpt4 key购买 nike

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

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

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.

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

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

最佳答案

// 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];

关于cocoa - 寻找 popUpMenuPositioningItem :atLocation:inView: equivalent for 10. 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2203708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com