1

さまざまなシナリオで画面にコンテキストメニューを表示する必要があるアプリケーションに取り組んでいます。私が書いている関数では、NSWindowsまたはNSViewsにアクセスできません。この関数は10.6で完全に機能するため、popUpMenuPositioningItem:atLocation:inViewを使用したいと思います。ただし、10.5をサポートする必要があるため、この機能は使用できません。

ドキュメントに記載されているように、私が最も興味を持っている機能は次のとおりです。

ビューがnilの場合、場所は画面座標系で解釈されます。これにより、どのウィンドウからも切り離されたメニューをポップアップ表示できます。

基本的に、画面上の場所を指定してコンテキストメニューを表示する必要がありますが、関連するビューはありません。

10.5でこれを達成する方法はありますか?

4

2 に答える 2

1
// 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];
于 2010-12-23T04:22:01.937 に答える
0

Cocoa での方法はわかりませんが、Carbon 関数の PopUpMenuSelect を使用できるかもしれません。

于 2010-04-05T19:18:43.583 に答える