windows 在 python 中截取一个应用程序,不管它前面是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1080719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
screenshot an application in python, regardless of what's in front of it
提问by
So I can use PIL to grab a screenshot of the desktop, and then use pywin32 to get its rectangle and crop out the part I want. However, if there's something in front of the window I want, it'll occlude the application I wanted a screenshot of. Is there any way to get what windows would say an application is currently displaying? It has that data somewhere, even when other windows are in front of it.
所以我可以使用PIL来抓取桌面的截图,然后使用pywin32来获取它的矩形并裁剪出我想要的部分。但是,如果窗口前面有我想要的东西,它会遮挡我想要截图的应用程序。有什么方法可以让窗口说应用程序当前正在显示什么?它在某处拥有该数据,即使它前面有其他窗口。
回答by Kiv
I've done the same thing by giving the application I want focus before taking the shot:
我做了同样的事情,在拍摄之前给应用程序我想要的焦点:
shell=win32com.client.Dispatch("Wscript.Shell")
success = shell.AppActivate(app_name) # Returns true if focus given successfully.
回答by Claudiu
There is a way to do this, using the PrintWindow
function. It causes the window to redraw itself on another surface.
有一种方法可以使用该PrintWindow
函数来做到这一点。它会导致窗口在另一个表面上重绘自身。
回答by hazzey
I posted working code in the answer here: Python Screenshot of inactive window. It uses the PrintWindow
function.
This is the "correct" way to copy the image of a hidden window, but that doesn't mean that it will always work. It depends on the program implementing the proper message, WM_Print
response.
我在这里的答案中发布了工作代码:非活动窗口的 Python 屏幕截图。它使用该PrintWindow
功能。这是复制隐藏窗口图像的“正确”方法,但这并不意味着它总是有效。这取决于实现正确消息、WM_Print
响应的程序。
If you try to use PrintWindow
it doesn't work, then your only remaining option would be to bring the window to the front before taking a BitBlit
.
如果您尝试使用PrintWindow
它不起作用,那么您唯一剩下的选择就是在使用BitBlit
.
回答by mpen
If you can, try saving the order of the windows, then move your app to the front, screenshot, and move it back really quickly. Might produce a bit of annoying flicker, but it might be better than nothing.
如果可以,请尝试保存窗口的顺序,然后将您的应用程序移到最前面,截屏,然后非常快速地将其移回。可能会产生一些烦人的闪烁,但总比没有好。
回答by Luke Stanley
Maybe you can position the app offscreen, then take the screenshot, then put it back?
也许您可以将应用程序放置在屏幕外,然后截取屏幕截图,然后再放回去?
回答by Lucas Jones
IIRC, not in Windows pre-vista - with Aero each window has it's own buffer, but before that, you would just use your method, of getting the rectangle. I'm not sure if pywin32 has Aero support or not.
IIRC,而不是在 Windows pre-vista 中 - 对于 Aero,每个窗口都有自己的缓冲区,但在此之前,您只需使用您的方法来获取矩形。我不确定 pywin32 是否支持 Aero。