在没有窗口的情况下进行屏幕截图 [英] Take ScreenShot without Window

查看:41
本文介绍了在没有窗口的情况下进行屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究 Son Of Grab Apple 演示,我真的很难获得没有任何窗口(只有桌面、停靠栏和菜单栏)的屏幕截图.有人知道怎么做吗?

I've been studying Son Of Grab Apple demo and I'm really struggling to get a screenshot of screen without any window (only desktop, dock and menu bar). Does anyone knows how to do so?

推荐答案

下面是一些示例代码,它只在桌面上截取屏幕截图.

Here's some example code which takes a screenshot with just the desktop.

CFArrayRef onScreenWindows = CGWindowListCreate(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
CFArrayRef nonDesktopElements = CGWindowListCreate(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
CFRange range = CFRangeMake(0, CFArrayGetCount(nonDesktopElements));
CFMutableArrayRef desktopElements = CFArrayCreateMutableCopy(NULL, 0, onScreenWindows);
for (int i = CFArrayGetCount(desktopElements) - 1; i >= 0; i--)
{
    CGWindowID window = (CGWindowID)(uintptr_t)CFArrayGetValueAtIndex(desktopElements, i);
    if (CFArrayContainsValue(nonDesktopElements, range, (void*)(uintptr_t)window))
        CFArrayRemoveValueAtIndex(desktopElements, i);
}

CGImageRef cgimage = CGWindowListCreateImageFromArray(CGRectInfinite, desktopElements, kCGWindowListOptionAll);
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithCGImage:cgimage];
NSData* data = [rep representationUsingType:NSPNGFileType properties:[NSDictionary dictionary]];
[data writeToFile:@"/tmp/foo.png" atomically:YES];

您应该能够扩展一般方法来抓取 Dock 和菜单栏.您构建一个您感兴趣的窗口 ID 列表,然后调用 CGWindowListCreateImageFromArray().我通过询问所有屏幕窗口然后所有屏幕窗口(不包括桌面元素)来计算桌面元素的窗口 ID.桌面元素是那些在第一个列表中但不在第二个列表中的元素.

You should be able to extend the general approach to grab the Dock and menu bar. You construct a list of window IDs that you're interested in and then call CGWindowListCreateImageFromArray(). I compute the window IDs of the desktop elements by asking for all on-screen windows and then all on-screen windows excluding the desktop elements. The desktop elements are those in the first list which aren't in the second list.

获取菜单栏和 Dock 的窗口 ID 并不那么直接,因为 CGWindowList API 中没有直接对应的选项.您需要使用 CGWindowListCopyWindowInfo()CGWindowListCreateDescriptionFromArray() 获取屏幕窗口的描述字典数组并检查内容.最有用的键可能是 kCGWindowLayer.除了使用我的示例代码中的技术获得的桌面元素之外,我认为您还需要 CGWindowLevelForKey(kCGDockWindowLevelKey) 和更高版本的任何东西.

Getting the window IDs for the menu bar and Dock is not quite as direct as that because there's no option in the CGWindowList API that directly corresponds to that. You'll need to get the array of description dictionaries for the on-screen windows using either CGWindowListCopyWindowInfo() or CGWindowListCreateDescriptionFromArray() and examine the contents. The most useful key will probably be kCGWindowLayer. In addition to the desktop elements obtained using the technique in my sample code, I think you'll want anything at CGWindowLevelForKey(kCGDockWindowLevelKey) and greater.

这篇关于在没有窗口的情况下进行屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆