0

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.

  • Are you looking for a native Carbon/Cocoa API? – dirkgently Apr 21 '09 at 08:09
  • I'm looking for a way to implement that feature ... possibly setting up a window which is transparent and inserting a view that could respond to user interactions ... would this be the way to go? or is there a better solution? –  Apr 21 '09 at 08:42

2 Answers2

2

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
  • 77,985
  • 20
  • 184
  • 180
1

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
  • 43,532
  • 6
  • 101
  • 124