我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用win32gui.GetClassName()。
def _window_enum_dialog_callback(hwnd, extra): '''Call back func which checks each open window and matches the name of window using reg ex''' #self._handle = None matchtext = extra matchtext = matchtext.encode('utf-8') logging.debug("call _window_enum_dialog_callback") classname = win32gui.GetClassName(hwnd) title_text = win32gui.GetWindowText(hwnd) title_text = title_text.decode('gbk').encode('utf-8') if classname == '#32770': matchtext = matchtext.encode('utf-8') logging.debug("msg: " + matchtext) logging.debug("Title is: " + title_text) if (matchtext.strip() == title_text.strip()): logging.debug("!!!!BINGO!!!!") top_hwnd = hwnd logging.debug("Find the window at the top") return False else: logging.debug("No matched .....") return True
def foreach_child(self): def callback(hwnd, window_hwnd): classname = win32gui.GetClassName(hwnd).lower() buffer_len = win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0) + 1 text = array('b', b'\x00\x00' * buffer_len) text_len = win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, buffer_len, text) text = win32gui.PyGetString(text.buffer_info()[0], buffer_len - 1).lower() for match in self._windows[window_hwnd]['matches']: if match["text"] in text: self._windows[window_hwnd]['to_click'].append(match["button"]) if "button" in classname: self._windows[window_hwnd]['buttons'].append({ 'text': text, 'handle': hwnd, }) return True return callback
def dumpWindow(hwnd, wantedText=None, wantedClass=None): """ :param hwnd: ???? :param wantedText: ?????? :param wantedClass: ??????? :return: ?????????????? """ windows = [] hwndChild = None while True: hwndChild = win32gui.FindWindowEx(hwnd, hwndChild, wantedClass, wantedText) if hwndChild: textName = win32gui.GetWindowText(hwndChild) className = win32gui.GetClassName(hwndChild) windows.append((hwndChild, textName, className)) else: return windows
def _MyCallback( hwnd, extra ): hwnds, classes = extra hwnds.append(hwnd) classes[win32gui.GetClassName(hwnd)] = 1
def _windowEnumerationHandler(hwnd, resultList): """Pass to win32gui.EnumWindows() to generate list of window handle, window text, window class tuples.""" resultList.append((hwnd, win32gui.GetWindowText(hwnd), win32gui.GetClassName(hwnd)))
def GetClassName(cls, whd): return win32gui.GetClassName(whd)
def is_desktop_on_foreground(self): # pylint: disable=no-self-use """Detect "show desktop" or something close enough.""" foreground = win32gui.GetForegroundWindow() return bool( foreground and win32gui.GetClassName(foreground) == "WorkerW")
def _windowEnumerationHandler(hwnd, resultList): '''Pass to win32gui.EnumWindows() to generate list of window handle, window text, window class tuples.''' resultList.append((hwnd, win32gui.GetWindowText(hwnd), win32gui.GetClassName(hwnd)))
def Find_Gui_button_hwnd(self,str_app='´ò¿ª',control_class='Button',control_name='´ò¿ª(&O)'): time.sleep(1) result = False print "*********Find_Gui_button function**********" #self.Mousepos_print() #print 'control_name:',str_app,',',control_name hwnd = win32gui.FindWindow(None, str_app) if hwnd < 1: hwnd = self.find_main_window(str_app) comboHwnd = win32gui.FindWindowEx(hwnd,0,control_class,None) #print 'comboHwnd',comboHwnd while comboHwnd: #print "control_class:",comboHwnd,control_name cla = win32gui.GetClassName(comboHwnd) #print 'cla:' ,cla bufLen=1024 buf =win32gui.PyMakeBuffer(bufLen) if control_class in cla: n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf) str = buf[:n] #print 'buttonname ,len,n,find :',str,len(control_name ),n,str.find(control_name) if str.find(control_name)>-1: result = True win32gui.PostMessage(comboHwnd,win32con.WM_LBUTTONDOWN,0) time.sleep(0.05) #win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONUP,0) win32gui.PostMessage(comboHwnd,win32con.WM_LBUTTONUP,0) time.sleep(0.05) time.sleep(1) print 'find control_name',result break comboHwnd = win32gui.GetWindow(comboHwnd,win32con.GW_HWNDNEXT) if result ==False: comboHwnd = -1 return comboHwnd
def Find_Gui_button(self,str_app='´ò¿ª',control_class='Button',control_name='´ò¿ª(&O)'): time.sleep(1) result = False print "*********Find_Gui_button function**********" #self.Mousepos_print() #print 'control_name:',str_app,',',control_name hwnd = win32gui.FindWindow(None, str_app) if hwnd < 1: hwnd = self.find_main_window(str_app) #print 'hwnd :',hwnd,str_app win32gui.SetForegroundWindow(hwnd) comboHwnd = win32gui.FindWindowEx(hwnd,0,control_class,None) #print 'comboHwnd',comboHwnd while comboHwnd: print "control_class:",comboHwnd,control_name cla = win32gui.GetClassName(comboHwnd) print 'cla:' ,cla bufLen=1024 buf =win32gui.PyMakeBuffer(bufLen) if control_class in cla: n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf) str = buf[:n] print 'buttonname ,len,n,find :',str,len(control_name ),n,str.find(control_name) if (len(control_name ) ==0 and n==0) or ((len(control_name )>0 and str.find(control_name)>-1 )): #win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONDOWN,0) win32gui.PostMessage(comboHwnd,win32con.WM_LBUTTONDOWN,0) time.sleep(0.05) #win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONUP,0) win32gui.PostMessage(comboHwnd,win32con.WM_LBUTTONUP,0) time.sleep(0.05) #win32gui.SendMessage(win32con.WM_CLOSE,0,0) #print '111' result = True break comboHwnd = win32gui.GetWindow(comboHwnd,win32con.GW_HWNDNEXT) time.sleep(1) return result
def Find_Gui_edit(self,str_app='´ò¿ª',control_class='ComboBox',filename='wtp_cuo1.pcap',control_name='',stop_flag='0'): print "*********Find_Gui_edit function**********",str_app time.sleep(1) #self.Mousepos_print() print 'str_app',str_app hwnd = win32gui.FindWindow(None, str_app) print 'hwnd',hwnd win32gui.SetForegroundWindow(hwnd) comboHwnd = win32gui.FindWindowEx(hwnd,0,control_class,None) bufLen=256 buf =win32gui.PyMakeBuffer(bufLen) while comboHwnd: if stop_flag=='1': win32gui.SendMessage(comboHwnd,win32con.WM_SETTEXT,bufLen,filename) time.sleep(1) break print "control_class:",comboHwnd cla = win32gui.GetClassName(comboHwnd) print cla if control_class in cla: n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf) str = buf[:n] if (len(control_name ) ==0 and n==0) or (len(control_name )>0 and str.find(control_name)>-1 ): win32gui.SendMessage(comboHwnd,win32con.WM_SETTEXT,bufLen,filename) time.sleep(1) break comboHwnd = win32gui.GetWindow(comboHwnd,win32con.GW_HWNDNEXT) time.sleep(1) return True
def _window_enum_dialog_callback_(self, hwnd, extra): '''Call back func which checks each open window and matches the name of window using reg ex''' #self._handle = None matchtext = extra logging.debug("call _window_enum_dialog_callback") classname = win32gui.GetClassName(hwnd) title_text = win32gui.GetWindowText(hwnd) title_text = title_text.decode('gbk').encode('utf-8') if classname == '#32770': matchtext = matchtext.encode('utf-8') logging.debug("msg: " + matchtext) logging.debug("Title is: " + title_text) # buf_size = 1 + win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0) # buffer_text = win32gui.PyMakeBuffer(buf_size) # win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, buf_size, buffer_text) # # logging.debug("Buffer_text: " + buffer_text) # logging.debug("Buffer_text decode(gbk).encode(utf-8): " + # buffer_text.decode('gbk').encode('utf-8')) # windowText = buffer_text[:buf_size] # # try: # windowText = windowText.decode('gbk').encode('utf-8') #unicode(windowText, 'utf-8') # except: # logging.debug("_window_enum_dialog_callback unicode exception") # pass # # message = ['Handle:\t' + str(hwnd), # 'Class Name:\t' + classname, # 'Window Text:\t' + windowText] # # logging.debug("Print the message: " + str(message)) # # #if re.match(wildcard, windowText) != None: if (matchtext.strip() == title_text.strip()): logging.debug("!!!!BINGO!!!!") self.hwnd = hwnd return False else: logging.debug("No matched .....") return True
def expect_the_specific_dialog(self, _current_dialog): ''' Set the windows white list, Then find another :_current_dialog: :return: the new top dialog ''' def _expect_window_dialog_enum_callback(hwnd, extra): '''Call back func which checks each open window and matches the name of window using reg ex''' #self._handle = None matchtext = extra logging.debug("call _window_enum_dialog_callback") classname = win32gui.GetClassName(hwnd) title_text = win32gui.GetWindowText(hwnd) title_text = title_text.decode('gbk').encode('utf-8') if classname == '#32770': matchtext = matchtext.encode('utf-8') logging.debug("msg: " + matchtext) logging.debug("Title is: " + title_text) if (matchtext.strip() == title_text.strip()): logging.debug("!!!!Second window BINGO!!!!") if hwnd not in self.white_windows_list: logging.debug("Find the second window at the top") self.expect_sec_window = hwnd return False else: logging.debug("Find the window at the top which is not the second") return True else: logging.debug("No matched .....") return True self.white_windows_list = [_current_dialog, ] windowtitle = win32gui.GetWindowText(_current_dialog) logging.debug("To find the second window, need match " + windowtitle) try: #win32gui.EnumWindows(_expect_window_dialog_enum_callback, windowtitle) win32gui.EnumChildWindows(self.hwnd, _expect_window_dialog_enum_callback, windowtitle) except: logging.debug("Got the error:") logging.debug("win32gui.EnumWindows with " + str(_expect_window_dialog_enum_callback))
def Find_Gui_Tree(self,str_app='New Scenario',control_class='Button',filename='test_baidu',control_name='AfxOleControl'): print "*********Find_Gui_Tree function**********" time.sleep(1) #self.Mousepos_print() print 'str_app',str_app hwnd = win32gui.FindWindow(None, str_app) if hwnd < 1: hwnd = self.find_main_window(str_app) print 'hwnd',hwnd win32gui.SetForegroundWindow(hwnd) print 'hwnd :',hwnd,str_app win32gui.SetForegroundWindow(hwnd) #self.Mousepos_print('5') comboHwnd = win32gui.FindWindowEx(hwnd,0,control_class,None) print '111111111111comboHwnd',comboHwnd while comboHwnd: print "control_class:",comboHwnd cla = win32gui.GetClassName(comboHwnd) print "control_class_NAME:",cla bufLen=1024 buf =win32gui.PyMakeBuffer(bufLen) n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf) str = buf[:n] print str ''' if control_class in cla: n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf) str = buf[:n] print str if (len(control_name ) ==0 and n==0) or ((len(control_name )>0 and str.find(control_name)>-1 )): win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONDOWN,0) time.sleep(0.05) win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONUP,0) time.sleep(0.05) win32gui.SendMessage(win32con.WM_CLOSE,0,0) break ''' comboHwnd = win32gui.GetWindow(comboHwnd,win32con.GW_HWNDNEXT) time.sleep(1) return True
def get_hwnd_by_title_class(self, class_text = None, title_text= None, parent_title = None,parent_class = None): """ Returns a windows window_handler Args: title_text (string): the title of the window we are looking for SPECIAL CASE: if "desktop:n" is given, a handle to the desktop number n handle is given Returns: int: the handler for the window if found Raises: win32.error: If the windowtitle is invalid """ if 'desktop:' in title_text.lower(): _ , num = title_text.lower().split(':',1) num = int(num) monitors = win32api.EnumDisplayMonitors() tar_mon = monitors[num] self.hwnd = tar_mon[1] return self.hwnd if title_text.lower() == "desktop": self.hwnd = win32gui.GetDesktopWindow() return self.hwnd child_hwnd = [] def child_enumerator(hwnd,param): child_hwnd.append(hwnd) return True if parent_title is not None or parent_class is not None: logging.debug("Where supplied title/class: {0}/{1}".format(str(title_text), str(class_text))) parent_hwnd = self.get_hwnd_by_title_class(class_text=parent_class,title_text=parent_title) win32gui.EnumChildWindows(parent_hwnd,child_enumerator,None) for hwnd in child_hwnd: hwnd_title = win32gui.GetWindowText(hwnd) hwnd_class = win32gui.GetClassName(hwnd) if (hwnd_title == title_text and title_text is not None) or \ (hwnd_class == class_text and class_text is not None): self.hwnd = hwnd return hwnd # logging.debug("Found parent with title/class {0}{1} at {2}".format(parent_title,parent_class,parent_hwnd)) # self.hwnd = win32gui.FindWindowEx(parent_hwnd,0,class_text,title_text) else: logging.debug("Where supplied title/class: {0}/{1}".format(str(title_text), str(class_text))) self.hwnd = win32gui.FindWindow(class_text, title_text) if self.hwnd == 0: raise ValueError('Unable to find a window with that title or class') logging.debug("Found window 0x{:2X}".format(self.hwnd)) return self.hwnd