[Mac OS]打开了哪个窗口?
我有一个窗口的CGWindowID和Mac的所有CGDirectDisplayID. 然后,我想知道显示在哪个窗口上. 我尝试获取Window的CGWindowInfo,但是找不到有用的信息.
I have the CGWindowID of one window and all CGDirectDisplayIDs of my Mac. Then I want to know which display the window on. I try to get the CGWindowInfo of the Window,but can`t find useful informations.
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionIncludingWindow, windowID);
CFArrayApplyFunction(windowList, CFRangeMake(0, CFArrayGetCount(windowList)), &WindowListApplierFunction, this);
CFRelease(windowList);
您可以为此使用NSScreen API.使用[NSScreen screens]
检索计算机连接到的屏幕,然后匹配[myWindow screen]
返回的屏幕.
You can use the NSScreen API for that. Use [NSScreen screens]
to retrieve the screens your computer is connected to and then match the screen returned by [myWindow screen]
.
如果您拥有要在哪个屏幕上知道的窗口,请执行以下操作:
If you own the window that you want know on which screen is at just do the following:
CGWindowID windowID = ... // init with your value
NSWindow *window = [NSApplication windowWithWindowNumber:(NSInteger)windowID];
if ([[NSScreen screens] count] > 1)
{
// you have more than one screen attached
NSScreen *currentScreen = [window screen];
// you can then test if the window is in the main display
if (currentScreen == [NSScreen mainScreen])
{
// your window is on the main screen
}
else
{
// your window is not on the main screen
}
}
但是,如果您不拥有该窗口,因为它是另一个应用程序所拥有的,那么我建议您首先了解NSScreen使用的Quartz协调系统和CGWindow API使用的Core Image坐标系统之间的区别.这里有一篇很好的文章(英文):
However, if you don't own the window, because it is owned by another app, then I suggest that you first understand the differences between the Quartz coordinating system used by NSScreen and the Core Image coordinate system used by the CGWindow API. There is a good article about this here (English):
http://www.thinkandbuild.it/deal-with-多屏编程/
和此处(日语)(如果您不懂日语,请使用Google翻译):
and here (Japanese) (use Google translator if you don't know Japanese):
http://xcatsan.blogspot.com/2009/09/nsscreen -cgwindow.html
第二,您需要按照我推荐的Son of Grab示例代码的说明或此处的说明来检索窗口边界:
Second, you need to retrieve the window bounds as explained by Son of Grab sample code I recommended or as explained here:
然后,您需要按照建议的窗口边界计算屏幕所在的位置.
Then you need to calculate the screen where it is at by the window bounds as suggested.