如何在鼠标光标处显示 NSMenu?

发布于 2024-12-21 19:57:44 字数 1238 浏览 1 评论 0原文

我正在开发一个应用程序,我想在当前鼠标位置旁边显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拍不死你 2024-12-28 19:57:44

您正在将石英与可可混合。这些 API 之间有很多重叠之处,如果您能坚持只使用一个,那就这么做吧。这样做:

NSPoint point = [NSEvent mouseLocation];

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:

NSPoint point = [NSEvent mouseLocation];

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.

甜心小果奶 2024-12-28 19:57:44

在 init: 中定义

NSPoint lastClick;

,然后:

- (void) rightMouseDown:(NSEvent *)theEvent {
    lastClick = [self convertPoint: theEvent.locationInWindow fromView: nil];
    [super rightMouseDown:theEvent];
}

并在您的操作中使用它

- (IBAction)someAction:(id)sender {
    //Use lastCLick.x and lastClick.y
}

Define in init:

NSPoint lastClick;

and then:

- (void) rightMouseDown:(NSEvent *)theEvent {
    lastClick = [self convertPoint: theEvent.locationInWindow fromView: nil];
    [super rightMouseDown:theEvent];
}

and in your action use it

- (IBAction)someAction:(id)sender {
    //Use lastCLick.x and lastClick.y
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文