views:

123

answers:

2

If you press cmd+shift+4 in Mac OS X you are able to select an area on your screen with the corrosponding coordinates shown. I need to implement such a function in one of my applications and have no idea of how to do it. Could anyone give me some advices on that?

Thx.

+1  A: 

This code fragment will return a CGImageRef that contains everything shown on the desktop for a given rectangle. It requires the ApplicationServices framework. The screen coordinates are flipped and the origin is at the top-left corner of the screen. In this case, the image ref would be owned by the caller and would need to be released with CGImageRelease when the caller was finished with it.

#import <ApplicationServices/ApplicationServices.h>

CGImageRef createScreenCapture(CGRect rect) {
  CGImageRef image = CGWindowCreateImage(
                       rect,
                       kCGWindowListOptionOnScreenOnly,
                       0,
                       kCGWindowImageDefault);
  return image;
}
Jason Coco
10.5-only, it's worth noting.
Chuck
@Chuck: Thanks, definitely worth mentioning!
Jason Coco
A: 

Typically this is done with an translucent overlay window which covers the entire desktop space.

Apple's got some older sample code which should give you a start.

Nicholas Riley
That's how I've done it now ... :)