Reputation: 81
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);
Upvotes: 4
Views: 2339
Reputation: 3847
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 to 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 on the main display
if (currentScreen == [NSScreen mainScreen])
{
// your window is on the main screen
}
else
{
// your window is not on the main screen
}
}
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 coordinate system used by NSScreen
and the Core Graphics coordinate system used by the CGWindow API. There is a good article about this here (English):
http://www.thinkandbuild.it/deal-with-multiple-screens-programming/
and here (Japanese) (use Google translator if you don't know Japanese):
http://xcatsan.blogspot.com/2009/09/nsscreen-cgwindow.html
Second, you need to retrieve the window bounds as explained by Son of Grab sample code I recommended or as explained here:
Determine which screen a window is on given the window ID
Then you need to calculate the screen where it is at by the window bounds as suggested.
Upvotes: 9