def foreground_qt_window(window: "QMainWindow") -> None: """ Try as hard as possible to bring a qt window to the front. This will use pywin32 if installed and running on windows as this seems to be the only reliable way to foreground a window. The build-in qt functions often doesn't work. Note that to use this with pyqtgraphs remote process you should use the ref in that module as in the example below. Args: window: Handle to qt window to foreground. Examples: >>> Qtplot.qt_helpers.foreground_qt_window(plot.win) """ try: import win32con from win32gui import SetWindowPos # use the idea from # https://stackoverflow.com/questions/12118939/how-to-make-a-pyqt4-window-jump-to-the-front SetWindowPos(window.winId(), win32con.HWND_TOPMOST, # = always on top. only reliable way to bring it to the front on windows 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) SetWindowPos(window.winId(), win32con.HWND_NOTOPMOST, # disable the always on top, but leave window at its top position 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) except ImportError: pass window.show() window.raise_() window.activateWindow()
def activateWindow(self): if not self._activationWindow: return self._activationWindow.setWindowState(( self._activationWindow.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive) SetWindowPos( self._activationWindow.winId(), win32con. HWND_TOPMOST, # = always on top. only reliable way to bring it to the front on windows 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) SetWindowPos( self._activationWindow.winId(), win32con. HWND_NOTOPMOST, # disable the always on top, but leave window at its top position 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) # self._activationWindow.raise_() self._activationWindow.activateWindow()
def OpenNewWindow(self, command): from ClickPoints import ClickPointsWindow from includes import LoadConfig global app config = LoadConfig(command) config.srcpath = command window = ClickPointsWindow(config, app) self.setWindowTitle("blabalba") print("ClickPoints started", time.time()-start_new_time, "s") if sys.platform[:3] == 'win': from win32gui import SetWindowPos import win32con SetWindowPos(window.winId(), win32con.HWND_TOPMOST, # = always on top. only reliable way to bring it to the front on windows 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) SetWindowPos(window.winId(), win32con.HWND_NOTOPMOST, # disable the always on top, but leave window at its top position 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) window.raise_() window.show() window.activateWindow() new_window_list = [] for win in self.windows: if not win.isHidden(): new_window_list.append(win) self.windows = new_window_list self.windows.append(window) print(self.windows)
def on_create(self, hwnd, message, wparam, lparam): set_timer(30000, self.on_timer) SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_CHILD) SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TOOLWINDOW) SetWindowPos(self.hwnd, 0, 0, 0, 200, 200, SWP_NOMOVE | SWP_NOZORDER) SetWindowPos(self.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE) SetWindowPos(self.hwnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER) ShowWindow(self.hwnd, SW_SHOW)
def raise_to_top(self): if sys.platform.startswith("win"): import win32con from win32gui import SetWindowPos # set to always-on-top and disable it again. that way windows stays in front flag = win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW win_id = self.winId() SetWindowPos(win_id, win32con.HWND_TOPMOST, 0, 0, 0, 0, flag) SetWindowPos(win_id, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, flag) # state = (self.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive # self.setWindowState(state) self.raise_() self.show() self.activateWindow()
def activateWindow(self): if not self._activationWindow: return self._activationWindow.show() self._activationWindow.setWindowState(( self._activationWindow.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive) SetWindowPos( self._activationWindow.winId(), # = always on top. only reliable way to bring it to the front on windows win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) # Do not reset topmost; for monitor control this is set... # SetWindowPos(self._activationWindow.winId(), # # disable the always on top, but leave window at its top position # win32con.HWND_NOTOPMOST, # 0, 0, 0, 0, # win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) # self._activationWindow.raise_() self._activationWindow.activateWindow()
def hide(self): SetWindowPos(self.hwnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW) data = APPBARDATA() data.cbSize = ctypes.sizeof(APPBARDATA) data.lParam = 1 SHAppBarMessage(ABM_SETSTATE, byref(data)) logging.info('Hide task bar.')
def show(self): SetWindowPos(self.hwnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW) data = APPBARDATA() data.cbSize = ctypes.sizeof(APPBARDATA) data.lParam = 0 SHAppBarMessage(ABM_SETSTATE, byref(data)) logging.info('Show task bar.')
def __call__(self): hwnd = PhonerWin() if hwnd: if self.value == SW_RESTORE: ShowWindowAsync(hwnd, SW_MINIMIZE) ShowWindowAsync(hwnd, SW_RESTORE) SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_ASYNCWINDOWPOS) SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_ASYNCWINDOWPOS) else: SetWindowPos( hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE) ShowWindowAsync(hwnd, SW_MINIMIZE) else: self.PrintError(self.plugin.text.text1) return self.plugin.text.text1
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): self.absolute_mouse_pos = GetCursorPos() self.game_window_position = GetWindowRect(self.game_window_handler) SetWindowPos( self.game_window_handler, HWND_TOP, self.absolute_mouse_pos[0] - self.app_window_move_offset[0], self.absolute_mouse_pos[1] - self.app_window_move_offset[1], self.game_window_position[2] - self.game_window_position[0], self.game_window_position[3] - self.game_window_position[1], SWP_NOREDRAW)
def hideSpotify(self) -> None: self.win.set_transparency(0) ShowWindow(self.win.handle, SW_SHOWNOACTIVATE) sleep(0.1) self.posY = self.base.rectangle().top SetWindowPos(self.win.handle, HWND_BOTTOM, 0, 0, 0, 0, BOTTOM_FLAGS) self.win.move_window(y=-50) if self.win.has_focus(): if self.hideloop > 9: self.hideloop = 0 try: SetForegroundWindow(DESKTOP_HWND) self.hideloop = 0 except: self.hideloop += 1 self.hideSpotify()
def __init__(self, root: "GUI"): self.startApp() self.startTitle = self.getText() root.updateText(self.startTitle) self.showSpotify() self.hideloop = 0 ct = 0 while True: try: if self.win.has_focus() or self.base.has_focus(): break self.win.minimize() sleep(0.1) self.win.restore() SetWindowPos(self.win.handle, HWND_TOP, 0, 0, 0, 0, TOP_FLAGS) except Exception as ex: print(ex) ct += 1 if ct == 10: self.showError() ct = 0 self.showSpotify()
def release_yys_topmost(self): x, y, _, _ = self.get_window_rect() SetWindowPos(self.handle, HWND_TOP, x, y, 1533, 901, SWP_DEFERERASE | SWP_NOREPOSITION)
def set_window_top(): hwnd = GetForegroundWindow() (left, top, right, bottom) = GetWindowRect(hwnd) SetWindowPos(hwnd, win32con.HWND_TOPMOST, left, top, (right - left) + 250, bottom - top, 0)
if GetClassName(h) == 'ExploreWClass': lparams.append(h) winList = [] EnumWindows(enumWinProc, winList) winCnt = len(winList) if winCnt == 0: # No Explorer running os.system('explorer.exe') while 1: try: FindWindow('ExploreWClass', None) #Wait for first instance to run except pywintypes.error, e: pass else: break time.sleep(0.1) # Sleep for a while before continuing os.system('explorer.exe') # Start second instance elif winCnt == 1: os.system('explorer.exe') # Start second instance time.sleep(2) # Wait for Explorer to run winList = [] EnumWindows(enumWinProc, winList) # Get handles of running Explorer hDesk = GetDesktopWindow() (dLeft, dTop, dRight, dBottom) = GetWindowRect(hDesk) # Get desktop size SetWindowPos(winList[0], 0, dRight / 2, 0, dRight / 2, dBottom, 0) # Set the windows sizes SetWindowPos(winList[1], 0, 0, 0, dRight / 2, dBottom, 0) ShowWindow(winList[0], 1) #Show the windows ShowWindow(winList[1], 1)
hwnd = FindWindow(None, "Free Virtual Keyboard (www.FreeVirtualKeyboard.com)") # Used to find the dimensions of the window rect = GetWindowRect(hwnd) rect_x = rect[0] rect_y = rect[1] rect_w = rect[2] - rect_x rect_h = rect[3] - rect_y # print(GetSystemMetrics(0), GetSystemMetrics(1)) is1080p = False if GetSystemMetrics(0) == 1536 and GetSystemMetrics(1) == 864: # if 1080p then... is1080p = True if is1080p: SetWindowPos(hwnd, HWND_TOPMOST, 384, 648, 768, 216, 0) # may need another inside 'on_click' for consistent size else: # 1600x900 position_XY(^^^ / 100)* 96 size_XY (^^^ / 83.33 * 100 not exact? slight adjustment SetWindowPos(hwnd, HWND_TOPMOST, 330, 650, 952, 254, 0) # may need another inside 'on_click' for consistent size # change caps value f def changer(): global caps # need 'global' keyword to change value of global variable caps = (caps + 5) % 10 # lower case keyboard lower_list = [["Esc", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "Del"], ["`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Backspace"],
def on_mousemove(self, hwnd, message, wparam, lparam): if wparam & MK_LBUTTON: x0, y0 = GetCursorPos() SetWindowPos(hwnd, 0, x0 - self._dx, y0 - self._dy, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOSENDCHANGING)
def adjust_yys_resolution(self): "调节阴阳师的分辨率,置顶" SetWindowPos(self.handle, HWND_TOPMOST, 0, 0, 1533, 901, SWP_DEFERERASE | SWP_NOREPOSITION)
def set_window_pos(hwnd, x, y, width, height): try: SetWindowPos(hwnd, HWND_TOP, x, y, width, height, SWP_SHOWWINDOW) except: log_record('не удалось установить окно в нужную позицию')
# Used to find the standard dimensions of the window rect = GetWindowRect(hwnd) rect_x = rect[0] rect_y = rect[1] rect_w = rect[2] - rect_x rect_h = rect[3] - rect_y # print(GetSystemMetrics(0), GetSystemMetrics(1)) is1080p = False if GetSystemMetrics(0) == 1536 and GetSystemMetrics( 1) == 864: # if 1080p then... is1080p = True if is1080p == True: SetWindowPos(hwnd, HWND_TOPMOST, 300, 570, 900, 300, 0) else: SetWindowPos(hwnd, HWND_TOPMOST, 1, 1, 900, 300, 0) # config this # we may need an if for every common resolution def on_click1(x, y, button, pressed): if pressed: # logging.info("Mouse clicked at ===> ({0}, {1})".format(x, y)) dynamic_rect = GetWindowRect( hwnd ) # this needs to stay dynamic for if window is moved, so inside 'on_click' x_window = (dynamic_rect[0] + 8 ) # account for difference between mouse and window xy