def setMaximize(self, filter): all_str = [] all_int = [] for i in filter: if str(i).isdigit(): all_int.append(int(i)) else: all_str.append(i) debug(all_int=all_int) debug(all_str=all_str) hdls, h_hide, h_hide_filter, h_pids = self.getListWindows( filter=all_str, print_list=False, pid=all_int) debug(h_hide=h_hide) debug(h_pids=h_pids) if h_pids: if len(h_pids) > 1: n = 1 for i in h_pids: print(str(n) + ". " + str(i[0])) n += 1 q = raw_input( "Select number to show [all = for show all of hide]: ") if q and str(q).isdigit() and not int(q) > len(h_pids): h = h_pids[int(q) - 1][-1] win32gui.ShowWindow(h, 1) elif q and str(q).strip() == 'all': for i in h_pids: win32gui.ShowWindow(i[-1], win32con.SW_MAXIMIZE) win32gui.BringWindowToTop(i[-1]) else: h = h_pids[0][-1] win32gui.ShowWindow(h, win32con.SW_MAXIMIZE) win32gui.BringWindowToTop(h) else: if hdls: if len(hdls) > 1: n = 1 for i in hdls: print(str(n) + ". " + str(i[0])) n += 1 q = raw_input( "Select number to show [all = for show all of hide]: ") if q and str(q).isdigit() and not int(q) > len(hdls): h = hdls[int(q) - 1][-1] win32gui.ShowWindow(h, 1) elif q and str(q).strip() == 'all': for i in hdls: win32gui.ShowWindow(i[-1], win32con.SW_MAXIMIZE) win32gui.BringWindowToTop(i[-1]) else: h = hdls[0][-1] win32gui.ShowWindow(h, win32con.SW_MAXIMIZE) win32gui.BringWindowToTop(h)
def main(): # メモ帳の起動 subprocess.Popen(r'notepad.exe') # 起動するまで待機 time.sleep(1) # 起動したメモ帳のハンドルを取得する。 hw1 = win32gui.GetForegroundWindow() print(hw1) tid1 = win32process.GetWindowThreadProcessId(hw1) # ウェイトして他プロセスをアクティブにしておく。 time.sleep(10) # アクティブにした他プロセスのハンドルを取得する。 hw2 = win32gui.GetForegroundWindow() print(hw2) tid2 = win32process.GetWindowThreadProcessId(hw2) # トップレベルウィンドウを切り替える(この時点ではフォーカスできていない) win32process.AttachThreadInput(tid1, tid2, True) win32gui.BringWindowToTop(hw1) win32process.AttachThreadInput(tid1, tid2, False) # ウィンドウの左上の隅っこをクリックしてアクティブにする。 (left, top, right, bottom) = win32gui.GetWindowRect(hw1) print(left, top, right, bottom) win32api.SetCursorPos((left + 1, top + 1)) time.sleep(0.1) win32api.mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0) time.sleep(0.1) win32api.mouse_event(MOUSEEVENTF_LEFTUP, 0, 0)
def run(self): self._handle = None wildcard = "80CC9F66-E7D8-4DDD-85B6-D9E6CD0E93E2x0x8x0" # wildcard = "Microsoft Visual Studio" win32gui.EnumWindows(self._window_enum_callback, wildcard) if self._handle != None: win32gui.BringWindowToTop(self._handle)
def __findQTWalletWindow(self): cb = lambda x,y: y.append(x) wins = [] win32gui.EnumWindows(cb,wins) # now check to see if any match our regexp: tgtWin = -1 for win in wins: txt = win32gui.GetWindowText(win) if txt == 'Myriadcoin Core - Wallet': tgtWin = win if tgtWin >= 0: win32gui.ShowWindow(tgtWin, win32con.SW_RESTORE) win32gui.ShowWindow(tgtWin, win32con.SW_SHOW) win32gui.BringWindowToTop(tgtWin) win32gui.SetForegroundWindow(tgtWin) # ShowWindow(hWnd,SW_SHOW); #::BringWindowToTop(hWnd); #::SetForegrounWindow(hWnd) return True return False
def showTweditWindowWithPIDInForeground(_pid): # documentaiton of win32con window flags and win32gui showWindow functions # style=win32con.SW_SHOWNORMAL : int # Specifies how the window is to be shown. It must be one of win32con.SW_HIDE, win32con.SW_MINIMIZE, win32con.SW_RESTORE, win32con.SW_SHOW, win32con.SW_SHOWMAXIMIZED win32con.SW_SHOWMINIMIZED, win32con.SW_SHOWMINNOACTIVE, win32con.SW_SHOWNA, win32con.SW_SHOWNOACTIVATE, or win32con.SW_SHOWNORMAL import win32gui import win32con import win32process handle = [] win32gui.EnumWindows(win_enum_callback_twedit, handle) # try: # dbgMsg("this is twedit window handle,",handle[0]) # except IndexError,e: # print "problem with getting window handle" print "HANDLE ELELMENTS:", len(handle) # pid=win32process.GetCurrentProcessId() for hwnd in handle: t, p = win32process.GetWindowThreadProcessId(hwnd) print "t=", t, " p=", p print "pid=", _pid # pid = win32process.GetProcessId() # print "pid=",pid if _pid == p: win32gui.SetActiveWindow(hwnd) win32gui.BringWindowToTop(hwnd) # this does not work on windows Vista dna windows 7 # win32gui.SetFocus(hwnd) win32gui.ShowWindow(hwnd, win32con.SW_RESTORE) win32gui.SetForegroundWindow(hwnd)
def __call__(self, ensoapi, window=None): if window is None: return None logging.debug("Go to window '%s'" % window) for hwnd, title in self.windows: title = xml.sax.saxutils.escape(title).lower() if title == window: try: #windowPlacement = win32gui.GetWindowPlacement(hwnd) #showCmd = windowPlacement[1] #print showCmd #if showCmd == SW_RESTORE: # win32gui.ShowWindow(hwnd, SW_RESTORE) #else: # win32gui.BringWindowToTop(hwnd) if win32gui.IsIconic(hwnd): win32gui.ShowWindow(hwnd, win32con.SW_RESTORE) win32gui.SetForegroundWindow(hwnd) except Exception, e: if e[0] == 0: time.sleep(0.2) try: win32gui.SetForegroundWindow(hwnd) except Exception, e: time.sleep(0.2) try: win32gui.BringWindowToTop(hwnd) except Exception, e: pass elif e[0] == 2: pass
def show_window_background(): """ Show the window in background, not minimized """ window = win32gui.FindWindow(MINECRAFT_CLASS_NAME, MINECRAFT_TITLE + MINECRAFT_VERSION) win32gui.SetForegroundWindow(window) win32gui.BringWindowToTop(window)
def showTweditWindowInForeground1(): # documentaiton of win32con window flags and win32gui showWindow functions # style=win32con.SW_SHOWNORMAL : int # Specifies how the window is to be shown. It must be one of win32con.SW_HIDE, win32con.SW_MINIMIZE, # win32con.SW_RESTORE, win32con.SW_SHOW, win32con.SW_SHOWMAXIMIZED win32con.SW_SHOWMINIMIZED, win32con. # SW_SHOWMINNOACTIVE, win32con.SW_SHOWNA, win32con.SW_SHOWNOACTIVATE, or win32con.SW_SHOWNORMAL import win32gui import win32con handle = [] win32gui.EnumWindows(win_enum_callback_twedit, handle) if len(handle): # win32gui.SetForegroundWindow(handle[0]) win32gui.SetActiveWindow(handle[0]) win32gui.BringWindowToTop(handle[0]) win32gui.SetFocus(handle[0]) win32gui.ShowWindow(handle[0], win32con.SW_RESTORE)
def switch_to_window(switch): switch = switch.lower() if (switch == "reference card" or switch == "reference"): webbrowser.open_new_tab('EVOCreferencecard.html') return #Generates list of the hwnd of all 'real' windows. def call(hwnd, param): """ The callback function to be used by EnumWindows. Appends all hwnds to param list """ param.append(hwnd) winds = [] win32gui.EnumWindows(call, winds) for window in winds: print(win32gui.GetWindowText(window)) if switch in win32gui.GetWindowText(window).lower(): print("found window") win32gui.ShowWindow(window, SW_SHOW) win32gui.BringWindowToTop(window) send_input("alt") win32gui.SetForegroundWindow(window) win32gui.SetWindowPos(window, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE) win32gui.SetWindowPos(window, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE) win32gui.SetWindowPos(window, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE) break
def open_friend_management_window(self, timeout=1): # 切换到好友列表 self.change_friend_tab() time.sleep(5 if self.send_setting is None else float( self.send_setting['login_delay'])) # 打开好友管理器 round = 3 while round > 0: count = 10 while count > 0: win32gui.BringWindowToTop(self.hwnd) self.main_dm.MoveTo(64, 151) self.main_dm.RightClick() time.sleep(timeout) # 向下移动菜单高度和宽度,点击【好友管理器】 self.main_dm.MoveR(80, 271) self.main_dm.LeftClick() time.sleep(0.5) friends_hwnd = self.get_friends_hwnd() if friends_hwnd is not None: return friends_hwnd else: time.sleep(2) count = count - 1 round = round - 1 return None
def set_active_window(hwnd=None): logger.debug("edcm.windows.set_active_window(hwnd=%s)" % hwnd) if hwnd: win32gui.SetForegroundWindow(hwnd) win32gui.BringWindowToTop(hwnd) win32gui.SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE) sleep(0.2)
def BringToTop(self): """ Brings the window to the front. :return: None :rtype: None """ self.AssertAlive() win32gui.BringWindowToTop(self.hwnd)
def activate(self, is_true=True): '''激活当前窗口 :param is_true: 是否激活,默认为True :type is_true: bool ''' # win32gui.SetWindowPos(self._parent_wnd,win32con.HWND_TOPMOST,0,0,0,0,win32con.SWP_NOSIZE|win32con.SWP_NOMOVE) self._window.TopLevelWindow.bringForeground() win32gui.BringWindowToTop(self._window.TopLevelWindow.HWnd)
def set_top_window(hwnd): """ @param hwnd HWND @return bool """ try: return bool(win32gui.BringWindowToTop(hwnd)) except: return False
def Find_User(name): a = win32gui.FindWindow(None, "카카오톡") win32gui.BringWindowToTop(a) win32gui.SetForegroundWindow(a) pyautogui.hotkey('ctrl', 'f') pyperclip.copy(name) pyautogui.hotkey('ctrl', 'v') pyautogui.press("down") pyautogui.press("enter")
def set_wind_pos(): global hwnd for wind in get_top_windows(): if win32gui.GetWindowText(wind).find('- 工作台') > -1: hwnd = wind # 置顶并且获得焦点 win32gui.BringWindowToTop(hwnd) win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE) win32gui.SetForegroundWindow(hwnd)
def activate_window(hwnd): global Shell Shell.SendKeys('%') pid = win32process.GetWindowThreadProcessId(hwnd)[1] windll.user32.AllowSetForegroundWindow(pid) win32gui.ShowWindow(hwnd, win32con.SW_SHOW) win32gui.BringWindowToTop(hwnd) win32gui.SetActiveWindow(hwnd) windll.user32.SwitchToThisWindow(hwnd, 1) win32gui.SetForegroundWindow(hwnd)
def exit_tim(self): """ 发送完毕正常退出 :return: """ win32gui.BringWindowToTop(self.hwnd) self.main_dm.MoveTo(40, 35) self.main_dm.LeftClick() self.main_dm.MoveTo(83, 479) self.main_dm.LeftClick()
def __set_foreground(self, hwnd): if win32gui.GetForegroundWindow() == hwnd: return win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE) time.sleep(1) # win32gui.ShowWindow(hwnd, win32con.SW_NORMAL) win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE) win32gui.BringWindowToTop(hwnd) win32gui.SetForegroundWindow(hwnd) time.sleep(1) return
def set_window_active(self): name = Config.read('window.name') h = win32gui.FindWindow(None, name) win32gui.BringWindowToTop(h) try: win32gui.SetForegroundWindow(h) except: import traceback traceback.print_exc() self.rect = win32gui.GetWindowRect(h)
def goToWindow(self, hwnd): windowPlacement = win32gui.GetWindowPlacement(hwnd) showCmd = windowPlacement[1] if showCmd == SW_RESTORE: win32gui.ShowWindow(hwnd, SW_RESTORE) else: win32gui.BringWindowToTop(hwnd) win32gui.SetForegroundWindow(hwnd) win32gui.SetActiveWindow(hwnd)
def set_game_window_in_focus(self): handle = win32gui.FindWindow(None, "Rocket League (32-bit, DX9, Cooked)") if handle is None: return try: win32gui.BringWindowToTop(handle) press('alt') win32gui.SetForegroundWindow(handle) except Exception as e: print("While trying to focus game window thre exception:", str(e))
def window_set_foreground(hwnd): """Bring a window to the foreground and activate it. :param hwnd: Hwnd of window """ try: win32gui.SetForegroundWindow(hwnd) win32gui.BringWindowToTop(hwnd) win32gui.SetActiveWindow(hwnd) except Exception as ex: logger.warning(ex)
def set_window_foreground_top(hwnd): # shell = win32com.client.Dispatch("WScript.Shell") # shell.SendKeys('%') try: win32gui.SendMessage(hwnd, win32con.WM_SYSCOMMAND, win32con.SC_RESTORE, 0) win32gui.SetForegroundWindow(hwnd) win32gui.BringWindowToTop(hwnd) time.sleep(window_manage_delay_time) return True except: return False
def setActiveCallback(hwnd, wildcard): '''check all windows ''' global game_window_rect if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None: win32gui.BringWindowToTop(hwnd) # prevent this error:pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available') shell = win32com.client.Dispatch("WScript.Shell") shell.SendKeys('%') # set foreground window win32gui.SetForegroundWindow(hwnd) game_window_rect = win32gui.GetWindowRect(hwnd)
def _goToWindow(self, hwnd): # Bascially copied from old Go-Y plugin windowPlacement = win32gui.GetWindowPlacement(hwnd) showCmd = windowPlacement[1] if showCmd == SW_RESTORE: win32gui.ShowWindow(hwnd, SW_RESTORE) else: win32gui.BringWindowToTop(hwnd) win32gui.SetForegroundWindow(hwnd) win32gui.SetActiveWindow(hwnd)
def set_focus_on_window(hwnd): """Bring a window to the foreground :param hwnd: hwnd of window """ if window_exist_by_hwnd(hwnd): win32gui.SetForegroundWindow(hwnd) win32gui.BringWindowToTop(hwnd) win32gui.SetActiveWindow(hwnd) return True else: logger.debug('Could not find the HWND: {}'.format(hwnd)) return False
def move_window_foreground(hwnd, name=""): try: win32gui.BringWindowToTop(hwnd) shell = win32com.client.Dispatch("WScript.Shell") shell.SendKeys('%') win32gui.SetForegroundWindow(hwnd) except Exception as e: if name != "": warn_print(f"Open {name}: {e}") else: warn_print(e) win32gui.ShowWindow(hwnd, win32con.SW_NORMAL)
def window_enum_callback(self, hwnd, wildcard): """ 窗口置顶 :param hwnd: :param wildcard: :return: """ if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None: win32gui.BringWindowToTop(hwnd) # 先发送一个alt事件,否则会报错导致后面的设置无效:pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available') shell = win32com.client.Dispatch("WScript.Shell") shell.SendKeys('%') # 设置为当前活动窗口 win32gui.SetForegroundWindow(hwnd)
def RaiseWindowNamed(nameRe): # start by getting a list of all the windows: cb = lambda x, y: y.append(x) wins = [] win32gui.EnumWindows(cb, wins) # now check to see if any match our regexp: tgtWin = -1 for win in wins: txt = win32gui.GetWindowText(win) if nameRe.match(txt): tgtWin = win break if tgtWin >= 0: win32gui.ShowWindow(tgtWin, 1) win32gui.BringWindowToTop(tgtWin)