我一直在研究 Son Of Grab Apple 演示,我真的很难获得没有任何窗口的屏幕截图(只有桌面、停靠栏和菜单栏)。有谁知道该怎么做?
问问题
1308 次
1 回答
4
这是一些示例代码,它仅使用桌面截取屏幕截图。
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。桌面元素是第一个列表中不在第二个列表中的元素。
获取菜单栏和 Dock 的窗口 ID 并没有那么直接,因为 CGWindowList API 中没有直接对应的选项。您需要使用CGWindowListCopyWindowInfo()
or获取屏幕窗口的描述字典数组CGWindowListCreateDescriptionFromArray()
并检查内容。最有用的键可能是kCGWindowLayer
. 除了在我的示例代码中使用该技术获得的桌面元素之外,我认为您会想要CGWindowLevelForKey(kCGDockWindowLevelKey)
更多的东西。
于 2013-02-02T22:22:39.130 回答