def get_top_window(windowHandle): while not is_movable_window(windowHandle) and win32gui.GetParent( windowHandle): try: windowHandle = win32gui.GetParent(windowHandle) except Exception, msg_: break
def GetControl(self, control_id=None): control_id = control_id or self.control_id try: h = win32gui.GetDlgItem(self.window.hwnd, control_id) except: hparent = win32gui.GetParent(self.window.hwnd) hparent = win32gui.GetParent(hparent) h = win32gui.GetDlgItem(hparent, control_id) return h
def OnKeyboardEvent(self, event): if not self.visible: #allow all windows return True if event.Key and event.Key.lower() in [ 'lwin', 'tab', 'lmenu', 'escape' ]: return False # block these keys a = [self.window.handle] if self.grabed_window and win32gui.IsChild( self.grabed_window.window.handle, event.Window): return True if self.grabed_window: a.append(self.grabed_window.window.handle) try: p = win32gui.GetParent(event.Window) except: return False if self.grabed_window and win32gui.IsChild( self.grabed_window.window.handle, p): return True return event.Window in a
def init(): def _MyCallback(hwnd, extra): windows = extra temp = list() temp.append(hex(hwnd)) temp.append(win32gui.GetClassName(hwnd)) temp.append(win32gui.GetWindowText(hwnd)) windows[hwnd] = temp def TestEnumWindows(): windows = {} win32gui.EnumWindows(_MyCallback, windows) return windows w = TestEnumWindows() workerw = [] check = '' # 匹配+搜索 for i in w: if 'WorkerW' == w[i][1]: workerw.append(w[i][0]) for i in w: if 'Progman' == w[i][1]: check = w[i][0] for i in workerw: if hex(win32gui.GetParent(eval(i))) == check: break print("\033[1;36m 初始化成功!!!\nhwnd:{}\033[0m".format(check)) return i
def __init__(self, hwnd=None, pid=None): self.pid = pid self.hwnd = hwnd if hwnd is not None: self.title = win32gui.GetWindowText(hwnd) _, self.pid = win32process.GetWindowThreadProcessId(hwnd) self.visible = win32gui.IsWindowVisible(hwnd) self.enabled = win32gui.IsWindowEnabled(hwnd) else: self.title = '' self.visible = None self.enabled = None if pid is not None: try: p = psutil.Process(pid) self.pname = p.name() except Exception: pass # print(self.pname) # PROCESS_QUERY_INFORMATION (0x0400) or PROCESS_VM_READ (0x0010) or PROCESS_ALL_ACCESS (0x1F0FFF) try: process = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, False, self.pid) self.pname = win32process.GetModuleFileNameEx(process, 0).split( os.path.sep)[-1] except Exception: self.pname = '' try: self.parent = win32gui.GetParent(hwnd) except Exception: self.parent = None
def enum_windows_func(hWnd, lParam): global count if not win32gui.GetWindow( hWnd, win32con.GW_OWNER) and win32gui.IsWindowVisible(hWnd): name: str = win32gui.GetClassName(hWnd) if name == 'Shell_TrayWnd' or name == 'Progman': return title = win32gui.GetWindowText(hWnd) rect = win32gui.GetWindowRect(hWnd) # 排除一些 dummy 的窗口 if rect == (0, 0, 0, 0): return count = count + 1 if '迷糊' in title: icon_handle = win32gui.SendMessage(hWnd, win32con.WM_GETICON, win32con.ICON_BIG, 0) print(name, hWnd, title, icon_handle) # result = win32gui.ShowWindow(hWnd, win32con.SW_SHOWNORMAL) # print('迷糊', result) print(hWnd, win32gui.GetParent(hWnd)) # win32gui.SetWindowPos(hWnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, # win32con.SWP_NOSIZE | win32con.SWP_NOMOVE | win32con.SWP_DRAWFRAME) # 对于多桌面这一点也是有效果的 win32gui.SetForegroundWindow(hWnd) # re = win32gui.SetActiveWindow(hWnd) # print(re) # icon_handle = win32gui.SendMessage(hWnd, win32con.WM_GETICON, win32con.ICON_BIG, 0) print('列举', count, name, rect, hex(hWnd), title)
def validate(self): """ Control some properties of the window to see if it should be tiled """ if win32gui.IsWindowVisible(self.hWindow): value = win32gui.GetWindowLong(self.hWindow, GWL_EXSTYLE) owner = win32gui.GetWindow(self.hWindow, GW_OWNER) if not win32gui.GetParent(self.hWindow): if (not owner and not value & WS_EX_TOOLWINDOW) or value & WS_EX_APPWINDOW: if not self.floating: return True else: if value & WS_EX_WINDOWEDGE: return True return False
def getparent(hwnd): parenthwnd = 0 try: parenthwnd = win32gui.GetParent(hwnd) except: pass return parenthwnd
def get_win_type(hwnd): # TODO: Improve the conditions # Note: No DOCK type # Is default ignore window? if _default_ignore_wins.check(hwnd): return WindowType.OTHER # Is an invalid window? if not win32gui.IsWindow(hwnd) or not win32gui.IsWindowEnabled(hwnd) or \ not win32gui.GetWindowText(hwnd): return WindowType.OTHER # Is specific class? class_name = win32gui.GetClassName(hwnd) if class_name == '#32770': # Dialog return WindowType.DIALOG # Is non-application window? if win32gui.GetParent(hwnd): return WindowType.OTHER # Is Floating or non-resizable? style = win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE) ex_style = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) if not style & win32con.WS_SIZEBOX or \ ex_style & win32con.WS_EX_TOOLWINDOW or \ ex_style & win32con.WS_EX_TOPMOST: return WindowType.DIALOG else: return WindowType.NORMAL
def getInnerHandle(self, handle): # cannot get the correct "inner handle" return nextHndle = handle user32 = ctypes.windll.user32 # controls = mf.findControls(handle, wantedClass="Scintilla") print 'Scintilla controls: %s' % controls for c in controls: ln = self.getCurrentLineNumber(c) numberLines = self.getNumberOfLines(c) visible1 = self.isVisible(c) info = win32gui.GetWindowPlacement(c) print 'c: %s, linenumber: %s, nl: %s, info: %s' % ( c, ln, numberLines, repr(info)) parent = c while 1: parent = win32gui.GetParent(parent) clName = win32gui.GetClassName(parent) visible = self.isVisible(parent) info = win32gui.GetWindowPlacement(parent) print 'parent: %s, class: %s, visible: %s, info: %s' % ( parent, clName, visible, repr(info)) if parent == handle: print 'at top' break
def IsSecureCRTTopCmdWnd(hWnd): # no effect: win32gui.IsWindowEnabled(hWnd) win32gui.IsWindowVisible(hWnd) if 'AfxFrameOrView120u' != win32gui.GetClassName(hWnd): return False if 0 != win32gui.GetWindow(win32gui.GetParent(hWnd), win32con.GW_HWNDPREV): return False return True
def is_app_window(win): # Determine if the window is application window if not win32gui.IsWindow(win): return False # Invisible windows are of no interest if not win32gui.IsWindowVisible(win): return False # Also disabled windows we do not want if not win32gui.IsWindowEnabled(win): return False exstyle = win32gui.GetWindowLong(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(win, win32con.GWL_STYLE) # Child window is suspicious if style & win32con.WS_CHILD == win32con.WS_CHILD: return False parent = win32gui.GetParent(win) owner = win32gui.GetWindow(win, win32con.GW_OWNER) # Also window which has parent or owner is probably not an application window if parent > 0 or owner > 0: return False # Tool windows we also avoid # TODO: Avoid tool windows? Make exceptions? Make configurable? if exstyle & win32con.WS_EX_TOOLWINDOW == win32con.WS_EX_TOOLWINDOW: return False # There are some specific windows we do not want to switch to win_class = win32gui.GetClassName(win) if "WindowsScreensaverClass" == win_class or "tooltips_class32" == win_class: return False # Now we probably have application window return True
def find_table_parameters(self): """Finds poker client window with the given table name.""" titles = {} win32gui.EnumWindows(win_enum_handler, titles) for hwnd in titles: if titles[hwnd] == "": continue # if window not visible, probably not a table if not win32gui.IsWindowVisible(hwnd): continue # if window is a child of another window, probably not a table if win32gui.GetParent(hwnd) != 0: continue HasNoOwner = win32gui.GetWindow(hwnd, win32con.GW_OWNER) == 0 WindowStyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) if HasNoOwner and WindowStyle & win32con.WS_EX_TOOLWINDOW != 0: continue if not HasNoOwner and WindowStyle & win32con.WS_EX_APPWINDOW == 0: continue if re.search(self.search_string, titles[hwnd], re.I): if self.check_bad_words(titles[hwnd]): continue self.number = hwnd break if self.number is None: log.error(_("Window %s not found. Skipping."), self.search_string) return self.title = titles[self.number] self.hud = None self.gdkhandle = QWindow.fromWinId(self.number)
def _sendNotifyMessage(hwnd, nofifyMessage): '''Send a notify message to a control.''' win32gui.SendMessage(win32gui.GetParent(hwnd), win32con.WM_COMMAND, _buildWinLong(nofifyMessage, win32api.GetWindowLong(hwnd, win32con.GWL_ID)), hwnd)
def callback(self, hwnd, param): if not win32gui.IsWindowVisible(hwnd): return if win32gui.GetWindowTextLength(hwnd) == 0: return if win32gui.GetParent(hwnd): return self.list.update({win32gui.GetWindowText(hwnd) : hwnd})
def __isRealWindow(self, hWnd): if not win32gui.IsWindowVisible(hWnd): return False if win32gui.GetParent(hWnd) != 0: return False if win32gui.GetWindowText(hWnd): return True return False
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 as e: pass # Add hwnd and title to the list windows.append((found_win, "%s: %s" % (process, win_title))) return True
def get_window_parent(hwnd): """ @param hwnd HWND @return 0 """ try: return win32gui.GetParent(hwnd) except: return 0
def __init__(self, window, webdriver, offscreen_win=None): self._window = window self._offscreen_win = offscreen_win self._webdriver = webdriver self._parent_wnd = win32gui.GetParent(self._window.HWnd) # 如果获取的父窗口是空 if self._parent_wnd == 0: self._parent_wnd = self._window.HWnd self._browser_type = 'not defined'
def _get_key_layout_handle(self, hwnd, wildcard): """Get window's key handler. :param hwnd: window handle. :param wildcard: wildcard. """ if self.key_handle_name == win32gui.GetWindowText(hwnd) and self.key_handle_class == win32gui.GetClassName( hwnd) and self.parent_hwnd == win32gui.GetParent(hwnd): self.key_handle = hwnd
def get_parent_info(self, hwnd): parent_hwnd = win32gui.GetParent(hwnd) #log.debug("parent hwnd: %s" % parent_hwnd) ### get parent title parent_title = win32gui.GetWindowText(parent_hwnd) return (parent_hwnd, parent_title)
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
def _get_key_layout_handle(self, hwnd, wildcard): """Gets information about general key handler. :param int hwnd: window handle. :param str wildcard: wildcard. """ if self.key_handle_name == win32gui.GetWindowText( hwnd) and self.key_handle_class == win32gui.GetClassName( hwnd) and self.parent_hwnd == win32gui.GetParent(hwnd): self.key_handle = hwnd
def isRealWindow(self, hWnd): # Thanks DzinX from stackoverflow. """Return True if given window is a real Windows application window.""" if not win32gui.IsWindowVisible(hWnd) or win32gui.GetParent(hWnd) != 0: 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_win_info(): handle = win32gui.GetForegroundWindow() print("[>] Foreground window number: " + str(handle)) win_name = str(win32gui.GetWindowText(handle)) print(f'Foreground window text: {win_name}') print("Find Windows Ex: " + str(win32gui.FindWindowEx(handle, 0, None, None))) #print(win32api.GetHandleInformation(handle)) print("Parrent: " + str(win32gui.GetParent(handle))) return handle, win_name
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 callback_child(self, hwnd, wildcard): """ find child window """ #log.debug("hwnd:%s" % hwnd) try: ### get title title = win32gui.GetWindowText(hwnd) #log.debug("title:%s" % title) ### get parent hwnd parent_hwnd = win32gui.GetParent(hwnd) #log.debug("parent hwnd: %s" % parent_hwnd) ### get parent title parent_title = win32gui.GetWindowText(parent_hwnd) #log.debug("parent tilte: %s" % parent_title) ### get class name class_name = win32gui.GetClassName(hwnd) if class_name in self.class_name_list: if hwnd in self.win_dict: pass else: self.win_dict[hwnd] = [] #log.debug("title:%s" % title) #log.debug("class name: %s" % class_name) #log.debug("parent tilte: %s" % parent_title) if (class_name, hwnd) in self.win_dict[hwnd]: pass else: if self.parent_title_list == None: self.win_dict[hwnd].append((class_name, hwnd, parent_title, title)) elif parent_title in self.parent_title_list: #self.target_list.append((class_name, hwnd)) self.win_dict[hwnd].append((class_name, hwnd, parent_title, title)) else: #log.debug((title, parent_hwnd, parent_title, class_name))#print(hwnd, title, cls) pass except Exception as ex: log.error(ex)
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 windowEnumTopLevelCb(self, hwnd, windowsList): """ Window Enum function for getTopLevelWindows """ title = win32gui.GetWindowText(hwnd) title = title.decode('gbk').encode('utf-8') className = win32gui.GetClassName(hwnd) className = title.decode('gbk').encode('utf-8') if win32gui.GetParent(hwnd) == 0 and title != '': windowsList.append((hwnd, unicode(title, errors='ignore')))
def GetParent(self): """ Gets the parent window :return: A task.WindowInfo object that represents the parent window :rtype: task.WindowInfo """ self.AssertAlive() parent_hwnd = win32gui.GetParent(self.hwnd) if not parent_hwnd: raise WindowInfo.NoParent("current window has no parent") return WindowInfo(parent_hwnd)