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
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)
win32gui.ShowWindow(HWND, win32con.SW_RESTORE)
, it now works in all situations .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".
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'))