是否可以捕获 NSCursor 窗口的图像?这是我的代码
获取光标窗口的原始值
let level = CGWindowLevelForKey(.CursorWindowLevelKey).int
我有一个方法可以使用 CGWindowListCopyWindowInfo 遍历屏幕上的所有窗口并返回“窗口服务器”拥有的光标窗口的窗口编号
let number = self.windowNumber(level, screen: screen, options: nil) //returns proper window number
let frame = screen.frame //screen is the current NSScreen that the mouse is in.
let cgimage = CGWindowListCreateImage(frame, .OptionIncludingWindow, CGWindowID(number), .Default)!
转换为 NSBitmapImageRep
let cursor = NSBitmapImageRep(CGImage: cgimage)
然后将光标的屏幕截图保存到磁盘。
这适用于所有其他窗口,除了保存空白窗口的光标。我也尝试过使用一堆不同类型的框架,但没有成功。我认为这是因为 CGWindowListCreateImage 自动省略了光标窗口。是否有可能阻止它忽略它?也许复制窗口并捕获它?
我尝试过的其他事情
- 使用获取图像
NSCursor.currentCursor().image
但因为我的应用程序是仅后台应用程序,我被迫使用NSCursor.currentSystemCursor().image
它总是返回非视网膜图像。 - 弄清楚哪个
NSCursor.currentSystemCursor().image
正在返回并尝试获取视网膜版本......只是无法比较真正带回哪个实例。