Guest User

ofGetGlobalMousePosition and ofGrabGlobalScreen

a guest
Feb 6th, 2011
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. ofPoint ofGetGlobalMousePosition() {
  2. #ifdef TARGET_OSX
  3.     Point mouse;
  4.     GetGlobalMouse(&mouse);
  5.     return ofPoint(mouse.h, mouse.v);
  6. #endif
  7. }
  8.  
  9. void ofGrabGlobalScreen(ofImage& img, ofRectangle& region) {
  10.     region.width = MAX((int) region.width, 1);
  11.     region.height = MAX((int) region.height, 1);
  12.  
  13. #ifdef TARGET_OSX
  14.     static int bytesPerPixel = 4;
  15.    
  16.     img.allocate(region.width, region.height, OF_IMAGE_COLOR_ALPHA);
  17.  
  18.     CGImageRef cgImage = CGWindowListCreateImage(
  19.         CGRectMake(region.x, region.y, region.width, region.height),
  20.         kCGWindowListOptionOnScreenOnly,
  21.         kCGNullWindowID,
  22.         kCGWindowImageDefault);
  23.    
  24.     CGContextRef spriteContext = CGBitmapContextCreate(
  25.         img.getPixels(), region.width, region.height,
  26.         CGImageGetBitsPerComponent(cgImage),
  27.         region.width * bytesPerPixel,
  28.         CGImageGetColorSpace(cgImage),
  29.         kCGImageAlphaPremultipliedLast);
  30.    
  31.     CGContextDrawImage(spriteContext, CGRectMake(0, 0, (CGFloat) region.width, (CGFloat) region.height), cgImage);
  32.  
  33.     CGContextRelease(spriteContext);   
  34.     CGImageRelease(cgImage);
  35. #endif
  36. }
Advertisement
Add Comment
Please, Sign In to add comment