"""Click a point in WeChat and grab any transient popup window that appears.""" import ctypes import sys import time import win32con import win32gui import win32process import win32ui from PIL import Image H = 197792 PID = 10800 def click(px, py): left, top, _, _ = win32gui.GetWindowRect(H) cx, cy = win32gui.ClientToScreen(H, (0, 0)) ox, oy = cx - left, cy - top x, y = px - ox, py - oy lp = (y << 16) | (x & 0xFFFF) u = ctypes.windll.user32 u.PostMessageW(H, win32con.WM_MOUSEMOVE, 0, lp) time.sleep(0.04) u.PostMessageW(H, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lp) time.sleep(0.04) u.PostMessageW(H, win32con.WM_LBUTTONUP, 0, lp) def grab(hwnd, path): left, top, right, bottom = win32gui.GetWindowRect(hwnd) w, h = right - left, bottom - top if w <= 0 or h <= 0: return False hwnd_dc = win32gui.GetWindowDC(hwnd) mfc_dc = win32ui.CreateDCFromHandle(hwnd_dc) save_dc = mfc_dc.CreateCompatibleDC() bmp = win32ui.CreateBitmap() bmp.CreateCompatibleBitmap(mfc_dc, w, h) save_dc.SelectObject(bmp) ok = ctypes.windll.user32.PrintWindow(hwnd, save_dc.GetSafeHdc(), 2) info = bmp.GetInfo() img = Image.frombuffer('RGB', (info['bmWidth'], info['bmHeight']), bmp.GetBitmapBits(True), 'raw', 'BGRX', 0, 1) if ok: img.save(path) mfc_dc.DeleteDC() save_dc.DeleteDC() win32gui.ReleaseDC(hwnd, hwnd_dc) return bool(ok) def popups(): found = [] def cb(h, _): try: _, pid = win32process.GetWindowThreadProcessId(h) if pid == PID and win32gui.IsWindowVisible(h): cls = win32gui.GetClassName(h) if cls == 'Qt51514QWindowToolSaveBits': found.append(h) except Exception: pass return True win32gui.EnumWindows(cb, None) return found if __name__ == '__main__': px, py = int(sys.argv[1]), int(sys.argv[2]) click(px, py) for delay in (0.15, 0.3, 0.6): time.sleep(delay) hits = popups() print('delay', delay, 'popups', hits, [win32gui.GetWindowRect(h) for h in hits]) for i, h in enumerate(hits): p = rf'C:\Users\GuoAn\AppData\Local\Temp\wx_popup_{i}.png' print('grab', h, grab(h, p), p) if hits: break