qshinoの日記

Powershell関係と徒然なこと

python ctypes フォアグラウンド・ウインドウの操作

python ctypes win

cのdllをpythonから呼び出せる。cdllがデフォルトで定義されている。libc=cdll.msvcrt , libc=CDLL('lbc.so.6')等でインスタンス生成

winの場合は、windll, oledllも追加で定義される。

kernel32, user32。64bitでもファイル名が32なのは気になる。

コード

from ctypes import *

user32 = 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)