Last active
May 2, 2023 13:26
-
-
Save jerblack/2b294916bd46eac13da7d8da48fcf4ab to your computer and use it in GitHub Desktop.
Find and move windows in Windows with ctypes and user32 in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you.