Python Notes32:pywin32获取窗口句柄

Python Notes32:pywin32获取窗口句柄

 

用到的库

import sys import win32gui import win32con

显示窗口属性

1. 获取所有窗口的句柄

def get_all_windows(): hWnd_list = [] win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWnd_list) print(hWnd_list) return hWnd_list
>>[66364, 66014, 65964, 65854, 65850, 65846, 65842, 65826, 65812, 65802, 65858, 65862, 65876, 65794, ....]

2.获取窗口的子窗口句柄

def get_son_windows(parent): hWnd_child_list = [] win32gui.EnumChildWindows(parent, lambda hWnd, param: param.append(hWnd), hWnd_child_list) print(hWnd_child_list) return hWnd_child_list
>>[66364, 66014, 65964, 65854, ...]

3. 获取句柄的标题

def get_title(hwnd): title = win32gui.GetWindowText(hwnd) print('窗口标题:%s' % (title)) return title
>>窗口标题:设置

4. 获取窗口类名

def get_clasname(hwnd): clasname = win32gui.GetClassName(hwnd) print('窗口类名:%s' % (clasname)) return clasname
>>窗口类名:ApplicationFrameWindow

其他

1. 置顶和取消

窗口置顶

def set_top(hwnd): win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOACTIVATE | win32con.SWP_NOOWNERZORDER | win32con.SWP_SHOWWINDOW | win32con.SWP_NOSIZE)

窗口取消置顶

def set_down(hwnd): win32gui.SetWindowPos(hwnd, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_SHOWWINDOW | win32con.SWP_NOSIZE | win32con.SWP_NOMOVE)

2. 显示和隐藏窗口

根据窗口名称获取句柄

def get_hwnd_from_name(name): hWnd_list = get_all_windows() for hwd in hWnd_list: title = get_title(hwd) if title == name: return hwd

窗口显示

def xianshi(name): hwd = get_hwnd_from_name(name) win32gui.ShowWindow(hwd, win32con.SW_SHOW)

窗口隐藏

def yingcang(name): hwd = get_hwnd_from_name(name) win32gui.ShowWindow(hwd, win32con.SW_HIDE)

显示和隐藏托盘

获取右下角托盘的任务句柄

def get_tuopan_hwd(): handle = win32gui.FindWindow("Shell_TrayWnd", None) hWnd_child_list = get_son_windows(handle)[1:] tuopan_hwd_list = [] flag = False for i in hWnd_child_list: if get_clasname(i) == 'TrayNotifyWnd': flag = True if flag: tuopan_hwd_list.append(i) return tuopan_hwd_list

隐藏托盘

def yingcang(name=''): tuopan_hwd_list = get_tuopan_hwd() if name == '': for i in tuopan_hwd_list[:7] : win32gui.ShowWindow(i, win32con.SW_HIDE) else: win32gui.ShowWindow(name, win32con.SW_HIDE)

显示托盘

def xianshi(name = ''): tuopan_hwd_list = get_tuopan_hwd() if name == '': for i in tuopan_hwd_list: win32gui.ShowWindow(i, win32con.SW_SHOW) else: win32gui.ShowWindow(name, win32con.SW_SHOW)
 
 

Demo

import win32gui hwnd_title = dict() #创建字典保存窗口的句柄与名称映射关系 hwnd_class = dict() hwnd_class_title = dict() def get_all_hwnd(hwnd, mouse): if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd): # hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)}) # hwnd_class.update({hwnd: win32gui.GetClassName(hwnd)}) hwnd_class_title.update({win32gui.GetClassName(hwnd):win32gui.GetWindowText(hwnd)}) win32gui.EnumWindows(get_all_hwnd, 0) # 窗口类型 --- 窗口名 # print(hwnd_class_title) for h, t in hwnd_class_title.items(): if t!= "": print(h, "---",t) # for h, t in hwnd_title.items(): # if t!= "": # print(h, t) # for h, t in hwnd_class.items(): # if t!= "": # print(h, t) from PyQt6.QtWidgets import QApplication from PyQt6.QtGui import * import win32gui import sys # hwnd = win32gui.FindWindow("TaskManagerWindow", '任务管理器') hwnd = win32gui.FindWindow("WeChatMainWndForPC", '微信') app = QApplication(sys.argv) screen = QApplication.primaryScreen() img = screen.grabWindow(hwnd).toImage() img.save("screenshot.jpg")
  • 有个小问题是,有的画面部分不显示