def all_ok(hwnd, param): text = win32gui.GetWindowText(hwnd) class_name = win32gui.GetClassName(hwnd) print('#{:0>8x} "{}": {}'.format(hwnd, text, class_name)) # Закрытие панели инструментов if class_name == 'ToolbarWindow32': win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) return True
def show_window_attr(self,hWnd): if not hWnd: return title = win32gui.GetWindowText(hWnd) clsname = win32gui.GetClassName(hWnd) left, top, right, bottom =win32gui.GetWindowRect(hWnd) # attr = {'hWnd':hWnd,'title': title, 'clsname':clsname, 'post':[left,top,right,bottom]} return attr
def windowEnumerationHandler(hwnd, top_windows): clsName = win32gui.GetClassName(hwnd) winStyle = win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE) if clsName == 'mintty' and winStyle & win32con.WS_VISIBLE: threadId, processId = win32process.GetWindowThreadProcessId(hwnd) procHdl = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, False, processId) info = win32process.GetProcessTimes(procHdl) top_windows.append((hwnd, info['CreationTime'], clsName, win32gui.GetWindowText(hwnd)))
def zjb(hwnd,c=None): """ 传入父窗口的句柄,寻找父句柄下的所有子句柄 """ handle = win32gui.FindWindowEx(hwnd,0,None,None) handlelist=[] while handle>0: if c: if c in win32gui.GetWindowText(handle) or c in win32gui.GetClassName(handle):handlelist.append(handle) else:handlelist.append(handle) handle = win32gui.FindWindowEx(hwnd,handle,None,None) return handlelist
def getMEmuWinHwnd(self): Thwnd = -1 hwndList = self.get_child_windows(0) for hwnd in hwndList: try: className = win32gui.GetClassName(hwnd) if className == 'Qt5QWindowIcon': #title = win32gui.GetWindowText(hwnd) #if title == 'MEmu': left, top, right, bottom = win32gui.GetWindowRect(hwnd) h = bottom - top if h > 780: Thwnd = hwnd break except: pass if Thwnd != -1: left, top, right, bottom = win32gui.GetWindowRect(Thwnd) print('发现安卓窗口,hwnd:', hex(Thwnd), ' 窗口:', str(left) + " " + str(top) + " " + str(right) + " " + str(bottom) + ' w= ' + str(right-left) + " h= " + str(bottom - top)) self._hwndMain = Thwnd """ # FindWindowEx(hwndParent=0, hwndChildAfter=0, lpszClass=None, lpszWindow=None); Thwnd = win32gui.FindWindowEx(Thwnd,0,'Qt5QWindowIcon', 'MainWindowWindow') # 安卓主窗口home键窗口 hwndHome = self.get_child_windows(Thwnd) for hwnd in hwndHome: className = win32gui.GetClassName(hwnd) if className == 'Qt5QWindowIcon': left, top, right, bottom = win32gui.GetWindowRect(hwnd) w = right - left if w == 36: print('发现安卓Home键窗口,hwnd:', hex(hwnd), ' 窗口:', str(left) + " " + str(top) + " " + str(right) + " " + str(bottom) + ' w= ' + str(right-left) + " h= " + str(bottom - top)) self._hwndMainButton = hwnd Thwnd = win32gui.FindWindowEx(Thwnd,0,'Qt5QWindowIcon','CenterWidgetWindow') Thwnd = win32gui.FindWindowEx(Thwnd,0,'Qt5QWindowIcon','RenderWindowWindow') Thwnd = win32gui.FindWindowEx(Thwnd,0,'subWin','sub') Thwnd = win32gui.FindWindowEx(Thwnd,0,'subWin','sub') left, top, right, bottom = win32gui.GetWindowRect(Thwnd) print('发现安卓窗口,hwnd:', hex(Thwnd), ' 窗口:', str(left) + " " + str(top) + " " + str(right) + " " + str(bottom) + ' w= ' + str(right-left) + " h= " + str(bottom - top)) self._hwndMain = Thwnd """ else: print("未发现安卓窗口")
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 get_c(br_instance): childlist = [] channels = [] win32gui.EnumChildWindows( br_instance, lambda br_instance, oldlist: oldlist.append(br_instance), childlist) for w in childlist: if 'BRChildClass' in win32gui.GetClassName(w): channels.append(w) #print win32gui.GetClassName(w) , win32gui.GetWindowText(w), w return channels
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 call_back(handle, dialog_l): _left, _top, _right, _bottom = win32gui.GetWindowRect(handle) # (_right - _left == 300) and (_bottom - _top == 195) # print(win32gui.GetParent(handle)) if win32gui.GetClassName(handle) == "#32770" and \ win32gui.GetWindow(handle, win32con.GW_OWNER) == self.__parent_trade: # if (_right - _left == 362) or (_right - _left == 341): # print(handle) # dialog_l.append(handle) if win_is_msg(handle): dialog_l.append(handle)
def matcher(window_handle): target = "" if need_ID: target += "ID>" + format_ID(window_handle) + "\n" if need_title: target += "title>" + win32gui.GetWindowText(window_handle) + "\n" if need_class: target += "class>" + win32gui.GetClassName(window_handle) + "\n" if need_app: target += "app>" + executable_for_window(window_handle) + "\n" if not (need_ID or need_title or need_class or need_app): target = win32gui.GetWindowText(window_handle) match = pattern.search(target) return match and not is_results_window(window_handle)
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 find_idxSubHandle(hwnd,handlelist=[]): """ 传入父窗口的句柄,和要查找的标题或者类型(默认为None),寻找父句柄下的所有子孙句柄 """ handle = win32gui.FindWindowEx(hwnd, 0, None, None) while handle>0: pan_xuanze=0 if btlx: if btlx in win32gui.GetWindowText(handle) or btlx in win32gui.GetClassName(handle):pan_xuanze=1 else:pan_xuanze=1 if pan_xuanze:handlelist.append(handle) find_idxSubHandle(handle,handlelist) handle = win32gui.FindWindowEx(hwnd, handle, None, None)
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 ResetWnd(title): hWndList = [] win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList) for hwnd in hWndList: classname = win32gui.GetClassName(hwnd) titlename = win32gui.GetWindowText(hwnd) if (titlename.find(title) >= 0): rect = win32gui.GetWindowRect(hwnd) win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 500, 500, win32con.SWP_SHOWWINDOW)
def filter_func(hwnd): # 找到classname = '#32770' 的窗体 re_classname_pattern = 'ToolbarWindow32' clsname = win32gui.GetClassName(hwnd) if re.match(re_classname_pattern, clsname) is None: return False # 找到 窗体标题为 “提示”的窗体 hwnd_chld_list = [] try: win32gui.EnumChildWindows( hwnd, lambda hwnd_sub, hwnd_chld_list_tmp: hwnd_chld_list_tmp. append(hwnd_sub), hwnd_chld_list) for hwnd_sub in hwnd_chld_list: if win32gui.GetClassName( hwnd_sub) == 'Static' and win32gui.GetWindowText( hwnd_sub) == '提示': return True except: pass return False
def ThreadFunc(argc): handle = win32gui.GetForegroundWindow() while True: window_title = win32gui.GetWindowText(handle) window_class = win32gui.GetClassName(handle) #print("句柄:", handle) #print("类名:", window_class) #print("标题:", window_title) #print("-" * 60) ret = GetTopClass() if (ret["handle"] == handle): print("自己是当前窗口") #开始菜单 if (ret["state"] == 0 and ret["class"] == "Windows.UI.Core.CoreWindow"): print("关闭开始菜单") win32api.keybd_event(91, 0, 0, 0) time.sleep(0.1) win32api.keybd_event(91, 0, win32con.KEYEVENTF_KEYUP, 0) time.sleep(0.1) #如果出现添加群表示这个群已经把我删除了 if (ret["state"] == 0 and ret["title"] == "添加群"): print("关闭添加群对话框") win32api.keybd_event(18, 0, 0, 0) #ALT time.sleep(0.1) win32api.keybd_event(115, 0, 0, 0) #F4 time.sleep(0.1) win32api.keybd_event(115, 0, win32con.KEYEVENTF_KEYUP, 0) time.sleep(0.1) win32api.keybd_event(18, 0, win32con.KEYEVENTF_KEYUP, 0) #如果出现添加群表示这个群已经把我删除了 if (ret["state"] == 0 and (ret["title"] == "关闭提示" or ret["title"] == "提示")): print("退出确认对话框") win32api.keybd_event(32, 0, 0, 0) #F4 time.sleep(0.1) win32api.keybd_event(32, 0, win32con.KEYEVENTF_KEYUP, 0) time.sleep(0.1) ret = GetTopClass() if (ret["state"] == 0 and (ret["title"] == "关闭提示" or ret["title"] == "提示")): print("退出确认对话框") win32api.keybd_event(32, 0, 0, 0) #F4 time.sleep(0.1) win32api.keybd_event(32, 0, win32con.KEYEVENTF_KEYUP, 0) time.sleep(0.1) #print("类名:", ret["class"]) #print("窗口:", ret["title"]) #print("*" * 60) time.sleep(10)
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 getmouseinfo(): ForegroundWindowHandle = win32gui.GetForegroundWindow() # Active(ForegroundWindowHandle) print "ForegroundWindowHandle========",ForegroundWindowHandle title = win32gui.GetWindowText(ForegroundWindowHandle) classname = win32gui.GetClassName(ForegroundWindowHandle) (x_, y_, w_, z_) = win32gui.GetWindowRect(ForegroundWindowHandle) (x, y) = win32gui.GetCursorPos() x_insidewindow = x - x_ y_insidewindow = y - y_ return title, classname, x_insidewindow, y_insidewindow
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 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 __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 callback(hwnd, hwnds): print win32gui.GetWindowText(hwnd), win32gui.GetClassName(hwnd) #print if win32gui.GetWindowText(hwnd) == 'Export Mesh (.obj)': hwnds.append(hwnd) if win32gui.GetWindowText(hwnd) == 'Export Files': hwnds.append(hwnd) if win32gui.GetWindowText(hwnd) == 'Close': hwnds.append(hwnd)
def get_handle_to_NIS_livefeed(self): ''' This function gets the handle to the NIS livefeed. ''' for i in range(1, 999999, 1): try: if str(win32gui.GetClassName(i)) == "G5_PICWND_CLASS": return i except: pass return None
def find_window_callback(id, key): # 获取窗口时候的回调 if win32gui.GetWindowText(id).strip(): _item = [] _item.append(hex(id)) _item.append(win32gui.GetClassName(id)) _item.append(win32gui.GetWindowText(id)) if key == 'windows': windows[id] = _item pass pass
def __set_trade_hwnd(self): hwnd_list = [] win32gui.EnumWindows(lambda handle, param: param.append(handle), hwnd_list) for hwnd in hwnd_list: if win32gui.GetWindowText( hwnd ) == "网上股票交易系统5.0" and "Afx:400000" in win32gui.GetClassName(hwnd): self.trade_hwnd = hwnd return print("未找到交易页面")
def handler(hwnd, windows): # log.logger.debug("\t\thandler-> %d", hwnd) ps = windows p = [] p.append(hex(hwnd)) p.append(win32gui.GetClassName(hwnd)) p.append(win32gui.GetWindowText(hwnd)) if filter(hwnd, p[2]): # log.logger.debug("\t\thandler<-\t%d\t%s", hwnd, p) ps[hwnd] = p
def zjb(hwnd,btlx=None): """ 传入父窗口的句柄,和要查找的标题或者类型(默认为None),寻找父句柄下的所有子句柄 """ handle = win32gui.FindWindowEx(hwnd,0,None,None) handlelist=[] while handle>0: if btlx: if btlx in win32gui.GetWindowText(handle) or btlx in win32gui.GetClassName(handle): handlelist.append(handle) else:handlelist.append(handle) handle = win32gui.FindWindowEx(hwnd,handle,None,None) return handlelist
def handleSub(hdl, param): cls = win32gui.GetClassName(hdl) print cls if cls != 'TPanel': return True try: txt = win32gui.GetWindowText(hdl) if txt.decode('gbk') == u'检查机房': print 'match' except Exception, e: print e
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