diff --git a/StayOnTop.py b/StayOnTop.py index 27654b0..632c9ae 100644 --- a/StayOnTop.py +++ b/StayOnTop.py @@ -3,14 +3,24 @@ import win32gui import win32con -def make_always_on_top(window): - win32gui.SetWindowPos(window._hWnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE) - print(f'Success! {window.title} is now always on top.') +def toggle_always_on_top(window): + # Get the window style + style = win32gui.GetWindowLong(window._hWnd, win32con.GWL_EXSTYLE) + + # Check if the window is currently set as always on top + if style & win32con.WS_EX_TOPMOST: + # If yes, remove the always on top style + win32gui.SetWindowPos(window._hWnd, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE) + print(f'Success! {window.title} is no longer always on top.') + else: + # If not, set the always on top style + win32gui.SetWindowPos(window._hWnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE) + print(f'Success! {window.title} is now always on top.') if __name__ == "__main__": for i in range(3, 0, -1): - print(f"Switch to the window you want to stay on top. Countdown: {i}") + print(f"Switch to the window you want to toggle. Countdown: {i}") time.sleep(1) focused_window = gw.getActiveWindow() - make_always_on_top(focused_window) + toggle_always_on_top(focused_window)