def findwindow(self, classname, title): """ breadth-first traversal to find all the child ctrls in window parameters: classname is window's classname title, windows's title you can obtain above attributions by spy++ return all the handle of ctrls in a window """ try: parent = win32gui.FindWindow(classname, title) ctrls = [parent] ret_ctrls = [] node = None child = None while ctrls != []: node = ctrls.pop(0) ret_ctrls.append(node) child = win32gui.GetWindow(node, win32con.GW_CHILD) while child is not 0: ctrls.append(child) child = win32gui.GetWindow(child, win32con.GW_HWNDNEXT) except win32gui.error, e: import logging logging.error('Fail to find win32 window') raise RuntimeError(e)
def _clip_windows(self, screen): if not ctypes.windll.user32.IsWindowVisible(self.hwnd): # overwatch window not visible screen[:] = 50 return screen clip_hwnd = win32gui.GetWindow(self.hwnd, GW_HWNDPREV) while clip_hwnd: try: other_rect = Rect(*win32gui.GetWindowRect(clip_hwnd)) name = win32gui.GetWindowText(clip_hwnd) clip_window = True for whitelist_app in self.CLIP_WHITELIST: if whitelist_app.lower() in name.lower(): # logger.warning(f'Not clipping {name.value}') clip_window = False if (clip_window and win32gui.IsWindowVisible(clip_hwnd) and other_rect.width > 0 and other_rect.height > 0 and name): monitorspace_rect = Rect( left=max(0, other_rect.left - self.monitor.left), top=max(0, other_rect.top - self.monitor.top), right=max( 0, min(other_rect.right - self.monitor.left, self.monitor.width)), bottom=max( 0, min(other_rect.bottom - self.monitor.top, self.monitor.height)), ) # logger.debug(f'Clipping {name.value} | {self.win_rect}, {other_rect} => {monitorspace_rect}') if monitorspace_rect.width > 10 and monitorspace_rect.height > 10: screen[monitorspace_rect.top:monitorspace_rect.bottom, monitorspace_rect.left:monitorspace_rect. right, ] = 0 screen = cv2.putText( screen, name, (monitorspace_rect.left + 100, monitorspace_rect.top + 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, ) # else: # logger.debug(f'Clipping {name.value} | {self.win_rect}, {other_rect} => None') except: logger.exception(f"Failed to clip window") clip_hwnd = win32gui.GetWindow(clip_hwnd, GW_HWNDPREV) return screen
def findWindows(): ret = [] sdi = win32api.RegisterWindowMessage("SciTEDirectorInterface") w = win32gui.GetWindow(win32gui.GetDesktopWindow(), win32con.GW_CHILD) while w: res = win32gui.SendMessage(w, sdi, 0, 0) if res == sdi: ret.append(w) w = win32gui.GetWindow(w, win32con.GW_HWNDNEXT) return ret
def update(self): win_title = get_active_window_title() handle = ctypes.windll.user32.FindWindowW(0, get_title_string(win_title)) self.wh.configure(text='Window Handle: ' + hex(handle)) if win_title.lower() in ['パスワード', 'password', 'pass word']: self.handlenum = hex(handle) print("ok : " + self.handlenum) cid = win32gui.GetDlgCtrlID(handle) cls = win32gui.GetClassName(handle) length = win32gui.SendMessage(handle, win32con.WM_GETTEXTLENGTH, 0, 0) buff = ctypes.create_unicode_buffer(length + 1) win32gui.SendMessage(handle, win32con.WM_GETTEXT, length + 1, buff) print(cid, cls, buff.value) hchild = win32gui.GetWindow(handle, win32con.GW_CHILD) length = win32gui.SendMessage(hchild, win32con.WM_GETTEXTLENGTH, 0, 0) buff = ctypes.create_unicode_buffer(length + 1) win32gui.SendMessage(hchild, win32con.WM_GETTEXT, length + 1, buff) print(buff.value) # ctypes.windll.user32.SendMessageW(handle, win32con.WM_CHAR, 0x61, 0) handle2 = win32gui.GetWindow(handle, win32con.GW_HWNDNEXT) length = win32gui.SendMessage(handle2, win32con.WM_GETTEXTLENGTH, 0, 0) buff = ctypes.create_unicode_buffer(length + 1) win32gui.SendMessage(handle2, win32con.WM_GETTEXT, length + 1, buff) print(buff.value) hWnd_child = get_hwnd_search_control(handle, 0, 0, "OK") print("ターゲットのハンドル", hex(hWnd_child)) result = win32gui.PostMessage(handle, win32con.BM_CLICK, 0, 0) print(result) rect = win32gui.GetWindowRect(handle) print("ターゲットの位置:", rect) txt = win32gui.GetWindowText() print("ターゲットのテキスト:", txt) # 200ms周期で再表示 self.master.after(200, self.update)
def getowner(hwnd): owner = 0 try: owner = win32gui.GetWindow(hwnd, win32con.GW_OWNER) except: pass return owner
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 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 _check_for_no_windows(self): #print "Application._check_for_no_windows" ### apid = wp.GetCurrentProcessId() #print "... apid =", apid ### htop = gui.GetDesktopWindow() hwin = gui.GetWindow(htop, wc.GW_CHILD) while hwin: wpid = wp.GetWindowThreadProcessId(hwin)[1] if wpid == apid: #print "... hwin", hwin ### if gui.GetWindowLong(hwin, wc.GWL_STYLE) & wc.WS_VISIBLE: #print "...... is visible" ### return hwin = gui.GetWindow(hwin, wc.GW_HWNDNEXT) #print "... none visible" ### self.no_visible_windows()
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 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 screenshot(self, res_width, res_height): # Set the current window to your window win32gui.SetForegroundWindow(self._hwnd) # Credits to Frannecklp hwin = win32gui.GetWindow(self._hwnd, win32con.GW_OWNER) width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN) height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN) left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN) top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN) hwindc = win32gui.GetWindowDC(hwin) srcdc = win32ui.CreateDCFromHandle(hwindc) memdc = srcdc.CreateCompatibleDC() bmp = win32ui.CreateBitmap() bmp.CreateCompatibleBitmap(srcdc, width, height) memdc.SelectObject(bmp) memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY) signedIntsArray = bmp.GetBitmapBits(True) img = np.fromstring(signedIntsArray, dtype='uint8') img.shape = (height, width, 4) srcdc.DeleteDC() memdc.DeleteDC() win32gui.ReleaseDC(hwin, hwindc) win32gui.DeleteObject(bmp.GetHandle()) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img = cv2.resize(img, (res_width, res_height)) return img
def iterwnd(hidden=None): hwnd = win32gui.GetForegroundWindow() while hwnd: if hidden or win32gui.IsWindowVisible(hwnd): yield hwnd hwnd = win32gui.GetWindow(hwnd, win32con.GW_HWNDNEXT) return None
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) and (_bottom - _top == 204): dialog_l.append(handle)
def get_hwnd_search_control(hWnd, target_control_id, target_control_class, target_control_title): try: htarget = 0 if hWnd != 0: # チェック情報取得 cid = win32gui.GetDlgCtrlID(hWnd) cls = win32gui.GetClassName(hWnd) # ターゲットテキスト取得 length = win32gui.SendMessage(hWnd, win32con.WM_GETTEXTLENGTH, 0, 0) buff = ctypes.create_unicode_buffer(length + 1) win32gui.SendMessage(hWnd, win32con.WM_GETTEXT, length + 1, buff) # チェック if win32gui.IsWindowVisible(hWnd): if target_control_id == 0 or target_control_id == cid: if target_control_class == 0 or target_control_class == cls: if target_control_title == "" or target_control_title == buff.value: # ターゲット確認 htarget = hWnd # 渡されたハンドルがNGの場合、その子およびその階層をチェック if htarget == 0: hChild = win32gui.GetWindow(hWnd, win32con.GW_CHILD) while (hChild): htmp = get_hwnd_search_control(hChild, target_control_id, target_control_class, target_control_title) if htmp != 0: htarget = htmp break hChild = win32gui.GetWindow(hChild, win32con.GW_HWNDNEXT) else: htarget = 0 return htarget except Exception as err: # 取得失敗 print("【ERROR】get_hwnd_search_control【ERROR】") print(err) return 0
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_child_window(hwnd): """ @param hwnd long @return long """ try: return win32gui.GetWindow(hwnd, win32con.GW_CHILD) except: return 0
def get_last_window(hwnd): """ @param hwnd long @return long sibling """ try: return win32gui.GetWindow(hwnd, win32con.GW_HWNDLAST) except: return 0
def get_previous_window(hwnd): """ @param hwnd long @return long sibling """ try: return win32gui.GetWindow(hwnd, win32con.GW_HWNDPREV) except: return 0
def get_child_window(self, parent): """Wrapper for GetWindow(parent, GW_CHILD).""" try: window = win32gui.GetWindow(parent, win32con.GW_CHILD) except pywintypes.error: window = 0 if self.verbose >= 3: print "GetWindow(%d, GW_CHILD) => %d" % (parent, window) return window
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) == trade_hwnd: if (_right - _left == 300) and (_bottom - _top == 195): dialog_l.append(handle) elif (_right - _left == 345) and (_bottom - _top == 229): dialog_l.append(handle)
def find_tax_application_window(): hwnd_desktop = win32gui.GetDesktopWindow() hwnd_child = win32gui.GetWindow(hwnd_desktop, win32con.GW_CHILD) # let user select application and table cell (temporary workaround for 2017 version) print("Please select first cell in target table.") time.sleep(10) while hwnd_child: if win32gui.IsWindowVisible(hwnd_child): title = win32gui.GetWindowText(hwnd_child) if SSE_WINDOW_TITLE in title: return hwnd_child hwnd_child = win32gui.GetWindow(hwnd_child, win32con.GW_HWNDNEXT) # not found: return None
def cb(hwnd, hwnds): if win32gui.IsWindowVisible(hwnd): parent = win32gui.GetWindowText( win32gui.GetWindow(hwnd, win32con.GW_OWNER)) self.log("Parent " + parent) if "Control FOUR" in parent: title = win32gui.GetWindowText(hwnd) self.log("Remembering " + title) self.WITecWindows.append(title) return True
def enum_handler(hwnd, results): results[hwnd] = { "title": win32gui.GetWindowText(hwnd), "visible": win32gui.IsWindowVisible(hwnd), "minimized": win32gui.IsIconic(hwnd), "rectangle": win32gui.GetWindowRect(hwnd), # (left, top, right, bottom) "next": win32gui.GetWindow( hwnd, win32con.GW_HWNDNEXT) # Window handle to below window }
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 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 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 win_is_verify_code(hand, parent_trade_hwnd) -> bool: if win32gui.GetClassName(hand) != "#32770" or win32gui.GetWindow(hand, win32con.GW_OWNER) != parent_trade_hwnd: return False text = "" sons = [] win32gui.EnumChildWindows(hand, lambda handle, param: param.append(handle), sons) for son in sons: if win32gui.GetClassName(son) in ["Static", "Button"]: t = get_item_text(son) if t: text = text + t return '检测到您正在拷贝数据' in text and "提示" in text and '确定' in text
def cb(hwnd, hwnds): if win32gui.IsWindowVisible(hwnd): parent = win32gui.GetWindowText( win32gui.GetWindow(hwnd, win32con.GW_OWNER)) if "Control FOUR" in parent: title = win32gui.GetWindowText(hwnd) if (not title in self.WITecWindows) and ( not "WITec Control" in title): self.log("Closing " + title) win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) else: self.log("Ignoring " + title) return True
def isRealWindow(hWnd): '''Return True iff given window is a real Windows application window.''' if not win32gui.IsWindowVisible(hWnd): return False if 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