/
ilya.petrash
/
SimplePyScripts
Обзор
Документация
Войти
/
ilya.petrash
/
SimplePyScripts
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
winapi__screenshot_window.py
56 строк
1 KB
gil9red
Refactoring. Using black code style
18 июн 2023, 16:21
18 июн 2023, 16:21
06fa13f
Код
Авторство
О чём код?
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = "ipetrash" # SOURCE: https://stackoverflow.com/a/24352388/5909792 import win32gui import win32ui from ctypes import windll from PIL import Image hwnd = win32gui.FindWindow(None, "Telegram") # Change the line below depending on whether you want the whole window # or just the client area. # left, top, right, bot = win32gui.GetClientRect(hwnd) left, top, right, bot = win32gui.GetWindowRect(hwnd) w = right - left h = bot - top hwndDC = win32gui.GetWindowDC(hwnd) mfcDC = win32ui.CreateDCFromHandle(hwndDC) saveDC = mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) saveDC.SelectObject(saveBitMap) # Change the line below depending on whether you want the whole window # or just the client area. # result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 1) result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0) print(result) bmpinfo = saveBitMap.GetInfo() bmpstr = saveBitMap.GetBitmapBits(True) im = Image.frombuffer( "RGB", (bmpinfo["bmWidth"], bmpinfo["bmHeight"]), bmpstr, "raw", "BGRX", 0, 1 ) win32gui.DeleteObject(saveBitMap.GetHandle()) saveDC.DeleteDC() mfcDC.DeleteDC() win32gui.ReleaseDC(hwnd, hwndDC) if result == 1: # PrintWindow Succeeded im.save("test.png")