[英]TrackPopupMenu equivalent in OS X
Windows has this nifty little API to create & display a popup menu on the desktop even from a background hidden application. Windows 有这个漂亮的小 API 甚至可以从后台隐藏的应用程序在桌面上创建和显示弹出菜单。 Is there something similar in Mac?
Mac中是否有类似的东西?
I've a background process started by launcher (in user context) and would like to display a popup menu, with a few options for user to select, when it receives a command from the network.我有一个由启动器启动的后台进程(在用户上下文中),并且希望在收到来自网络的命令时显示一个弹出菜单,其中包含 select 的一些用户选项。 Possible?
可能的?
The background process itself is a plain C++ command line program.后台进程本身是一个普通的 C++ 命令行程序。
You can try to use popUpMenuPositioningItem:atLocation:inView: .您可以尝试使用popUpMenuPositioningItem:atLocation:inView: 。
The documentation says:文档说:
If view is nil, the location is interpreted in the screen coordinate system.
如果 view 为 nil,则在屏幕坐标系中解释位置。 This allows you to pop up a menu disconnected from any window.
这允许您弹出一个与任何 window 断开连接的菜单。
So, for example:因此,例如:
[myMenu popUpMenuPositioningItem:nil atLocation:[NSEvent mouseLocation] inView:nil];
For the record here's the code that I wrote that displays the menu on the desktop (I'm not a Mac programmer, so there could be errors or my implementation need not be the most optimal):作为记录,这里是我编写的在桌面上显示菜单的代码(我不是 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]);
}
}
Call HandleRButtonDown() from main and you should get a popup menu on the screen.从 main 调用 HandleRButtonDown() ,您应该会在屏幕上看到一个弹出菜单。
Hope this helps someone.希望这可以帮助某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.