How do I convert Cocoa co-ords from top left == origin to bottom left == origin

风流意气都作罢 提交于 2019-12-08 17:16:52

问题


I use CGWindowListCopyWindowInfo to get a list of all windows. It gives me the co-ordinates of each window based upon the origin being the top-left of the screen.

If I use NSWindow's setFrame method, the co-ordinates on based upon the origin being the bottom-left of the screen.

What's a clean, reliable way to convert from one to the other?

Added: By clean and reliable, I mean, something sure to work regardless whether the user has multiple screens or is using Spaces. I figure there must be a known idiom using library APIs.


回答1:


Math is quite reliable :-)

yFromBottom = screenHeight - windowHeight - yFromTop

Main screen height is

[[[NSScreen screens] objectAtIndex:0] frame].size.height



回答2:


I would suggest using an NSAffineTransform. If you draw with respect to the default origin and then apply a transform to the view, you can essentially flip things around in one fell swoop.




回答3:


Try something like this (from here):

NSRect boundsInWindow = [myView convertRect:[myView bounds] toView:nil];
NSRect visibleRectInWindow = [myView convertRect:[myView visibleRect] toView:nil];

// Flip Y to convert NSWindow coordinates to top-left-based window coordinates.
float borderViewHeight = [[myView window] frame].size.height;
boundsInWindow.origin.y = borderViewHeight - NSMaxY(boundsInWindow);
visibleRectInWindow.origin.y = borderViewHeight - NSMaxY(visibleRectInWindow);


来源:https://stackoverflow.com/questions/852875/how-do-i-convert-cocoa-co-ords-from-top-left-origin-to-bottom-left-origin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!