开发者

How to emulate the cmd-shift-4 screen capture interface?

开发者 https://www.devze.com 2023-03-11 05:59 出处:网络
I am building an OSX app and would like to capture a portion of the screen with a similar interface as the cmd-shift-4 UX, IE initiating the action:

I am building an OSX app and would like to capture a portion of the screen with a similar interface as the cmd-shift-4 UX, IE initiating the action:

  1. presents a mouse cursor
  2. mouseDown and drag, mouseUp captures the screen coordinates while drawing a translucent overlay to denote the area being captured

I've found a good starting point in http://code.google.com/p/captureme/ which presents an NSPanel and captures开发者_如何转开发 the area within the panel. However I'd like do a bit better and emulate the cmd-shift-4 interface.

I can't find if something like this is built in (or if there's an existing solution). I suppose the straight forward way is to use an NSResponder and replace the cursor on mouseDown, then on a mouse drag event draw a translucent rectangle between the mouseDown point and the current cursor location. Let me know if my thinking is along the correct lines.

Thanks!


Here's the nutshell version of what you want to do: Create a fullscreen transparent window at CGShieldingWindowLevel(), setIgnoresMouseEvents:NO, have the view set the cursor to whatever you want, and implement dragging to draw the marquee in the dragged area.


You can use screencapture command-line tool. This will put your screenshot to the pasteboard:

NSTask* task = [[NSTask alloc] init];
[task setArguments: [NSArray arrayWithObject: @"-ic"]];
[task setLaunchPath: @"/usr/sbin/screencapture"];
[task launch];
[task waitUntilExit];
[task release];
NSData* data = [[NSPasteboard generalPasteboard] dataForType: NSPasteboardTypePNG];

Also, you can setup task to save your screenshot directly to a file:

[task setArguments: [NSArray arrayWithObjects: @"-i", filePath, nil]];
0

精彩评论

暂无评论...
验证码 换一张
取 消