screenshot an application in python, regardless of

2019-05-08 03:51发布

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.

6条回答
等我变得足够好
2楼-- · 2019-05-08 04:27

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.

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.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-05-08 04:27

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.

查看更多
4楼-- · 2019-05-08 04:36

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.
查看更多
仙女界的扛把子
5楼-- · 2019-05-08 04:40

Maybe you can position the app offscreen, then take the screenshot, then put it back?

查看更多
等我变得足够好
6楼-- · 2019-05-08 04:49

There is a way to do this, using the PrintWindow function. It causes the window to redraw itself on another surface.

查看更多
Root(大扎)
7楼-- · 2019-05-08 04:50

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.

查看更多
登录 后发表回答