我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用win32gui.EnumWindows()。
def get_hwnds(pid): """return a list of window handlers based on it process id""" def callback(hwnd, hwnds): if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd): _, found_pid = win32process.GetWindowThreadProcessId(hwnd) if found_pid == pid: hwnds.append(hwnd) return True hwnds = [] win32gui.EnumWindows(callback, hwnds) return hwnds
def __init__(self, window_name=None, exe_file=None, exclude_border=True): hwnd = 0 # first check window_name if window_name is not None: hwnd = win32gui.FindWindow(None, window_name) if hwnd == 0: def callback(h, extra): if window_name in win32gui.GetWindowText(h): extra.append(h) return True extra = [] win32gui.EnumWindows(callback, extra) if extra: hwnd = extra[0] if hwnd == 0: raise WindowsAppNotFoundError("Windows Application <%s> not found!" % window_name) # check exe_file by checking all processes current running. elif exe_file is not None: pid = find_process_id(exe_file) if pid is not None: def callback(h, extra): if win32gui.IsWindowVisible(h) and win32gui.IsWindowEnabled(h): _, p = win32process.GetWindowThreadProcessId(h) if p == pid: extra.append(h) return True return True extra = [] win32gui.EnumWindows(callback, extra) #TODO: get main window from all windows. if extra: hwnd = extra[0] if hwnd == 0: raise WindowsAppNotFoundError("Windows Application <%s> is not running!" % exe_file) # if window_name & exe_file both are None, use the screen. if hwnd == 0: hwnd = win32gui.GetDesktopWindow() self.hwnd = hwnd self.exclude_border = exclude_border
def find_window_wildcard(self, wildcard): """ This function takes a string as input and calls EnumWindows to enumerate through all open windows """ self._handle = None win32gui.EnumWindows(self._window_enum_callback, wildcard)
def find_dialog_wildcard(self, wildcard): ''' Enumerate all the dialog to find the dialog which title matches the title''' #extra = (self._handle, wildcard) try: win32gui.EnumWindows(_window_enum_dialog_callback, wildcard) except: logging.debug("Got the error:") logging.debug("win32gui.EnumWindows with " + str(_window_enum_dialog_callback)) pass self.hwnd = top_hwnd
def kill(self, gracePeriod=5000): """ Kill process. Try for an orderly shutdown via WM_CLOSE. If still running after gracePeriod (5 sec. default), terminate. """ win32gui.EnumWindows(self.__close__, 0) if self.wait(gracePeriod) != win32event.WAIT_OBJECT_0: win32process.TerminateProcess(self.hProcess, 0) win32api.Sleep(100) # wait for resources to be released
def __close__(self, hwnd, dummy): """ EnumWindows callback - sends WM_CLOSE to any window owned by this process. """ TId, PId = win32process.GetWindowThreadProcessId(hwnd) if PId == self.PId: win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
def TestEnumWindows(): windows = [] classes = {} win32gui.EnumWindows(_MyCallback, (windows, classes)) print "Enumerated a total of %d windows with %d classes" % (len(windows),len(classes)) if "tooltips_class32" not in classes: print "Hrmmmm - I'm very surprised to not find a 'tooltips_class32' class."
def TestEnumWindows(): windows = [] classes = {} win32gui.EnumWindows(_MyCallback, (windows, classes)) print("Enumerated a total of %d windows with %d classes" % (len(windows),len(classes))) if "tooltips_class32" not in classes: print("Hrmmmm - I'm very surprised to not find a 'tooltips_class32' class.")
def get_all_windows(): """ Generates list of the hwnd of all 'real' windows. Returns: (bool): List of hwnd of real windows. """ def call(hwnd, param): """ The callback function to be used by EnumWindows. Appends all hwnds to param list """ if is_real_window(hwnd): param.append(hwnd) winds = [] wg.EnumWindows(call, winds) return winds
def run(self): while self.should_run(): win32gui.EnumWindows(self.foreach_window(), 0) self._stop.wait(0.5)
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 GetHWndByProcId(cls, procid): def callback(hwnd, procinfo): pid = procinfo.get("procid", None) t, pid_2 = win32process.GetWindowThreadProcessId(hwnd) #print pid,"==", find_pid if pid == pid_2: p_hwnd = win32gui.GetParent(hwnd) if p_hwnd == 0: # top window procinfo["hwnd"] = hwnd return True procinfo = { "procid": procid, "hwnd": None, } win32gui.EnumWindows(callback, procinfo) return procinfo["hwnd"]
def window_resize(): win32gui.EnumWindows(enumHandler, None)
def find_window_wildcard(self, wildcard): """ This function takes a string as input and calls EnumWindows to enumerate through all open windows """ self._handle = None win32gui.EnumWindows(self._window_enum_callback, wildcard) #??????
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 findTopWindows(wantedText=None, wantedClass=None, selectionFunction=None): '''Find the hwnd of top level windows. You can identify windows 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: wantedText Text which required windows' captions must contain. wantedClass Class to which required windows must belong. selectionFunction Window 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 window. Returns: A list containing the window handles of all top level windows matching the supplied selection criteria. Usage example: optDialogs = findTopWindows(wantedText="Options") ''' results = [] topWindows = [] win32gui.EnumWindows(_windowEnumerationHandler, topWindows) for hwnd, windowText, windowClass in topWindows: if wantedText and not _normaliseText(wantedText) in _normaliseText(windowText): continue if wantedClass and not windowClass == wantedClass: continue if selectionFunction and not selectionFunction(hwnd): continue results.append(hwnd) return results
def findTopWindows(self,wantedText=None, wantedClass=None, selectionFunction=None): '''Find the hwnd of top level windows. You can identify windows 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: wantedText Text which required windows' captions must contain. wantedClass Class to which required windows must belong. selectionFunction Window 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 window. Returns: A list containing the window handles of all top level windows matching the supplied selection criteria. Usage example: optDialogs = findTopWindows(wantedText="Options") ''' results = [] topWindows = [] win32gui.EnumWindows(_windowEnumerationHandler, topWindows) for hwnd, windowText, windowClass in topWindows: if wantedText and not _normaliseText(wantedText) in _normaliseText(windowText): continue if wantedClass and not windowClass == wantedClass: continue if selectionFunction and not selectionFunction(hwnd): continue results.append(hwnd) return results
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))