#coding =utf-8__author__ = ' Administrator ' __doc__ = ' pythonwin Win32gui Usage This file shows how to use Win32gui to traverse all the top-level windows in the system, and iterate through all the sub-windows in the top-level window Description: 1. The original message sends all calls to the SendMessage, thinking that the optimization guru internally is independent of threading, but not, so avoid waiting and use PostMessage2 automatically. ' #优化大师窗口信息YHDS_WINDOW_TITTLE = ' wopti Utilities ' yhds_window_clsname = ' twomccmainform ' #常量CONST_YJYH = ' one key optimization ' Const_ YJQL = ' One key cleanup ' Const_dialog_class = ' #32770 ' const_static_class = ' STATIC ' #设置DEBUG = Falseimport sysimport win32guiimport wi N32conimport timefrom pprint Import pprintfrom datetime import Datetimedef Gbk2utf8 (s): Return S.decode (' GBK '). Encode (' Utf-8 ') def get_window_attr (hWnd): #中文系统默认title是gb2312的编码 title = Win32gui. GetWindowText (hWnd) title = Gbk2utf8 (title) Clsname = Win32gui. GetClassName (HWND) return TITLE,CLSNAMEDEF Get_dialog_text (hwnd): #获取对话框的内容信息 childs = Get_child_windows (hwnd) For h in childs:r_title, R_name = get_window_attr (h) if r_name! = Const_static_class:continue R_title = R_title.strip () If Len (r_title) > 0:return r_title return ' Def get_top_dialogs (): ' Gets all the top-level dialogs: return: ' HW Ndlist = get_top_windows () dialogs = [] for h in Hwndlist: _, Clsname = Get_window_attr (h) if clsname = = CONST_DIALOG_CLASS:dialogs.append (h) return dialogsdef Show_dialog (): "Displays all dialog form:p Aram hwn D:: Return: "' Show_windows (Get_top_dialogs ()) def show_window_attr (hWnd): ' Display properties of window: return: ' If not hwnd:return Title,clsname = get_window_attr (HWND) print ' Window handle:%s '% (HWND) print ' Window caption:%s '% (t itle) print ' window class name:%s '% (clsname) print ' Def show_windows (hwndlist): For h in Hwndlist:show_window_attr ( h) def get_top_windows (): Hwndlist = [] Win32gui. EnumWindows (Lambda hWnd, Param:param.append (hwnd), hwndlist) return Hwndlistdef demo_top_windows (): "' shows how to list all Top-level window: return: ' Hwndlist = Get_top_windows () show_windows (hwndlist) return Hwndlistdef get_child_windows (parent): Hwndchildlist = [] Win32gui. Enumchildwindows (parent, Lambda hwnd, Param:param.append (HWND), hwndchildlist) return hwndchildlistdef Demo_child_win Dows (parent): "Shows how to list all child windows: return:" If not parent:return hwndchildlist = Get_child_windo WS (Parent) show_windows (hwndchildlist) return hwndchildlistdef Find_window (hwnd,clsname = none, title = none): If Not Clsname and is Title:return False def notnonecompare (L, R): If not L:return True return L = = R R_title,r_clsname = get_window_attr (hWnd) return Notnonecompare (Clsname, r_clsname) and Notnonecompa Re (title, r_title) def find_window_from_list (hwndlist, clsname = none, title = none): For h in Hwndlist:if Find_ Window (h, Clsname, title): Return Hdef Demo_win32gui (): Hwndlist = Demo_top_windows () Assert Len (hwndlist Parent = hwndlist[20] #这里系统的窗口好像不能直接遍历, do not know whether it is a permission issue HWNDCHILDList = demo_child_windows (parent) print ('-----top windows-----') pprint (hwndlist) print ('-----sub Windows:from%s- -----'% (parent) Pprint (hwndchildlist) def wait_get_top_window (clsname = none, title = none): If not clsname and no T title: #无效参数 return while true:hwndlist = Get_top_windows () hWnd = Find_window_from_list (Hwndlist, Clsname= Clsname, title= title) print ('---searching---%s '% (DateTime.Now ())) if Hwnd:ret Urn hWnd #每间隔两秒钟查询一次 time.sleep (2) def get_window_by_title_filter (Hwndlist, Namefilter): for h in Hwndlist : r_title, R_name = get_window_attr (h) #标题中包含过滤器的任何一项, is considered to be found for F-in Namefilter:if F-in R _title:return hdef Autoyhds (): #x坐标和y坐标分别是, coordinates within the window def click (hWnd, X, y): #低位是x, High is Y LP Aram = y << 15 | X Win32gui. PostMessage (HWnd, Win32con. Wm_lbuttondown, LParam) Win32gui. PostMessage (HWnd, Win32con. Wm_lbuttonuP, LParam) def get_center_pos (hWnd): L, T, r, B = Win32gui. GetWindowRect (hwnd) x = (r-l)/2 y = (b-t)/2 return x, y def Click_window (hwnd): X, y = Get_center_pos (HWND) Click (hwnd, X, y) def CLICK_AND_DO (hwnd, const_condition, work_name= ' work ", work = None) : Print (' Start%s ... '% (work_name)) Click_window (hWnd) #等待任务执行完成 while True:time.slee P (2) R_title, _ = get_window_attr (hWnd) if r_title = = Const_condition and Win32gui. IsWindowEnabled (hWnd): Print ('%s: Complete '% (work_name)) ' Return print ('%s, please wait ... '% (Work_name)) If Work:work () def onekey_optimize (hwnd): Click_and_do (hwnd, CONST_YJYH, CONST_YJYH) def get_ Ok_and_cancel (hwnd): Childs = Get_child_windows (hwnd) OK = get_window_by_title_filter (childs, [' OK ', ' yes ']) Cancel = Get_window_by_title_filter (childs, [' Cancel ', ' no ']) asSert OK and cancel return OK, cancel Def onekey_clean_work (): Dialogs = Get_top_dialogs () filter = [' Windows Optimizer master ', ' Delete multiple items '] dialog = Get_window_by_title_filter (dialogs, Filter) If dialog: #根据对话框 Type to click OK or cancel OK, Cancel = get_ok_and_cancel (dialog) Text = get_dialog_text (dialog) if ' register Table Backup ' in Text:click_window (cancel) Else:click_window (OK) def onekey_clean (hwn d): Click_and_do (HWnd, CONST_YJQL, CONST_YJQL, Onekey_clean_work) "Print (' Start a key cleanup ... ') #点击开始 Perform a one-click Cleanup Operation Click_window (hWnd) time.sleep (2) #等待优化大师分析, waiting for the analysis result dialog = Wait_get_top_window (Clsnam E=const_dialog_class, title= ' Windows Optimizer master ') Ok, Cancel = Get_ok_and_cancel (DIALOG) #点击确定删除扫描到的文件和文件夹 C Lick_window (OK) time.sleep (2) #确定将所有的内容移动到垃圾箱 dialog = Wait_get_top_window (clsname=const_dialog_class , title= ' delete multiple items ') OK, Cancel = get_ok_and_cancel (dialog) assert OK and cancel Click_window (OK) print (' Finish one key cleanup ') "Def debug (): #dialog = Get_onekey_clean_yhds () #print dialog #show_dialog () dialogs = g Et_top_dialogs () filter = [' Windows Optimizer master ', ' Delete multiple items '] dialog = Get_window_by_title_filter (dialogs, filter) Print Get_dialog_text (Dialog) Sys.exit () if Debug:debug () hwndlist = Get_top_windows () Yhds = Find_window_from_list (Hwndlist, Yhds_window_clsname, yhds_window_tittle) if not yhds:print (' Optimization master application not started ') Sys.exit (0) childs = get_child_windows (YHDS) yjyh = Find_window_from_list (Childs, None, const_yjyh) yjql = find _window_from_list (Childs, None, CONST_YJQL) if not yjyh or not yjql:print (' No optimization button found ') sys.exit (0) Onek Ey_optimize (YJYH) Onekey_clean (YJQL) sys.exit (0) Reload (SYS) sys.setdefaultencoding (' Utf-8 ') Autoyhds ()
Python-written automation to handle optimization gurus