# 通过窗口类名、窗口标题获取控件句柄
hwnd = win32gui.FindWindow("ClassName", "TitleName")
# 通过句柄设置控件内容
win32gui.SendMessage(hwnd , 12, 0, '文本内容')
# 修改窗口标题
SetWindowText(hwnd, "Title")
#通过句柄获取子窗口句柄
ch_hwnd = win32gui.FindWindowEx(hwnd, 0, "ClassName", "TitleName")
# 获取所有子窗口
hwndChildList = []
win32gui.EnumChildWindows(hwnd, lambda hwnd, param: param.append(hwnd), hwndChildList)
print(hwndChildList)
# 模糊查询句柄
def FindWinHwnd(title, top=True):
titles = []
def foo(hwnd, mouse):
if top:
if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):
if title in GetWindowText(hwnd):
titles.append(hwnd)
else:
if title in GetWindowText(hwnd):
titles.append(hwnd)
EnumWindows(foo, 0)
if titles:
return titles[0]
else:
return 0
# 截图
def window_api_capture(file, hWnd=0):
if hWnd == 0:
MoniterDev = win32api.EnumDisplayMonitors(None, None)
width = MoniterDev[0][2][2]
height = MoniterDev[0][2][3]
else:
left, top, right, bot = win32gui.GetWindowRect(hWnd)
width = right - left
height = bot - top
# 返回句柄窗口的设备环境,覆盖整个窗口,包括非客户区,标题栏,菜单,边框
hWndDC = win32gui.GetWindowDC(hWnd)
# 创建设备描述表
mfcDC = win32ui.CreateDCFromHandle(hWndDC)
# 创建内存设备描述表
saveDC = mfcDC.CreateCompatibleDC()
# 创建位图对象准备保存图片
saveBitMap = win32ui.CreateBitmap()
# 为bitmap开辟存储空间
saveBitMap.CreateCompatibleBitmap(mfcDC, width, height)
# 将截图保存到saveBitMap中
saveDC.SelectObject(saveBitMap)
# 保存bitmap到内存设备描述表
saveDC.BitBlt((0, 0), (width, height), mfcDC, (0, 0), win32con.SRCCOPY)
try:
saveBitMap.SaveBitmapFile(saveDC, file)
except:
pass
win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hWnd, hWndDC)
网友评论