OS X 中的 TrackPopupMenu 等价物
TrackPopupMenu equivalent in OS X
Windows 有这个漂亮的小东西 API 可以在桌面上创建和显示弹出菜单,即使是从后台隐藏的应用程序。 Mac中有类似的东西吗?
我有一个由启动器启动的后台进程(在用户上下文中),当它收到来自网络的命令时,我想显示一个弹出菜单,其中包含一些供用户选择的选项 select。可能吗?
后台进程本身是一个普通的 C++ 命令行程序。
您可以尝试使用popUpMenuPositioningItem:atLocation:inView:。
文档说:
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.
因此,例如:
[myMenu popUpMenuPositioningItem:nil atLocation:[NSEvent mouseLocation] inView:nil];
作为记录,这是我编写的在桌面上显示菜单的代码(我不是 Mac 程序员,因此可能存在错误或者我的实现不一定是最佳的):
// Dummy View class used to receive Menu Events
@interface DummyView : NSView
{
NSMenuItem* nsMenuItem;
}
- (void) OnMenuSelection:(id)sender;
- (NSMenuItem*)MenuItem;
@end
@implementation DummyView
- (NSMenuItem*)MenuItem
{
return nsMenuItem;
}
- (void)OnMenuSelection:(id)sender
{
nsMenuItem = sender;
}
@end
static void HandleRButtonDown()
{
@autoreleasepool {
NSRect graphicsRect; // contains an origin, width, height
graphicsRect = NSMakeRect(200, 200, 50, 100);
[NSApplication sharedApplication];
// Style flags:
NSUInteger windowStyle = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable;
// Window bounds (x, y, width, height).
NSRect windowRect = NSMakeRect(0, 0, 0, 0);
NSWindow * window = [[NSWindow alloc] initWithContentRect:windowRect
styleMask:windowStyle
backing:NSBackingStoreBuffered
defer:NO];
// Window controller:
NSWindowController * windowController = [[NSWindowController alloc] initWithWindow:window];
DummyView *nsView = [[DummyView alloc] initWithFrame:graphicsRect];
[window setContentView:nsView];
// Create Menu and Dummy View
NSMenu *nsMenu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];
[nsMenu setAutoenablesItems:NO];
NSMenuItem* item1 = [nsMenu addItemWithTitle:@"Menu Item #1" action:@selector(OnMenuSelection:) keyEquivalent:@""];
[item1 setTag:100];
[item1 setTarget:nsView];
[item1 setEnabled:YES];
NSMenuItem *item2 = [nsMenu addItemWithTitle:@"Menu Item #2" action:@selector(OnMenuSelection:) keyEquivalent:@""];
[item2 setTag:200];
[item2 setTarget:nsView];
[item2 setEnabled:YES];
[nsMenu popUpMenuPositioningItem:item1 atLocation:[NSEvent mouseLocation] inView:nsView];
printf("Selected item: %d\n", (int)[[nsView MenuItem] tag]);
}
}
从 main 调用 HandleRButtonDown(),您应该会在屏幕上看到一个弹出菜单。
希望这对某人有所帮助。
Windows 有这个漂亮的小东西 API 可以在桌面上创建和显示弹出菜单,即使是从后台隐藏的应用程序。 Mac中有类似的东西吗?
我有一个由启动器启动的后台进程(在用户上下文中),当它收到来自网络的命令时,我想显示一个弹出菜单,其中包含一些供用户选择的选项 select。可能吗?
后台进程本身是一个普通的 C++ 命令行程序。
您可以尝试使用popUpMenuPositioningItem:atLocation:inView:。
文档说:
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.
因此,例如:
[myMenu popUpMenuPositioningItem:nil atLocation:[NSEvent mouseLocation] inView:nil];
作为记录,这是我编写的在桌面上显示菜单的代码(我不是 Mac 程序员,因此可能存在错误或者我的实现不一定是最佳的):
// Dummy View class used to receive Menu Events
@interface DummyView : NSView
{
NSMenuItem* nsMenuItem;
}
- (void) OnMenuSelection:(id)sender;
- (NSMenuItem*)MenuItem;
@end
@implementation DummyView
- (NSMenuItem*)MenuItem
{
return nsMenuItem;
}
- (void)OnMenuSelection:(id)sender
{
nsMenuItem = sender;
}
@end
static void HandleRButtonDown()
{
@autoreleasepool {
NSRect graphicsRect; // contains an origin, width, height
graphicsRect = NSMakeRect(200, 200, 50, 100);
[NSApplication sharedApplication];
// Style flags:
NSUInteger windowStyle = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable;
// Window bounds (x, y, width, height).
NSRect windowRect = NSMakeRect(0, 0, 0, 0);
NSWindow * window = [[NSWindow alloc] initWithContentRect:windowRect
styleMask:windowStyle
backing:NSBackingStoreBuffered
defer:NO];
// Window controller:
NSWindowController * windowController = [[NSWindowController alloc] initWithWindow:window];
DummyView *nsView = [[DummyView alloc] initWithFrame:graphicsRect];
[window setContentView:nsView];
// Create Menu and Dummy View
NSMenu *nsMenu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];
[nsMenu setAutoenablesItems:NO];
NSMenuItem* item1 = [nsMenu addItemWithTitle:@"Menu Item #1" action:@selector(OnMenuSelection:) keyEquivalent:@""];
[item1 setTag:100];
[item1 setTarget:nsView];
[item1 setEnabled:YES];
NSMenuItem *item2 = [nsMenu addItemWithTitle:@"Menu Item #2" action:@selector(OnMenuSelection:) keyEquivalent:@""];
[item2 setTag:200];
[item2 setTarget:nsView];
[item2 setEnabled:YES];
[nsMenu popUpMenuPositioningItem:item1 atLocation:[NSEvent mouseLocation] inView:nsView];
printf("Selected item: %d\n", (int)[[nsView MenuItem] tag]);
}
}
从 main 调用 HandleRButtonDown(),您应该会在屏幕上看到一个弹出菜单。
希望这对某人有所帮助。