如何在Mac OSX上的窗口客户端内获得真正的鼠标位置?

时间:2017-04-06 16:16:36

标签: c++ objective-c macos

当我使用此代码时,我有一个错误的Y鼠标位置值。返回值为(x,y + 20)。

int Height = int [[Window contentView] frame].size.height;

int x = (int)[(NSEvent *)event locationInWindow].x;

int y = Height - (int)[(NSEvent *)event locationInWindow].y;

我认为高度是窗口的高度:`标题栏+矩形客户端。我想要高度的真正价值;

1 个答案:

答案 0 :(得分:1)

您可以将坐标转换为视图。

   - (void)mouseMoved:(NSEvent *)event
    {
        NSPoint locationInView = [self convertPoint:[event locationInWindow]
                                           fromView:yourView];
    }

确保获得某种事件(在我的示例中为鼠标事件)。确保它们已启用。

[window setAcceptsMouseMovedEvents:YES];
相关问题