Windows 7: how to bring a window to the front no matter what other window has focus?

后端 未结 9 1658
走了就别回头了
走了就别回头了 2020-11-28 08:04

I\'m implementing a task-bar replacement, dock-like application-switcher style program. It\'s doing some unique stuff with OpenGL, and with keyboard shortcuts, so the way it

相关标签:
9条回答
  • 2020-11-28 08:54

    According to nspire, I've tried his solution with python 2.7 and W8, and it works like a charm, even if the window is minimized *.

    win32gui.ShowWindow(HWND, win32con.SW_RESTORE)
    win32gui.SetWindowPos(HWND,win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE + win32con.SWP_NOSIZE)  
    win32gui.SetWindowPos(HWND,win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE + win32con.SWP_NOSIZE)  
    win32gui.SetWindowPos(HWND,win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_SHOWWINDOW + win32con.SWP_NOMOVE + win32con.SWP_NOSIZE)
    
    • Originally it was if the window it's not minimized, but thanks to Whome's comment win32gui.ShowWindow(HWND, win32con.SW_RESTORE), it now works in all situations .
    0 讨论(0)
  • 2020-11-28 08:54

    If you're implementing hotkeys, use RegisterHotKey. As Raymond Chen puts it (companion blog article to the one already linked by Chris), "Pressing a registered hotkey gives you the foreground activation love".

    0 讨论(0)
  • 2020-11-28 09:04

    I don't like these suggestions of using win32gui because you can't easily install that via pip. So here's my solution:

    First, install pywinauto via pip. If you're on Python 2.7.9 or a newer version on the 2 branch, or Python 3.4.0 or a newer version from the 3 branch, pip is already installed. For everyone else, update Python to get it (or you can manually download and install it by running this script, if you must run an older version of Python.)

    Just run this from the command line (not from within Python):

    pip install pywinauto
    

    Next, import what you need from pywinauto:

    from pywinauto.findwindows    import find_window
    from pywinauto.win32functions import SetForegroundWindow
    

    Finally, it's just one actual line:

    SetForegroundWindow(find_window(title='taskeng.exe'))
    
    0 讨论(0)
提交回复
热议问题