4

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);
tadman
  • 208,517
  • 23
  • 234
  • 262
Mr. Duan
  • 81
  • 1
  • 4

1 Answers1

7

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.

jvarela
  • 3,744
  • 1
  • 22
  • 43
  • Thanks for your answer. I get a CGWindowID and use it to capture image. So another question, how to get NSWindow from CGWindowID? – Mr. Duan Dec 02 '16 at 02:04
  • I will update my answer to explain how you can do that. – jvarela Dec 02 '16 at 19:52
  • NSWindow *window = [NSApplication windowWithWindowNumber:(NSInteger)windowID]; I try this function in my code, but it is not useful. Because the target window is in the other process. How to deal with this problem? – Mr. Duan Dec 05 '16 at 02:57
  • @Mr.Duan Check then this sample code from Apple: it should do what you want: https://developer.apple.com/library/content/samplecode/SonOfGrab/Introduction/Intro.html – jvarela Dec 07 '16 at 07:02
  • 1
    A good sample,but there are some differences from what I want to do. After captured the window, I want to know the position of the window on the display and draw it to an image with the same size of the display.So I need to know which display the window on. – Mr. Duan Dec 20 '16 at 07:56
  • @jvarela The [NSApplication windowWithWindowNumber:(NSInteger)windowID]; and CGWindowID windowID = you refer to, in [Determine which screen a window is on given the window ID](https://stackoverflow.com/questions/7850685/determine-which-screen-a-window-is-on-given-the-window-id) are two different things. Currently there is no way to correlate the two window ids, one provided by Cocoa (NS) and one provided by Core Graphics (CG) – StefanS Apr 07 '22 at 10:57