我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用win32con.HWND_TOPMOST。
def show_window(self): self.load() try: from win32gui import SetWindowPos import win32con SetWindowPos(self.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.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 self.raise_() self.showMaximized() self.setWindowState(self.windowState() & ~Qt.WindowMinimized | Qt.WindowActive) self.activateWindow()
def stay_on_bottom(self): """Pin to desktop or something close enough (call repeatedly).""" if self.is_desktop_on_foreground(): if self.topmost is False: win32gui.SetWindowPos( self.hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOSIZE | win32con.SWP_NOMOVE) self.topmost = True else: if self.topmost is True: win32gui.SetWindowPos( self.hwnd, win32con.HWND_BOTTOM, 0, 0, 0, 0, win32con.SWP_NOSIZE | win32con.SWP_NOMOVE) self.topmost = False # "To prevent the window button from being placed on the taskbar, create # the unowned window with the WS_EX_TOOLWINDOW extended style. As an # alternative, you can create a hidden window and make this hidden window # the owner of your visible window. The Shell will remove a window's button # from the taskbar only if the window's style supports visible taskbar # buttons. If you want to dynamically change a window's style to one that # doesn't support visible taskbar buttons, you must hide the window first # (by calling ShowWindow with SW_HIDE), change the window style, and then # show the window." # -- https://msdn.microsoft.com/en-us/library/bb776822%28v=vs.85%29.aspx
def OnInitDialog(self, hwnd, msg, wparam, lparam): self.hwnd = hwnd desktop = win32gui.GetDesktopWindow() dt_l, dt_t, dt_r, dt_b = win32gui.GetWindowRect(desktop) centre_x, centre_y = win32gui.ClientToScreen( desktop, ( (dt_r-dt_l)/2, (dt_b-dt_t)/2) ) bmCtrl = win32gui.GetDlgItem(self.hwnd, IDC_BITMAP) win32gui.SendMessage(bmCtrl, win32con.STM_SETIMAGE, win32con.IMAGE_BITMAP, self.hSplash) win32gui.SetWindowPos(self.hwnd, win32con.HWND_TOPMOST, centre_x-(self.bmWidth/2), centre_y-(self.bmHeight/2), self.bmWidth, self.bmHeight, win32con.SWP_HIDEWINDOW) win32gui.SetForegroundWindow(self.hwnd)
def window_foreground_loop(timeout=20): """ set the windows python console to the foreground (for example when you are working with a fullscreen program) """ hwnd=int(win32console.GetConsoleWindow()) while True: win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0,0,0,0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE) time.sleep(timeout)
def bringToTop(self): if platform.system() == "Windows": self.showNormal() self.raise_() self.activateWindow() try: import win32con import win32gui hwnd = self.effectiveWinId() rect = win32gui.GetWindowRect(hwnd) x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y # mark it "always on top", just for a moment, to force it to the top win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST, x, y, w, h, 0) win32gui.SetWindowPos(hwnd,win32con.HWND_NOTOPMOST, x, y, w, h, 0) except Exception as e: print "Whoops", e elif platform.system() == "Darwin": self.raise_() self.showNormal() self.activateWindow() else: flags = self.windowFlags() self.setWindowFlags( flags | QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.X11BypassWindowManagerHint) QtCore.QCoreApplication.processEvents() #self.show() self.setWindowFlags( flags ) self.show()
def set_window_top(): hwnd = win32gui.GetForegroundWindow() (left, top, right, bottom) = win32gui.GetWindowRect(hwnd) win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, left, top, right - left, bottom - top, 0)
def SetWinCenter(cls, whd): rect = WinUtil.GetCompRect(whd) left = (win32api.GetSystemMetrics(win32con.SM_CXFULLSCREEN)-(rect.right-rect.left))/2; top = (win32api.GetSystemMetrics(win32con.SM_CYFULLSCREEN)-(rect.bottom-rect.top))/2; #Move the window to the correct coordinates with SetWindowPos() cls.SetAsForegroundWindow(whd) win32gui.SetWindowPos(whd, win32con.HWND_TOPMOST, left, top,-1,-1, win32con.SWP_NOSIZE | win32con.SWP_NOZORDER); win32gui.SetWindowPos(whd, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_NOSIZE | win32con.SWP_NOMOVE);
def set_always_on_top(val): global windowsWindow global isOnTop if isWindows: if windowsWindow: rect = win32gui.GetWindowRect(windowsWindow) x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y placement = win32gui.GetWindowPlacement(windowsWindow) if val is True: win32gui.SetWindowPos(windowsWindow, win32con.HWND_TOPMOST, x, y, w, h, placement[1]) isOnTop = True # win32gui.SetFocus(windowsWindow) else: win32gui.SetWindowPos(windowsWindow, win32con.HWND_NOTOPMOST, x, y, w, h, placement[1]) isOnTop = False return True else: print("Window with title 'Google It Up!' doesnt exist") return False elif isMac: # Not working, dont try command = "/usr/bin/osascript -e 'tell app \"System Events\"' " command += "-e 'set theApps to every process whose name contains \"Google It Up!\"' " command += "-e 'set theApp to item 1 of theApps' -e 'set frontmost of theApp to true' " command += "-e 'end tell'" if val == False: command = command.replace('true', 'false') os.system(command)