def callback(hWnd, lParam): if win32gui.IsWindowVisible(hWnd): title = win32gui.GetWindowText(hWnd) if title: print(hex(hWnd), title) item = QListWidgetItem(title) item.setData(QtCore.Qt.UserRole, hWnd) item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable) item.setCheckState(QtCore.Qt.Unchecked) self.listWindow.addItem(item)
def listCallback(hwnd, ctx): if not win32gui.IsWindowVisible(hwnd) or not win32gui.IsWindowEnabled( hwnd): return childCtx = {} try: win32gui.EnumChildWindows(hwnd, listCallback, childCtx) except pywintypes.error: pass ctx[hwnd] = (childCtx, win32gui.GetWindowText(hwnd), win32gui.GetClassName(hwnd))
def callback(h, extra): try: if win32gui.IsWindowVisible(h) and win32gui.GetClassName( h) == "MENUEX": if "开始菜单" == win32gui.GetWindowText(h).decode( "gbk").encode("utf-8"): extra.append(h) except: print h return True
def find_ths_wnd(): hwnd_ths = 0 while True: hwnd_ths = win32gui.FindWindowEx(None, hwnd_ths, None, None) if hwnd_ths == 0: return False if win32gui.IsWindowVisible(hwnd_ths): win_text = win32gui.GetWindowText(hwnd_ths) if win_text[0:3] == '同花顺': return hwnd_ths return False
def __enum_handler(hwnd, *args): nonlocal handle _, p = win32process.GetWindowThreadProcessId(hwnd) if p == pid \ and win32gui.IsWindow(hwnd) \ and win32gui.GetClassName(hwnd) == '#32770' \ and win32gui.IsWindowVisible(hwnd) \ and win32gui.IsWindowEnabled(hwnd) \ and win32gui.GetWindowText(hwnd) in windows: handle = hwnd return
def update_window_info(): global __raid_hwnd global __window_info if win32gui.IsWindowVisible(__raid_hwnd): rect = win32gui.GetWindowRect(__raid_hwnd) x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y __window_info = Point(x, y, w, h) return __window_info
def winEnumHandler(hwnd, ctx): if win32gui.IsWindowVisible(hwnd): if "youtube" in ( win32gui.GetWindowText(hwnd).lower()): win32gui.ShowWindow(hwnd, SW_HIDE) global pid_process pid_process = win32process.GetWindowThreadProcessId( hwnd) return "ok" else: pass
def classify_window(hwnd): # Invisible windows are ignored until they become visible if not win32gui.IsWindowVisible(hwnd): return None, IgnoreTemporary, "Invisible" # Cloaked windows are not handled if pylewm.window.is_window_handle_cloaked(hwnd): return None, IgnoreTemporary, "Cloaked" style = win32api.GetWindowLong(hwnd, win32con.GWL_STYLE) exStyle = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) window = pylewm.window.Window(hwnd) # Windows with no title are temporary and should be ignored if window.window_title == '': return None, IgnoreTemporary, "Empty Title" # Windows with no title are temporary and should be ignored if window.window_title in ALWAYS_IGNORE_TITLES: return None, IgnoreTemporary, "Always Ignored by Name" # Check if any filters ignore this if pylewm.filters.is_ignored(window): return None, IgnorePermanent, "Ignored by Filter" # Don't bother with windows that don't overlap the desktop at all if not window.rect.overlaps(pylewm.monitors.DesktopArea): return None, IgnoreTemporary, "Off Screen" # Windows with 0 size are not managed if window.rect.height == 0 or window.rect.width == 0: return window, IgnorePermanent, "Zero Size" # Special always ignored classes window_class = window.window_class.lower() if window_class in ALWAYS_IGNORE_CLASSES: return window, IgnorePermanent, "Ignored Class" # NOACTIVATE windows that aren't APPWINDOW are ignored by # the taskbar, so we probably should ignore them as well if (exStyle & win32con.WS_EX_NOACTIVATE ) and not (exStyle & win32con.WS_EX_APPWINDOW): return window, IgnorePermanent, "Not AppWindow" # Windows that aren't resizable are ignored, # we can usually assume these aren't available for tiling. if not (style & win32con.WS_SIZEBOX): return window, Floating, "No Resize" # Some classes that Windows uses should always be realistically floating if window_class in ALWAYS_FLOATING_CLASSES: return window, Floating, "Floating Class" return window, Tiled, None
def callback(hwnd, extra): try: if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled( hwnd): children.append([ win32gui.GetWindowText(hwnd), win32gui.GetClassName(hwnd) ]) except: print(sys.exc_info()[0]) return True
def get_all_hwnd(hwnd, unused): global hwnds_list_of_taegets # 是现有窗口 & 启用了窗口 & 窗口具有WS_VISIBLE样式(非隐藏) if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled( hwnd) and win32gui.IsWindowVisible(hwnd): title = win32gui.GetWindowText(hwnd) # 加入列表 hwnds_list_of_taegets.append({ 'title': '* 无标题' if title == '' else title, # 窗口标题 'hwnd': hwnd # 句柄 ID })
def getWinCallBack(id, titles): ''' CallBack Function. @param titles get (id, title). ''' # is a window? is enabled? is visible? if win32gui.IsWindow(id) and win32gui.IsWindowEnabled( id) and win32gui.IsWindowVisible(id): title = win32gui.GetWindowText(id) if title: titles.append((id, title))
def close_all_dialog(self): hdlg = win32gui.FindWindowEx(0, 0, '#32770', None) while hdlg > 0: hdlg = win32gui.FindWindowEx(0, hdlg, '#32770', None) dlgthread, dlgprocessId = win32process.GetWindowThreadProcessId( hdlg) if win32gui.IsWindowVisible( hdlg ) and dlgprocessId == self.processId and self.threadId == dlgthread: win32gui.PostMessage(hdlg, win32con.WM_CLOSE, 0, 0) return 0
def find_windows(**kwargs): title = kwargs.get('title', None) class_name = kwargs.get('class_name', None) process = kwargs.get('process', None) parent = kwargs.get('parent', 0) top_level_only = kwargs.get('top_level_only', False) visible = kwargs.get('visible', None) enabled = kwargs.get('enabled', None) width = kwargs.get('width', None) encoding = kwargs.get('encoding', None) if isinstance(width, (int, float)): min_width = max_width = int(width) elif isinstance(width, (tuple, list)) and len(width) == 2: min_width, max_width = width min_width = math.floor(min_width) max_width = math.ceil(max_width) height = kwargs.get('height', None) if isinstance(height, (int, float)): min_height = max_height = int(height) elif isinstance(height, (tuple, list)) and len(height) == 2: min_height, max_height = height min_height = math.floor(min_height) max_height = math.ceil(max_height) tree_handles = get_tree_handles(parent, recursive=not top_level_only) handles = [] for handle in tree_handles: if isinstance(title, str): if encoding: if not GetWindowText(handle, encoding=encoding) == title: continue else: if not win32gui.GetWindowText(handle) == title: continue if process and not win32process.GetWindowThreadProcessId( handle)[-1] == process: continue if isinstance(class_name, str) and not win32gui.GetClassName(handle) == class_name: continue if visible is not None and not win32gui.IsWindowVisible( handle) == visible: continue if enabled is not None and not win32gui.IsWindowEnabled( handle) == enabled: continue if width or height: rect = win32gui.GetWindowRect(handle) if width and not min_width <= rect[2] - rect[0] <= max_width: continue if height and not min_height <= rect[3] - rect[1] <= max_height: continue handles.append(handle) return handles
def foucsWindowSwitchHandler(hwnd, names): if win32gui.IsWindowVisible(hwnd): if any(part in win32gui.GetWindowText(hwnd) for part in names): ##### # prevent switch error # http://stackoverflow.com/questions/14295337/win32gui-setactivewindow-error-the-specified-procedure-could-not-be-found shell = win32com.client.Dispatch("WScript.Shell") shell.SendKeys('%') ##### win32gui.SetForegroundWindow(hwnd) return # first only
def winEnumHandler( self, hwnd, ctx ): if win32gui.IsWindowVisible( hwnd ): # get the proccessid from the windowhandle processID = win32process.GetWindowThreadProcessId(hwnd)[1] processName = self.processManager.EnumProcesses(processID) windowTitle = win32gui.GetWindowText( hwnd ) if processName and processName.lower() == APPLICATION_NAME and self.path in windowTitle: if processName.lower() in self.applications_dict.keys(): self.applications_dict[processName.lower()].append( windowTitle ) else: self.applications_dict[processName.lower()] = [ windowTitle ]
def _move_window_win32_cb(self, hwnd, extra): window_title = win32gui.GetWindowText(hwnd) for name in self.window_name_list: if name in window_title and win32gui.IsWindowVisible(hwnd): # move position win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, self.pos_x, self.pos_y, self.window_width, self.window_height, 0) self.callback_ret = True logger.info('Found [{}] for moving position.'.format(name)) break
def f(hwnd, _): if not win32gui.IsWindowVisible(hwnd): return if win32gui.GetParent(hwnd): return if win32gui.GetWindow(hwnd, win32con.GW_OWNER): return title = win32gui.GetWindowText(hwnd) if not title: return windows.append((hwnd, title))
def tutorial(): hWndList = [] win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList) print(hWndList) for hwnd in hWndList: # only print the window which is visible if win32gui.IsWindowVisible(hwnd): title = win32gui.GetWindowText(hwnd) print(hwnd) print(title) print("")
def _enumWindows(self, hwnd, _): """遍历回调函数""" if hwnd == self.myhwnd: return # 防止自己嵌入自己 if win32gui.IsWindow(hwnd) and win32gui.IsWindowVisible( hwnd) and win32gui.IsWindowEnabled(hwnd): phwnd = win32gui.GetParent(hwnd) title = win32gui.GetWindowText(hwnd) name = win32gui.GetClassName(hwnd) self.windowList.addItem('{0}|{1}|\t标题:{2}\t|\t类名:{3}'.format( hwnd, phwnd, title, name))
def get_windowstate(hwnd): statu = "正常" if not gui.IsWindowVisible(hwnd): statu = "隐藏" if gui.IsIconic(hwnd): statu = "最小化" if (gui.GetWindowLong(hwnd, con.GWL_EXSTYLE) & con.WS_EX_TOPMOST) != 0: statu = "置顶" if gui.GetForegroundWindow() == hwnd: statu = "活动窗口" return statu
def enumHandlerMove(hwnd, lParam): old_name = lParam['old_name'] coords = lParam['coords'] new_name = lParam['name'] size = lParam['size'] if win32gui.IsWindowVisible(hwnd): if old_name in win32gui.GetWindowText(hwnd): win32gui.MoveWindow(hwnd, coords[0], coords[1], size[0], size[1], True) win32gui.SetWindowText(hwnd, new_name) print("Moved window: %s" % hwnd)
def callback(found_win, windows): # Determine if the window is application window if not win32gui.IsWindow(found_win): return True # Invisible windows are of no interest if not win32gui.IsWindowVisible(found_win): return True # Also disabled windows we do not want if not win32gui.IsWindowEnabled(found_win): return True exstyle = win32gui.GetWindowLong(found_win, win32con.GWL_EXSTYLE) # AppWindow flag would be good at this point if exstyle & win32con.WS_EX_APPWINDOW != win32con.WS_EX_APPWINDOW: style = win32gui.GetWindowLong(found_win, win32con.GWL_STYLE) # Child window is suspicious if style & win32con.WS_CHILD == win32con.WS_CHILD: return True parent = win32gui.GetParent(found_win) owner = win32gui.GetWindow(found_win, win32con.GW_OWNER) # Also window which has parent or owner is probably not an application window if parent > 0 or owner > 0: return True # Tool windows we also avoid # TODO: Avoid tool windows? Make exceptions? Make configurable? if exstyle & win32con.WS_EX_TOOLWINDOW == win32con.WS_EX_TOOLWINDOW: return True # There are some specific windows we do not want to switch to win_class = win32gui.GetClassName(found_win) if "WindowsScreensaverClass" == win_class or "tooltips_class32" == win_class: return True # Now we probably have application window # Get title # Using own GetWindowText, because win32gui.GetWindowText() doesn't # return unicode string. win_title = GetWindowText(found_win) # Removing all accents from characters win_title = unicodedata.normalize('NFKD', win_title).encode( 'ascii', 'ignore') # Get PID so we can get process name _, process_id = win32process.GetWindowThreadProcessId(found_win) process = "" try: # Get process name phandle = win32api.OpenProcess( win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, False, process_id) pexe = win32process.GetModuleFileNameEx(phandle, 0) pexe = os.path.normcase(os.path.normpath(pexe)) # Remove extension process, _ = os.path.splitext(os.path.basename(pexe)) except Exception, e: pass
def get_widows_information(hwnd, window_list): if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled( hwnd) and win32gui.IsWindowVisible(hwnd): title = win32gui.GetWindowText(hwnd) class_name = win32gui.GetClassName(hwnd) rect = win32gui.GetClientRect(hwnd) x, y = win32gui.ClientToScreen(hwnd, (rect[0], rect[1])) client_rect = (x, y, rect[2], rect[3]) if x != -32000 and y != -32000 and title != '': window_list.append( WindowInformation(title, class_name, client_rect))
def is_alt_tab_window(hwnd): """Check whether a window is shown in alt-tab. See http://stackoverflow.com/a/7292674/238472 for details. """ if not win32gui.IsWindowVisible(hwnd) or not win32gui.IsWindow(hwnd): return False hwnd_walk = win32con.NULL hwnd_try = ctypes.windll.user32.GetAncestor(hwnd, win32con.GA_ROOTOWNER) while hwnd_try != hwnd_walk: hwnd_walk = hwnd_try hwnd_try = ctypes.windll.user32.GetLastActivePopup(hwnd_walk) if win32gui.IsWindowVisible(hwnd_try): break if hwnd_walk != hwnd: return False # the following removes some task tray programs and "Program Manager" ti = TITLEBARINFO() ti.cbSize = ctypes.sizeof(ti) ctypes.windll.user32.GetTitleBarInfo(hwnd, ctypes.byref(ti)) if ti.rgstate[0] & win32con.STATE_SYSTEM_INVISIBLE: return False # Tool windows should not be displayed either, these do not appear in the # task bar. if win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) & win32con.WS_EX_TOOLWINDOW: return False pwi = WINDOWINFO() windll.user32.GetWindowInfo(hwnd, byref(pwi)) # A top-level window created with this style does not become the foreground # window when the user clicks it. The system does not bring this window to # the foreground when the user minimizes or closes the foreground window. # The window does not appear on the taskbar by default. if pwi.dwExStyle & win32con.WS_EX_NOACTIVATE: return False return True
def spdfhandler(hwnd, lParam): for proc in psutil.process_iter(): if proc.name() == "SumatraPDF.exe": if win32gui.IsWindowVisible(hwnd): if \'SumatraPDF\' in win32gui.GetWindowText(hwnd): win32gui.ShowWindow(hwnd,win32con.SW_MINIMIZE) win32gui.ShowWindow(hwnd,win32con.SW_MAXIMIZE) win32gui.SetForegroundWindow(hwnd) break else: subprocess.Popen(readerdir) break
def is_real_from_hwnd(hwnd): if not win32gui.IsWindowVisible(hwnd): return False if win32gui.GetParent(hwnd): return False hasNoOwner = win32gui.GetWindow(hwnd, win32con.GW_OWNER) == 0 lExStyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) if (((lExStyle & win32con.WS_EX_TOOLWINDOW) == 0 and hasNoOwner) or ((lExStyle & win32con.WS_EX_APPWINDOW != 0) and not hasNoOwner)): if win32gui.GetWindowText(hwnd): return True return False
def get_windows(hWnd, hWnds): if all([ win32gui.IsWindow(hWnd), win32gui.IsWindowVisible(hWnd), win32gui.GetWindowText(hWnd) ]): cls_name = win32gui.GetClassName(hWnd) hWnd_name = win32gui.GetWindowText(hWnd) if hWnds.get(cls_name, False): hWnds[cls_name].append(hWnd) else: hWnds[cls_name] = [hWnd]
def print(self): for hwnd in self._handles: rect = win32gui.GetWindowRect(hwnd) x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y print("PID %s:" % win32process.GetWindowThreadProcessId(hwnd)[1]) print("\t Is Visible : %d" % win32gui.IsWindowVisible(hwnd)) print("\t Class Name : %s" % win32gui.GetClassName(hwnd)) print("\t Location : (%d, %d)" % (x, y)) print("\t Size : (%d, %d)" % (w, h))
def gui_get_all_hwnd(hwnd, mouse): if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled( hwnd) and win32gui.IsWindowVisible(hwnd): if win32gui.GetClassName( hwnd) == "UnityWndClass" and win32gui.GetWindowText( hwnd ) == "PrincessConnectReDive": # DMM Game Player # emulator_lst.update({hwnd: "DMM_PrincessConnectReDive"}) print("yo!") else: win32gui.EnumChildWindows(hwnd, check_emulator_window, win32gui.GetWindowText(hwnd))
def enum_handler(hwnd, l_param): # Hidden window if not win32gui.IsWindowVisible(hwnd): self._wins.append(hwnd) return # Window which dose not have title bar bar_info = TitlebarInfoType() bar_info.cbSize = ctypes.sizeof(bar_info) ctypes.windll.user32.GetTitleBarInfo(hwnd, ctypes.byref(bar_info)) if bar_info.rgstate[0] & win32con.STATE_SYSTEM_INVISIBLE: self._wins.append(hwnd) return