Mac OS X:在NSView坐标和全局屏幕坐标之间转换 [英] Mac OS X: Convert between NSView coordinates and global screen coordinates

查看:599
本文介绍了Mac OS X:在NSView坐标和全局屏幕坐标之间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多显示器设置中,我遇到以下情况:

I have the following situation in my multi-monitor setup:

在此示例中,我想将窗口精确定位在黄色箭头所示的坐标处.但是,我所要做的只是一个NSView的坐标,它是一个NSWindow的contentView的子视图,该子视图跨越了整个(更大,更大)的辅助监视器.

In this example I want to position a window exactly at the coordinates depicted with the yellow arrow. All I do have however, are the coordinates of an NSView that is a subview of the contentView of an NSWindow that spans the entire (bigger,upper) secondary monitor.

以下是定义全局坐标空间的方式:

Here's how the global coordinate space is defined:

  • {0,0}是我的笔记本电脑屏幕左上角的坐标. (绿色)
  • {-296,-1080}是第二个屏幕(黑色)左上角的坐标
  • {0,800}是左下角的坐标(此处没有箭头)

因此y从绿色箭头向下增加,而从绿色箭头向上减少.

Thus y is increasing going down from green arrow, decreasing going up from green arrow.

问题:

如何将黄色箭头({100,100},此NSScreen内NSWindow内的NSView)描绘的点转换为该全局坐标系. (注意:在NSView中,坐标系的左下角有{0,0},向上增加.)

How do I convert the point depicted by the yellow arrow ({100,100}, NSView inside NSWindow inside this NSScreen) into that global coordinate system. (Note: In an NSView the coordinate system has {0,0} in the bottom left corner, increasing upwards.)

我相信正确的答案是{-196,-980},但是在任何屏幕上任何窗口进行此转换的代码是什么?

I believe the correct answer is {-196, -980}, but whats the code to get this conversion for any window on any screen?

我已经在这个问题上花费了太多时间,因此非常感谢您的帮助.

I have spent too much time on this problem already, so any help is very appreciated.

(不确定是否相关,但底部屏幕上显示视网膜分辨率.)

(Not sure if relevant, but the bottom screen has a retina resolution display.)

推荐答案

Mac OS在不同的地方使用不同的坐标系.视图可以定义它们的y轴是向上还是向下(isFlipped).窗口的原点用y轴向上的屏幕坐标"表示.屏幕使用y指向下的全局坐标系进行排列.

Mac OS uses different coordinate systems in various places. Views can define if they have an upwards or downwards pointing y-axis (isFlipped). A window's origin is expressed in "screen coordinates" with an upwards y-axis. Screens are arranged using the global coordinate system with y pointing down.

最好不要自己尝试转换所有坐标空间,而要让负责的对象来完成这项工作:

It's better not to try to do the conversion of all the coordinate spaces yourself but let the responsible objects do the job:

NSView *yellowView; // your view that contains the point with the yellow arrow.
NSPoint yellowPoint = { 100, 100 };

NSPoint pointInWindow = [yellowView convertPoint:yellowPoint toView:nil];
NSPoint pointOnScreen = [[yellowView window] convertRectToScreen:(CGRect){.origin=pointInWindow}];

NSWindow *newWindow = [[NSWindow alloc] initWithContentRect:(CGRect){ pointOnScreen, {32, 32}} styleMask: ...];

这篇关于Mac OS X:在NSView坐标和全局屏幕坐标之间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆