"""Pictures of the handover window without showing it and without doing anything: for whoever wants to look at it before it is released, and for the tests. NX2navi.exe --selftest --shots DIR through the stub of an installed launcher pythonw start.pyw --selftest --shots DIR directly NX2NAVI_HANDOVER_TEST=DIR the same from the environment (a test that starts the stub) The window opens far off the screen, each state is drawn and saved as handover-.png (the window as Windows draws it, title bar included), state.json says what was found on this PC, and the process ends with exit code 0. Started through the environment it first stays until it is eight seconds old, longer than the stub waits for a failed start. No button is pressed, nothing is started, downloaded or written outside DIR. """ import ctypes import json import os import struct import time import zlib from pathlib import Path import handover BORN = time.time() def write_png(path, w, h, rgb): def chunk(kind, data): return struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data)) rows = b''.join(b'\0' + rgb[y * w * 3:(y + 1) * w * 3] for y in range(h)) Path(path).write_bytes(b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', w, h, 8, 2, 0, 0, 0)) + chunk(b'IDAT', zlib.compress(rows, 6)) + chunk(b'IEND', b'')) def snap(win, path): """The window as Windows draws it (PrintWindow: it need not be on the screen). False where that is not possible.""" if os.name != 'nt': return False from ctypes import wintypes user32, gdi32 = ctypes.WinDLL('user32'), ctypes.WinDLL('gdi32') P = ctypes.c_void_p user32.GetParent.restype, user32.GetParent.argtypes = P, [P] user32.GetWindowRect.argtypes = [P, ctypes.POINTER(wintypes.RECT)] user32.GetWindowDC.restype, user32.GetWindowDC.argtypes = P, [P] user32.ReleaseDC.argtypes = [P, P] user32.PrintWindow.argtypes = [P, P, ctypes.c_uint] gdi32.CreateCompatibleDC.restype, gdi32.CreateCompatibleDC.argtypes = P, [P] gdi32.CreateCompatibleBitmap.restype, gdi32.CreateCompatibleBitmap.argtypes = P, [P, ctypes.c_int, ctypes.c_int] gdi32.SelectObject.restype, gdi32.SelectObject.argtypes = P, [P, P] gdi32.GetDIBits.argtypes = [P, P, ctypes.c_uint, ctypes.c_uint, P, P, ctypes.c_uint] gdi32.DeleteObject.argtypes = [P] gdi32.DeleteDC.argtypes = [P] hwnd = user32.GetParent(win.winfo_id()) or win.winfo_id() r = wintypes.RECT() user32.GetWindowRect(hwnd, ctypes.byref(r)) w, h = r.right - r.left, r.bottom - r.top if w <= 0 or h <= 0: return False dc = user32.GetWindowDC(hwnd) mem = gdi32.CreateCompatibleDC(dc) bmp = gdi32.CreateCompatibleBitmap(dc, w, h) old = gdi32.SelectObject(mem, bmp) try: user32.PrintWindow(hwnd, mem, 2) # PW_RENDERFULLCONTENT header = struct.pack('