python枚举窗口句柄_python和pywin32实现窗口查找、遍历和点击

本文介绍了如何利用Python的Pywin32库来查找窗口句柄,根据类名和标题获取窗口位置和大小,遍历子窗口,执行鼠标点击操作以及模拟键盘输入,最后演示了关闭窗口的方法。
摘要由CSDN通过智能技术生成

Pywin32是一个Python库,为python提供访问Windows API的扩展,提供了齐全的windows常量、接口、线程以及COM机制等等。

1.通过类名和标题查找窗口句柄,并获得窗口位置和大小

import win32gui

import win32api

classname = "MozillaWindowClass"

titlename = "百度一下,你就知道 - Mozilla Firefox"

#获取句柄

hwnd = win32gui.FindWindow(classname, titlename)

#获取窗口左上角和右下角坐标

left, top, right, bottom = win32gui.GetWindowRect(hwnd)

2.通过父句柄获取子句柄

def get_child_windows(parent):

'''

获得parent的所有子窗口句柄

返回子窗口句柄列表

'''

if not parent:

return

hwndChildList = []

win32gui.EnumChildWindows(parent, lambda hwnd, param: param.append(hwnd), hwndChildList)

return hwndChildList

#获取某个句柄的类名和标题

title = win32gui.GetWindowText(hwnd)

clsname = win32gui.GetClassName(hwnd)

<
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值