我一直在研究 Son Of Grab Apple 演示,我真的很难获得没有任何窗口(只有桌面、扩展坞和菜单栏)的屏幕截图。有谁知道该怎么做?
下面是一些示例代码,仅使用桌面进行屏幕截图。
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)
和更大。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)