Choosing an area on a Mac OS X Desktop

397 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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.

2
On

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;
}