Skip to content

Instantly share code, notes, and snippets.

@amnweb
Created October 14, 2024 14:37
Show Gist options
  • Save amnweb/0ba26d07b654da291db2a2c33df652a9 to your computer and use it in GitHub Desktop.
Save amnweb/0ba26d07b654da291db2a2c33df652a9 to your computer and use it in GitHub Desktop.
import win32gui
import win32api
import win32con
import time
import keyboard
def get_window_handle(window_title):
def callback(hwnd, extra):
if window_title in win32gui.GetWindowText(hwnd):
extra.append(hwnd)
hwnds = []
win32gui.EnumWindows(callback, hwnds)
return hwnds[0] if hwnds else None
def move_window():
window_title = "amn" # For test I will use the title of the window instead of the PID or the process name
hwnd = get_window_handle(window_title)
if not hwnd:
print(f"{window_title} is not open.")
return
while True:
if keyboard.is_pressed('alt'):
if win32api.GetKeyState(win32con.VK_LBUTTON) < 0:
rect = win32gui.GetWindowRect(hwnd)
initial_mouse_pos = win32api.GetCursorPos()
initial_window_pos = (rect[0], rect[1])
while keyboard.is_pressed('alt') and win32api.GetKeyState(win32con.VK_LBUTTON) < 0:
current_mouse_pos = win32api.GetCursorPos()
dx = current_mouse_pos[0] - initial_mouse_pos[0]
dy = current_mouse_pos[1] - initial_mouse_pos[1]
print(dx, dy)
new_window_pos = (initial_window_pos[0] + dx, initial_window_pos[1] + dy)
win32gui.MoveWindow(hwnd, new_window_pos[0], new_window_pos[1], rect[2] - rect[0], rect[3] - rect[1], True)
time.sleep(0.01)
time.sleep(0.1)
if __name__ == "__main__":
move_window()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment