Skip to content

Instantly share code, notes, and snippets.

@jerblack
Last active May 2, 2023 13:26
Show Gist options
  • Save jerblack/2b294916bd46eac13da7d8da48fcf4ab to your computer and use it in GitHub Desktop.
Save jerblack/2b294916bd46eac13da7d8da48fcf4ab to your computer and use it in GitHub Desktop.
Find and move windows in Windows with ctypes and user32 in Python
import ctypes
user32 = ctypes.windll.user32
# get screen resolution of primary monitor
res = (user32.GetSystemMetrics(0), user32.GetSystemMetrics(1))
# res is (2293, 960) for 3440x1440 display at 150% scaling
user32.SetProcessDPIAware()
res = (user32.GetSystemMetrics(0), user32.GetSystemMetrics(1))
# res is now (3440, 1440) for 3440x1440 display at 150% scaling
# get handle for Notepad window
# non-zero value for handle should mean it found a window that matches
handle = user32.FindWindowW(u'Notepad', None)
# or
handle = user32.FindWindowW(None, u'Untitled - Notepad')
# meaning of 2nd parameter defined here
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx
# minimize window using handle
user32.ShowWindow(handle, 6)
# maximize window using handle
user32.ShowWindow(handle, 9)
# move window using handle
# MoveWindow(handle, x, y, height, width, repaint(bool))
user32.MoveWindow(handle, 100, 100, 400, 400, True)
@shakilofficial0
Copy link

thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment