时间:2021-04-15 python教程 查看: 791
python博客下载spy++:
Microsoft Spy++ V15.0.26724.1 简体中文绿色版 64位
拖住中间的“寻找工具”放到想要定位的软件上,然后松开
以微信为例,我们会得到“微信”这个窗口的句柄,为“00031510”,注意这个句柄是“十六进制”,即“0x31510”。
点击ok我们会看到更详细的属性信息
同理拖放到“微信”上,获取到“微信”的界面
点击ok,会直接定位到“微信”
在这里我们会看到一条信息00031510 “微信” WeChatMainWndForPC
– 00031510:代表十六进制的窗口句柄
– 微信:代表窗口标题
– WeChatMainWndForPC:代表窗口的类名
导入必要的库
import win32con
from win32 import win32gui
from win32 import win32clipboard as w
import pyautogui
import sys
def show_window_attr(hwnd):
"""
显示窗口的属性
:param hwnd: 窗口句柄(十进制)
:return: 所有的属性
WindowName: 窗口标题
ClassName: 窗口类名
HwndPy: 窗口句柄(十进制)
HwndSpy: 窗口句柄(十六进制)
"""
if not hwnd:
return
WindowName = win32gui.GetWindowText(hwnd)
ClassName = win32gui.GetClassName(hwnd)
HwndPy = hwnd
HwndSpy = hex(hwnd)
return (WindowName, ClassName, HwndPy, HwndSpy)
已经知道“微信”十六进制的窗口句柄,先转化为十进制,可获取全部属性
>>> int(0x31510)
202000
>>> show_window_attr(202000)
('微信', 'WeChatMainWndForPC', 202000, '0x31510')
def show_top_windows():
"""
列出所有的顶级窗口及属性
:return: 全部的顶层窗口及对应属性
"""
hwndList = []
win32gui.EnumWindows(lambda hwnd, param: param.append(show_window_attr(hwnd)), hwndList)
return hwndList
获取到的是整个桌面所有的窗口
>>> show_top_windows()
[('CClipboardThread', 'CClipboardThread', 2165890, '0x210c82'), ('G', 'GDI+ Hook Window Class', 463410, '0x71232'), ('', 'ForegroundStaging', 66338, '0x10322'), ('', 'ForegroundStaging', 66294, '0x102f6'), ('', 'tooltips_class32', 66234, '0x102ba'), ('', 'tooltips_class32', 66204, '0x1029c'), ('', 'tooltips_class32', 66200, '0x10298'), ('', 'tooltips_class32', 66196, '0x10294'), ('', 'tooltips_class32', 66192, '0x10290'), ('', 'tooltips_class32', 66174, '0x1027e'), ('', 'tooltips_class32', 66166, '0x10276'), ('', 'tooltips_class32', 66154, '0x1026a'), ('', 'tooltips_class32', 66888, '0x10548'), ('', 'tooltips_class32', 131762, '0x202b2'), ('', 'Q360NetmonClass', 197502, '0x3037e'), ('', 'tooltips_class32', 66208, '0x102a0'), ('', 'tooltips_class32', 11404742, '0xae05c6'), ('', 'tooltips_class32', 66214, '0x102a6'), ('', 'tooltips_class32', 66228, '0x102b4'), ('', 'tooltips_class32', 66222, '0x102ae'),
def FindFuzzyTopWindow(FuzzyWindowName=None):
"""
根据标题模糊查找全部符合条件的主窗体
:param FuzzyWindowName: 窗口标题部分文字
:return:
"""
all_windows = show_top_windows()
result = []
for window in all_windows:
if FuzzyWindowName in window[0]:
result.append(window)
return result
>>> FindFuzzyTopWindow(FuzzyWindowName='同花顺')
[('同花顺(v8.80.80) - 我的板块', 'Afx:400000:b:10003:6:d60ca7',
134184, '0x20c28'), ('同花顺股灵通', '#32770', 462652, '0x70f3c')]
def FindSubHandles(pHandle=None, ClassName=None, WinName=None, index=None):
"""
返回窗体下全部的子窗体,默认主窗体下的窗体
:param pHandle: 窗口句柄(十进制)
:param ClassName: 窗口类名,返回特定类名
:param WinName: 窗口标题,返回特定标题
:param index: 位置,返回特定位置的窗口
:return: 包含属性的全部子窗口
"""
num = 0
handle = 0
SubHandlesList = []
while True:
# find next handle, return HwndPy
handle = win32gui.FindWindowEx(pHandle, handle, ClassName, WinName)
if handle == 0:
# no more handle
break
# get handle attribution
attr = show_window_attr(handle)
# append to list
SubHandlesList.append(tuple(list(attr) + [num]))
num += 1
if index is not None:
return SubHandlesList[index]
else:
return SubHandlesList
>>> FindSubHandles(pHandle=134184)
[('', 'Button', 69090, '0x10de2', 0),
('', 'ToolbarWindow32', 69272, '0x10e98', 1),
('', 'msctls_statusbar32', 265490, '0x40d12', 2),
('', 'msctls_statusbar32', 134664, '0x20e08', 3),
('', 'AfxFrameOrView42s', 134212, '0x20c44', 4),
('', 'AfxControlBar42s', 134180, '0x20c24', 5),
('', 'AfxControlBar42s', 134192, '0x20c30', 6),
('功能树', 'AfxControlBar42s', 134194, '0x20c32', 7),
('', 'AfxControlBar42s', 134196, '0x20c34', 8),
('HqEmbededTradeContainer', 'Afx:400000:0', 69270, '0x10e96', 9),
('功能树', 'Afx:400000:8:10003:10006e:0', 69320, '0x10ec8', 10),
('', 'Afx:400000:0', 69430, '0x10f36', 11),
('', 'Afx:400000:0', 69432, '0x10f38', 12)]
对比SPY++中的结果完全一致
下面全部的子窗口,遍历所有窗口 这里获取到的是全部层级的子窗口
def ShowAllHandle(pHandle=None, HandleList=[[None]], HandleDict=dict()):
"""
生成窗口全部对应的关系
:param pHandle: 目标父窗口
:param HandleList: 默认为[[None]]
:param HandleDict: 用于存放对应关系
:return: 返回目标窗口下全部子父窗口的字典
"""
sys.setrecursionlimit(1000000)
if pHandle:
HandleList[-1][0] = pHandle
handles = FindSubHandles(HandleList[-1][0][2])
else:
handles = FindSubHandles()
for handle in handles:
HandleDict[handle] = pHandle
# 这个根节点已经遍历完,删除
del HandleList[-1][0]
# 如果有叶节点,非空,则加入新的叶节点
if handles:
HandleList.append(handles)
# 删除已被清空的根
HandleList = [HandleGroup for HandleGroup in HandleList if HandleGroup]
# 如果还有根就继续遍历,否则输出树
if HandleList:
return ShowAllHandle(pHandle=HandleList[-1][0], HandleList=HandleList, HandleDict=HandleDict)
else:
return HandleDict
pHandle:这个参数设置为
('同花顺(v8.80.80) - 我的板块', 'Afx:400000:b:10003:6:d60ca7', 134184, '0x20c28')
调用函数
ShowAllHandle(FindFuzzyTopWindow(FuzzyWindowName='同花顺')[0])
得到一个全部对应关系的字典
因为句柄在不同电脑上的结果是不同的,所以我们要找到这个唯一的路
def FindHandlePath(TargetHandle, num):
"""
寻找特定窗口的寻找路径
:param TargetHandle: 窗口句柄(十六进制)
:param num: 窗口所属index,在spy++内查看
:return:
ParentWindow:顶层窗口
TargetPath:路径的index
"""
AllPath = ShowAllHandle(pHandle=None, HandleList=[[None]], HandleDict=dict())
key = tuple(list(show_window_attr(int(TargetHandle))) + [num])
handlepath = [key]
while True:
key = AllPath[key]
if not key:
handlepath = handlepath[::-1]
ParentWindow = handlepath[0]
TargetPath = [(i[-1]) for i in handlepath[1:]]
return ParentWindow, TargetPath
handlepath.append(key)
我们得到其十六进制句柄为“0x40D98”,同时index为6
>>> FindHandlePath(TargetHandle=0x40D98, num=6)
(('同花顺(v8.80.80) - 我的板块', 'Afx:400000:b:10003:6:d60ca7', 134184, '0x20c28', 308),
[5, 0, 6])
def FindTargetHandle(pHandle, WindowList):
"""
递归寻找子窗口的句柄
:param pHandle: 祖父窗口的完整句柄 (WindowName, ClassName, HwndPy, HwndSpy)
:param WindowList: 子窗口列表
:return: 目标窗口的完整属性
"""
for i in range(len(WindowList)):
pHandle = FindSubHandles(pHandle[2], index=WindowList[i])
return pHandle
(('同花顺(v8.80.80) - 我的板块', 'Afx:400000:b:10003:6:d60ca7', 134184, '0x20c28', 308), [5, 0, 6])
>>> FindTargetHandle(('同花顺(v8.80.80) - 我的板块', 'Afx:400000:b:10003:6:d60ca7', 134184, '0x20c28', 308), [5, 0, 6])
('', 'Button', 265624, '0x40d98', 6)
>>> x,y,m,n = win32gui.GetWindowRect(265624)
>>> pyautogui.moveTo((x+m)/2, (y+n)/2)
此时鼠标会自动移动到这个窗体上
参考链接:
https://blog.csdn.net/qq_25408423/article/details/80884114
https://blog.csdn.net/seele52/article/details/17504925
到此这篇关于python中spy++的使用超详细教程的文章就介绍到这了,更多相关python中spy++使用内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!