窗口操作
Ctypes
from ctypes import windll, byref
from ctypes.wintypes import RECT, HWND
SetWindowPos = windll.user32.SetWindowPos
GetClientRect = windll.user32.GetClientRect
GetWindowRect = windll.user32.GetWindowRect
EnableWindow = windll.user32.EnableWindow
SWP_NOSIZE = 0x0001
SWP_NOMOVE = 0X0002
SWP_NOZORDER = 0x0004
查找窗体
# 获取前景窗口句柄
hwnd = windll.user32.FindWindowW(None, "一梦江湖")
# 查找指定窗口
def find_window(class_name:str=None, window_text:str="") -> int:
hwnd = windll.user32.FindWindowW(class_name, window_text)
if hwnd:
return hwnd
return -1
移动窗体
def move_window(handle: HWND, x: int, y: int):
"""移动窗口到坐标(x, y)
Args:
handle (HWND): 窗口句柄
x (int): 横坐标
y (int): 纵坐标
"""
SetWindowPos(handle, 0, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER)
win32api
import win32con,win32gui,win32api
from win32con import WM_INPUTLANGCHANGEREQUEST
查找窗口
# 获取前景窗口句柄
hwnd = win32gui.GetForegroundWindow()
# 设置某个句柄为前景
win32gui.SetForegroundWindow(hwnd)
# 获取前景窗口标题
title = win32gui.GetWindowText(hwnd)
# 查找指定窗口
def find_window(class_name:str=None, window_text:str="") -> int:
hwnd = win32gui.FindWindow(class_name, window_text)
if hwnd:
return hwnd
return -1
枚举所有窗口
def enum_windows() -> List[int]:
hwnd_list:List[int] = []
win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hwnd_list)
return hwnd_list
获取键盘布局列表
im_list = win32api.GetKeyboardLayoutList()
im_list = list(map(hex, im_list))
print(im_list)
oldKey = hex(win32api.GetKeyboardLayout())
查找指定窗口
hwnd = win32gui.FindWindow(None,'a.txt - 记事本')
查找指定窗口或子窗口
# 查找指定窗口
def find_window(class_name:str=Optional[str], window_text:Optional[str], parent_hwnd:Optional[int]) -> int:
if parent_hwnd == None:
hwnd = win32gui.FindWindow(class_name, window_text)
else:
hwnd = win32gui.FindWindowEx(parent_hwnd, None, class_name, window_text)
if hwnd:
return hwnd
return -1
获取窗口相关信息
class Hwnd():
info = None
def __init__(self, hwnd:int):
self.hwnd = hwnd
self.info = Hwnd.get_info_by_hwnd(hwnd)
self.debug()
def debug(self) -> bool:
"""
@Description 一下几种情况返回 `False`
- 1、查找窗口信息失败
- 2、目标窗体最小化
returns `{bool}` {description}
"""
if not self.info:return False
if self.info['context_width'] == 0 or self.info['context_height'] == 0 :
print('\nWaring: 窗体 [{}] 可能被最小化'.format(self.hwnd), end='\n\n')
return False
return True
@staticmethod
def get_info_by_hwnd(hwnd:int) -> dict:
"""
@Description 返回当前句柄的基本信息
- param hwnd :{int} 窗口句柄
@returns `{dict}` {description}
```python
{
"hwnd":窗口句柄,
"window_width":完整窗体宽度,
"window_height":完成窗体高度,
'context_width':真实内容区宽度,
'context_height':真实内容去高度,
'border':左右下边宽,一般只有上边宽不一样,
'border_top':上边宽高度,
'window_left':窗体位置,
'window_top':int,
'window_right':int,
'window_bottom':int
}
"""
window_left, window_top, window_right, window_bottom = win32gui.GetWindowRect(hwnd)
window_width = window_right - window_left
window_height = window_bottom - window_top
context_left, context_top, context_right, context_bottom = win32gui.GetClientRect(hwnd)
context_width = context_right - context_left
context_height = context_bottom - context_top
border = (window_width - context_width) // 2
border_top = (window_height - context_height) - border
result = {
"hwnd":hwnd,
"window_width":window_width,
"window_height":window_height,
'context_width':context_width,
'context_height':context_height,
'border':border,
'border_top':border_top,
'window_left':window_left,
'window_top':window_top,
'window_right':window_right,
'window_bottom':window_bottom,
}
return result
if ( __name__ == "__main__"):
hwnd =
test = Hwnd('')
获取窗口class_name
、window_text
def get_window_info(hwnd:int) -> dict:
class_name = win32gui.GetClassName(hwnd)
window_text = win32gui.GetWindowText(hwnd)
return {
'class_name':class_name,
"window_text":window_text
}