views:

18

answers:

1

I'm trying to get an image of an offscreen window to use in a CALayer-based animation, but no matter which method I try I cannot get an image out the other side. Here is the code I've been using on a custom NSWindow subclass in my project:

CGImageRef windowImage = CGWindowListCreateImage(CGRectNull, 
                                                 kCGWindowListOptionAll, 
                                                 (CGWindowID)[self windowNumber], 
                                                 kCGWindowImageDefault);

This is what the image should look like:

alt text

and here's what I get (ignore the slightly larger size - that's my fault in the rendering of the image):

alt text

Can anyone see where I'm going wrong? Strangely, Quartz Debug can see the window image without issue, but the Son of Grab sample code from Apple cannot.

+1  A: 

From the documentation comment for CGWindowListCreateImage in CGWindow.h:

kCGWindowListOptionAll, kCGWindowListOptionOnScreenOnly: Use all on-screen windows in this user session to construct the image. The parameter windowID should be kCGNullWindowID.

That's why you're getting a screenshot of all the windows that are within the rectangle you're interested in.

To take a screenshot of a specific window, use kCGWindowListOptionIncludingWindow.

Peter Hosey
Thanks Peter, I should have mentioned in my question that I tried that option as well to no avail. The interesting part of this is that the rect that has been captured is not the rect that the window is sitting in when it is onscreen/visible.
Tony Arnold