python句柄部分操作
python句柄部分操作
python句柄部分操作
1 ```python3 2 # 通过窗口类名、窗口标题获取控件句柄 3 hwnd = win32gui.FindWindow("ClassName", "TitleName") 4 # 通过句柄设置控件内容 5 win32gui.SendMessage(hwnd , 12, 0, '文本内容') 6 # 修改窗口标题 7 SetWindowText(hwnd, "Title") 8 #通过句柄获取子窗口句柄 9 ch_hwnd = win32gui.FindWindowEx(hwnd, 0, "ClassName", "TitleName") 10 11 # 获取所有子窗口 12 hwndChildList = [] 13 win32gui.EnumChildWindows(hwnd, lambda hwnd, param: param.append(hwnd), hwndChildList) 14 print(hwndChildList) 15 # 模糊查询句柄 16 def FindWinHwnd(title, top=True): 17 titles = [] 18 19 def foo(hwnd, mouse): 20 if top: 21 if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd): 22 if title in GetWindowText(hwnd): 23 titles.append(hwnd) 24 else: 25 if title in GetWindowText(hwnd): 26 titles.append(hwnd) 27 28 EnumWindows(foo, 0) 29 if titles: 30 return titles[0] 31 else: 32 return 0 33 34 # 截图 35 def window_api_capture(file, hWnd=0): 36 if hWnd == 0: 37 MoniterDev = win32api.EnumDisplayMonitors(None, None) 38 width = MoniterDev[0][2][2] 39 height = MoniterDev[0][2][3] 40 else: 41 left, top, right, bot = win32gui.GetWindowRect(hWnd) 42 width = right - left 43 height = bot - top 44 # 返回句柄窗口的设备环境,覆盖整个窗口,包括非客户区,标题栏,菜单,边框 45 hWndDC = win32gui.GetWindowDC(hWnd) 46 # 创建设备描述表 47 mfcDC = win32ui.CreateDCFromHandle(hWndDC) 48 # 创建内存设备描述表 49 saveDC = mfcDC.CreateCompatibleDC() 50 # 创建位图对象准备保存图片 51 saveBitMap = win32ui.CreateBitmap() 52 # 为bitmap开辟存储空间 53 saveBitMap.CreateCompatibleBitmap(mfcDC, width, height) 54 # 将截图保存到saveBitMap中 55 saveDC.SelectObject(saveBitMap) 56 # 保存bitmap到内存设备描述表 57 saveDC.BitBlt((0, 0), (width, height), mfcDC, (0, 0), win32con.SRCCOPY) 58 try: 59 saveBitMap.SaveBitmapFile(saveDC, file) 60 except: 61 pass 62 win32gui.DeleteObject(saveBitMap.GetHandle()) 63 saveDC.DeleteDC() 64 mfcDC.DeleteDC() 65 win32gui.ReleaseDC(hWnd, hWndDC)