Pythonでウィンドウ制御!ハンドル取得から操作まで

Pythonでは、pyautoguiライブラリを使用してウィンドウハンドルを取得し、ウィンドウを制御することができます。以下は、現在のウィンドウハンドルを取得し、ウィンドウの位置やサイズを制御する方法を示すサンプルコードです:

import pyautogui

# 获取当前活动窗口句柄
handle = pyautogui.getActiveWindow()

# 获取窗口的位置和大小
x, y, width, height = pyautogui.getWindowGeometry(handle)

# 移动窗口到指定位置
new_x = 100
new_y = 100
pyautogui.moveWindow(handle, new_x, new_y)

# 调整窗口大小
new_width = 800
new_height = 600
pyautogui.resizeWindow(handle, new_width, new_height)

この例では、まずpyautogui.getActiveWindow()を使用して現在アクティブなウィンドウのハンドルを取得し、次にpyautogui.getWindowGeometry(handle)を使用してウィンドウの位置とサイズを取得します。それから、pyautogui.moveWindow()とpyautogui.resizeWindow()関数を使用してウィンドウを移動したりサイズを調整したりします。

pyautoguiライブラリは、pipを使用してインストールする必要があります。以下のコマンドを使用してインストールできます:

pip install pyautogui
bannerAds