參考別人文章的
如下:
# coding=utf-8 __author__ = 'Administrator' __doc__ = ''' pythonwin中win32gui的用法 本文件演如何使用win32gui來遍歷系統中所有的頂層窗口, 並遍歷所有頂層窗口中的子窗口 ''' import win32gui from pprint import pprint def show_window_attr(hWnd): ''' 顯示窗口的屬性 :return: ''' if not hWnd: return # 中文系統默認title是gb2312的編碼 title = win32gui.GetWindowText(hWnd)
#這里可能要把標題的gbk轉換成utf-8 ,暫時不管了,可以加個函數 clsname = win32gui.GetClassName(hWnd) # print('窗口句柄:%s ' % (hWnd)) print('窗口類名:%s' % (clsname)) # str = "Chrome_WidgetWin_1" # if str == clsname: # fd = win32gui.FindWindow(clsname, None) # 查找窗口句柄 # win32gui.ShowWindow(fd, 1) # 隱藏窗口 print ('窗口句柄:%s ' % (hWnd)) print ('窗口標題:%s' % (title)) def show_windows(hWndList): for h in hWndList: show_window_attr(h) def demo_top_windows(): ''' 演示如何列出所有的頂級窗口 :return: ''' hWndList = [] win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList) show_windows(hWndList) return hWndList def demo_child_windows(parent): ''' 演示如何列出所有的子窗口 :return: ''' if not parent: return hWndChildList = [] win32gui.EnumChildWindows(parent, lambda hWnd, param: param.append(hWnd), hWndChildList) show_windows(hWndChildList) return hWndChildList hWndList = demo_top_windows() assert len(hWndList) parent = hWndList[20] # 這里系統的窗口好像不能直接遍歷,不知道是否是權限的問題 hWndChildList = demo_child_windows(parent) print('-----top windows-----') pprint(hWndList) print('-----sub windows:from %s------' % (parent)) pprint(hWndChildList)
