Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import win32gui
- import win32ui
- import win32con
- def screenshot(hwnd = None):
- from time import sleep
- if not hwnd:
- hwnd=win32gui.GetDesktopWindow()
- l,t,r,b=win32gui.GetWindowRect(hwnd)
- h=b-t
- w=r-l
- hDC = win32gui.GetWindowDC(hwnd)
- myDC=win32ui.CreateDCFromHandle(hDC)
- newDC=myDC.CreateCompatibleDC()
- myBitMap = win32ui.CreateBitmap()
- myBitMap.CreateCompatibleBitmap(myDC, w, h)
- newDC.SelectObject(myBitMap)
- win32gui.SetForegroundWindow(hwnd)
- sleep(.2) #lame way to allow screen to draw before taking shot
- newDC.BitBlt((0,0),(w, h) , myDC, (0,0), win32con.SRCCOPY)
- myBitMap.Paint(newDC)
- myBitMap.SaveBitmapFile(newDC,'last_screenshot.bmp')
- def _get_windows_bytitle(title_text, exact = False):
- def _window_callback(hwnd, all_windows):
- all_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
- windows = []
- win32gui.EnumWindows(_window_callback, windows)
- if exact:
- return [hwnd for hwnd, title in windows if title_text == title]
- else:
- return [hwnd for hwnd, title in windows if title_text in title]
- if __name__ == "__main__":
- # First, find the window handle for "MyApplication"
- hwnds = _get_windows_bytitle("firefox", exact=False)
- # Check if we found any windows
- if hwnds:
- # Take a screenshot of the first window we found
- screenshot(hwnds[0])
- print("Screenshot taken and saved to last_screenshot.bmp")
- else:
- print("No window found with the specified title.")
Advertisement
Add Comment
Please, Sign In to add comment