I'm trying to understand the win32 api / pywin32 package, and I have code that takes a screenshot of a window with a particular name. I'm having a hard time understanding the win32 api, and the current code effectively searches for the same window two different times. I'm assuming it's possible to do better. Here's the code:
1 - Preliminaries:
from PIL import Image
import win32ui, win32gui, win32con
#name of window to screenshot
name = "Photos"
2- The first time I find the window, I am able to use the result to get information about its shape and location:
window = win32ui.FindWindow(None, name)
left, top, right, bottom = window.GetWindowRect()
width = right - left
height = bottom - top
3 - The second time I find the window, I'm able to feed the result into the GetWindowDC
function. The remainder of my code (not shown) uses this device context information to perform the screenshot.
def findit(hwnd,ctx):
if win32gui.GetWindowText(hwnd) == name: # check the title
return hwnd
window = win32gui.EnumWindows(findit,None)
#convert this into a device context handle
windowDC = win32gui.GetWindowDC(window)
I'd like to know if there is a way to reproduce left, top, right, bottom
and windowDC
without using two different methods to find the window I want. In addition, I'd like to get an intuition for what data is being contained in window
the first time I compute it vs the second. I'm more interested in this understanding than actually taking the screenshot, so code that does that job in a completely different way probably won't be helpful.