Python Window Handles: Control with PyAutoGUI

You can use the pyautogui library in Python to obtain window handles and control windows. Below is an example code showing how to retrieve the current window handle and control the window’s position and size.

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)

In this example, we begin by using pyautogui.getActiveWindow() to obtain the handle of the current active window, then use pyautogui.getWindowGeometry(handle) to retrieve the position and size of the window. Next, we utilize the functions pyautogui.moveWindow() and pyautogui.resizeWindow() to move and resize the window.

Please note that the pyautogui library needs to be installed using pip. You can use the following command to install it:

pip install pyautogui
bannerAds