Guest User

Untitled

a guest
Feb 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. long windowId = kCGNullWindowID;
  2. CGRect *temp = new CGRect;
  3.  
  4. // get all windows on the current screen
  5. CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionAll | kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
  6.  
  7. // find the current window, that will be the first one on layer 0
  8. CFIndex windowNum = CFArrayGetCount(windowList);
  9. for (int i=0;i<(int)windowNum;i++) {
  10. CFDictionaryRef info = (CFDictionaryRef)CFArrayGetValueAtIndex(windowList, i);
  11.  
  12. CFNumberRef currentWindowLayer = (CFNumberRef)CFDictionaryGetValue(info, kCGWindowLayer);
  13.  
  14. long layer = LONG_MAX;
  15. CFNumberGetValue(currentWindowLayer, kCFNumberLongType, &layer);
  16.  
  17. if (layer > 0)
  18. continue;
  19.  
  20. // get window title (not necessary, but as reference, don't forget to free the string)
  21. CFStringRef currentTitle = (CFStringRef)CFDictionaryGetValue(info, kCGWindowName);
  22. char* bla = MYCFStringCopyUTF8String(currentTitle);
  23.  
  24. // get the window number
  25. CFNumberRef currentWindowNumber = (CFNumberRef)CFDictionaryGetValue(info, kCGWindowNumber);
  26. CFNumberGetValue(currentWindowNumber, kCFNumberLongType, &windowId);
  27.  
  28. // get the window rect
  29. CFDictionaryRef bounds = (CFDictionaryRef)CFDictionaryGetValue(info, kCGWindowBounds);
  30. CGRectMakeWithDictionaryRepresentation(bounds, temp);
  31.  
  32. // break on the first window, that will be the current
  33. break;
  34. }
  35.  
  36. CFStringRef filename = CFSTR("/tmp/test.gif");
  37.  
  38. CFURLRef urlRef = CFURLCreateWithFileSystemPath( kCFAllocatorDefault, filename, kCFURLPOSIXPathStyle, false );
  39.  
  40. // create new destination, for an animated gif, change the count variable
  41. int count = 1;
  42.  
  43. CGImageDestinationRef idst = CGImageDestinationCreateWithURL( urlRef, kUTTypeGIF, count, NULL );
  44.  
  45. // set the delay in case of animated gif
  46. float delay = 1.0 / (1e6/1e2);
  47. CFNumberRef delayNumber = CFNumberCreate(NULL, kCFNumberFloatType, &delay);
  48. const void *keys[1] = { kCGImagePropertyGIFDelayTime };
  49. const void *values[1] = { delayNumber };
  50. CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 3,
  51. &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  52.  
  53. // create the screenshot of windowId with rect temp
  54. CGImageRef screenShot = CGWindowListCreateImage(*temp, kCGWindowListOptionIncludingWindow, windowId, kCGWindowImageDefault);
  55.  
  56. // add screenshot to the destination
  57. CGImageDestinationAddImage( idst, screenShot, options );
  58.  
  59. // release screenshot
  60. CGImageRelease(screenShot);
  61.  
  62. // finalize the animated gif
  63. CGImageDestinationFinalize( idst );
Add Comment
Please, Sign In to add comment