我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用win32gui.EnumChildWindows()。
def Save(): try: win32gui.PostMessage(win32gui.FindWindowEx(win32gui.FindWindow('#32770','???'),None,'Button','??(&S)'),win32con.BM_CLICK,0,0) time.sleep(1) global handle handle = None win32gui.EnumChildWindows(win32gui.FindWindow('#32770','?????'),handle_window,'?(&Y)') if handle!= None: win32gui.PostMessage(handle,win32con.BM_CLICK,0,0) time.sleep(3) except Exception as e: ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e))
def attach(self, device): if self.device is not None: print "Warning: already attached to a device." if device is not self.device: self.detach() handle = device.hwnd def callback(hwnd, extra): extra.add(hwnd) return True self.watched_hwnds.add(handle) try: # EnumChildWindows may crash for windows have no any child. # refs: https://mail.python.org/pipermail/python-win32/2005-March/003042.html win32gui.EnumChildWindows(handle, callback, self.watched_hwnds) except pywintypes.error: pass self.device = device print "attach to device", device
def dumpWindows(hwnd): """Dump all controls from a window Useful during development, allowing to you discover the structure of the contents of a window, showing the text and class of all contained controls. Parameters ---------- hwnd The window handle of the top level window to dump. Returns ------- all windows Usage example:: replaceDialog = findTopWindow(wantedText='Replace') pprint.pprint(dumpWindow(replaceDialog)) """ windows = [] win32gui.EnumChildWindows(hwnd, _windowEnumerationHandler, windows) return windows
def foreach_window(self): def callback(hwnd, lparam): title = win32gui.GetWindowText(hwnd).lower() for window in self.to_close: if window in title: win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) print "Closed window ({})".format(title) for window in self.clicks: if window in title: self._windows[hwnd] = { "matches": self.clicks[window], "to_click": [], "buttons": [] } try: win32gui.EnumChildWindows(hwnd, self.foreach_child(), hwnd) except: print "EnumChildWindows failed, moving on." for button_toclick in self._windows[hwnd]['to_click']: for button in self._windows[hwnd]['buttons']: if button_toclick in button['text']: win32gui.SetForegroundWindow(button['handle']) win32gui.SendMessage(button['handle'], win32con.BM_CLICK, 0, 0) print "Clicked on button ({} / {})".format(title, button['text']) del self._windows[hwnd] return True return callback
def EnumChildWindows(cls, parent_whd): if not parent_whd: return whd_child_list = [] win32gui.EnumChildWindows(parent_whd, lambda hWnd, param: param.append(hWnd), whd_child_list) return whd_child_list
def dumpWindow(hwnd): '''Dump all controls from a window into a nested list Useful during development, allowing to you discover the structure of the contents of a window, showing the text and class of all contained controls. Arguments: The window handle of the top level window to dump. Returns A nested list of controls. Each entry consists of the control's hwnd, its text, its class, and its sub-controls, if any. Usage example: replaceDialog = findTopWindow(wantedText='Replace') pprint.pprint(dumpWindow(replaceDialog)) ''' windows = [] try: win32gui.EnumChildWindows(hwnd, _windowEnumerationHandler, windows) except win32gui.error: # No child windows return windows = [list(window) for window in windows] for window in windows: childHwnd, windowText, windowClass = window window_content = dumpWindow(childHwnd) if window_content: window.append(window_content) return windows
def find_main_window(self,starttxt): print "*********find_main_window function**********" global MAIN_HWND MAIN_HWND = 0 win32gui.EnumChildWindows(0, self.is_win_ok, starttxt) return MAIN_HWND
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 get_expect_window_label_text(self, _expect_sec_window): ''' Try to get window label text ''' # label_text = u"????" # label_text = label_text.encode('utf-8') # return label_text label_text = '' window = self.expect_sec_window child_control = None last_child = 0 while True: logging.debug("Find the child controls for parent window") child_control = win32gui.FindWindowEx(window, last_child, 'static', None) if not child_control: logging.debug("The child is None") break; else: logging.debug("The child is not None, ") buffer = win32gui.PyMakeBuffer(200) length = win32gui.SendMessage(child_control, win32con.WM_GETTEXT, 200, buffer) result = buffer[:length] result = result.decode('gbk').encode('utf-8') logging.debug("Got the child text is :" + result) last_child = child_control label_text = result time.sleep(0.5) def _winfun(hwnd, lparam): s = win32gui.GetWindowText(hwnd) s = s.decode('gbk').encode('utf-8') logging.debug("winfun, child_hwnd: %d txt: %s" % (hwnd, s)) return 1 if window: logging.debug("To enumerate all the child windows") win32gui.EnumChildWindows(self.expect_sec_window, _winfun, None) #bufferlength = struct.pack('i', 255) #count = win32gui.SendMessage(self.expect_sec_window, win32con.get, 0, 0) #for itemIndex in range(count): # value = array.array('c', bufferlength +str().ljust(253)) # valueLength = win32gui.SendMessage(self.expect_sec_window, getValueMessage, itemIndex, value) # yield value.tostring()[:valueLength] return label_text
def findControls(topHwnd, wantedText=None, wantedClass=None, selectionFunction=None): '''Find controls. You can identify controls using captions, classes, a custom selection function, or any combination of these. (Multiple selection criteria are ANDed. If this isn't what's wanted, use a selection function.) Arguments: topHwnd The window handle of the top level window in which the required controls reside. wantedText Text which the required controls' captions must contain. wantedClass Class to which the required controls must belong. selectionFunction Control selection function. Reference to a function should be passed here. The function should take hwnd as an argument, and should return True when passed the hwnd of a desired control. Returns: The window handles of the controls matching the supplied selection criteria. Usage example: optDialog = findTopWindow(wantedText="Options") def findButtons(hwnd, windowText, windowClass): return windowClass == "Button" buttons = findControl(optDialog, wantedText="Button") ''' def searchChildWindows(currentHwnd): results = [] childWindows = [] try: win32gui.EnumChildWindows(currentHwnd, _windowEnumerationHandler, childWindows) except win32gui.error: # This seems to mean that the control *cannot* have child windows, # i.e. not a container. return for childHwnd, windowText, windowClass in childWindows: descendentMatchingHwnds = searchChildWindows(childHwnd) if descendentMatchingHwnds: results += descendentMatchingHwnds if wantedText and \ not _normaliseText(wantedText) in _normaliseText(windowText): continue if wantedClass and \ not windowClass == wantedClass: continue if selectionFunction and \ not selectionFunction(childHwnd): continue results.append(childHwnd) return results return searchChildWindows(topHwnd)
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