如何在鼠标光标处显示 NSMenu?
我正在开发一个应用程序,我想在当前鼠标位置旁边显示 NSMenu“上下文菜单”。此时,我知道如何从 WebView 触发命令来显示上下文菜单,但我似乎无法让菜单显示在屏幕上的正确位置。看来我的坐标与我需要的垂直反转。这似乎是一个简单的问题,但我花了很多时间尝试最好的“最佳实践”解决方案。我是否需要弄清楚鼠标所在的屏幕并手动计算 y 坐标?
这是我非常简单的代码:
- (IBAction)showMenu:(id)sender
{
CGEventRef ourEvent = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(ourEvent);
NSPoint wp = {0,0};
wp = [self.window convertScreenToBase:point];
NSLog(@"Location? x= %f, y = %f", (float)point.x, (float)point.y);
NSLog(@"Location? x= %f, y = %f", (float)wp.x, (float)wp.y);
NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseUp location:CGPointMake(wp.x, wp.y) modifierFlags:0 timestamp:NSTimeIntervalSince1970 windowNumber:[_window windowNumber] context:nil eventNumber:0 clickCount:0 pressure:0.1];
//NSEvent *event = [NSEvent eventWithCGEvent:ourEvent];
NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];
[theMenu insertItemWithTitle:@"Item 1" action:@selector(beep:) keyEquivalent:@"" atIndex:0];
[theMenu insertItemWithTitle:@"Item 2" action:@selector(beep:) keyEquivalent:@"" atIndex:1];
[NSMenu popUpContextMenu:theMenu withEvent:event forView:nil];
}
有人可以为我提供示例代码,以最自然的方式完成此任务。
I am working on an app where I want to show an NSMenu "context menu" next to the current mouse location. At this point, I know how to trigger the command from the WebView to show the context menu, I just can't seem to get the menu to show up at the correct placement on the screen. It seems like my coordinates are inverted vertically from what I need. This seems like such a simple problem, but I have spent a good bit of time trying to best a "best practices" solution. Do I need to figure out which screen the mouse is in and calculate the y coordinate manually?
Here is my really simple code that gets close:
- (IBAction)showMenu:(id)sender
{
CGEventRef ourEvent = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(ourEvent);
NSPoint wp = {0,0};
wp = [self.window convertScreenToBase:point];
NSLog(@"Location? x= %f, y = %f", (float)point.x, (float)point.y);
NSLog(@"Location? x= %f, y = %f", (float)wp.x, (float)wp.y);
NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseUp location:CGPointMake(wp.x, wp.y) modifierFlags:0 timestamp:NSTimeIntervalSince1970 windowNumber:[_window windowNumber] context:nil eventNumber:0 clickCount:0 pressure:0.1];
//NSEvent *event = [NSEvent eventWithCGEvent:ourEvent];
NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];
[theMenu insertItemWithTitle:@"Item 1" action:@selector(beep:) keyEquivalent:@"" atIndex:0];
[theMenu insertItemWithTitle:@"Item 2" action:@selector(beep:) keyEquivalent:@"" atIndex:1];
[NSMenu popUpContextMenu:theMenu withEvent:event forView:nil];
}
Can someone please provide me sample code for the most natural way to get this done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在将石英与可可混合。这些 API 之间有很多重叠之处,如果您能坚持只使用一个,那就这么做吧。这样做:
Quartz 和 Cocoa 使用不同的坐标系:Quartz 使用“硬件地址”样式坐标,左上角为 (0, 0),Cocoa 使用“数学”样式坐标,左下角为 (0, 0)。
You are mixing Quartz with Cocoa. There is a lot of overlap between the APIs, and if you can stick with just one, do so. Do this instead:
Quartz and Cocoa use different coordinate systems: Quartz uses "hardware address" style coordinates with (0, 0) at the top left, Cocoa uses "mathematics" style coordinates with (0, 0) at the bottom left.
在 init: 中定义
,然后:
并在您的操作中使用它
Define in init:
and then:
and in your action use it