这篇文章给大家介绍怎么在python中利用ctypes模拟一个点击事件,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
代码如下:
WindowFunction = ctypes.windll.LoadLibrary("E:\\Python Hack\\DLL\\ScreenFunction.dll")
DllGetPixel = WindowFunction.GetWindowPixel
DllGetPixel.argtypes=[ctypes.wintypes.HWND,ctypes.wintypes.c_int,ctypes.wintypes.c_int]
DllGetPixel.restypes=[ctypes.wintypes.c_uint32]
DllGetMultiPixel = WindowFunction.GetWindowMultiPixel
DllGetMultiPixel.argtypes=[ctypes.wintypes.HWND,ctypes.wintypes.c_void_p,ctypes.wintypes.c_void_p]
DllGetMultiPixel.restypes=[ctypes.wintypes.c_int]
cMulti = (ctypes.wintypes.c_int * 17)(Pos0.x,Pos0.y,Pos1.x,Pos1.y,Pos2.x,Pos2.y,Pos3.x,Pos3.y,
Pos0.x,Pos0.y-5,Pos1.x,Pos1.y-5,Pos2.x,Pos2.y-5,Pos3.x,Pos3.y-5,
0)
dwLen = DllGetMultiPixel(wHWND,byref(cMulti),None)
RGB = (ctypes.wintypes.DWORD * dwLen)()
quit = False
while not quit:
DllGetMultiPixel(wHWND,byref(cMulti),byref(RGB))
flag = 0
if not RGB[0] == 0xfff5f5f5 or not RGB[4] == 0xfff5f5f5:
EmuCursorClick(rect.left+Pos0.x,rect.top+Pos0.y)
flag = 1
elif not RGB[1] == 0xfff5f5f5 or not RGB[5] == 0xfff5f5f5:
EmuCursorClick(rect.left+Pos1.x,rect.top+Pos1.y)
flag = 2
elif not RGB[2] == 0xfff5f5f5 or not RGB[6] == 0xfff5f5f5:
EmuCursorClick(rect.left+Pos2.x,rect.top+Pos2.y)
flag = 3
elif not RGB[3] == 0xfff5f5f5 or not RGB[7] == 0xfff5f5f5:
EmuCursorClick(rect.left+Pos3.x,rect.top+Pos3.y)
flag = 4
cot = 0
if flag == 0:
quit=True
elif flag == 1:
RGB0 = DllGetPixel(wHWND,Pos0.x,Pos0.y) & 0xffffffff
while not RGB0 == 0xfff5f5f5:
time.sleep(0.05)
cot += 1
if cot > 20:
quit=True
break
RGB0 = DllGetPixel(wHWND,Pos0.x,Pos0.y) & 0xffffffff
elif flag == 2:
RGB1 = DllGetPixel(wHWND,Pos1.x,Pos1.y) & 0xffffffff
while not RGB1 == 0xfff5f5f5:
break
RGB1 = DllGetPixel(wHWND,Pos1.x,Pos1.y) & 0xffffffff
elif flag == 3:
RGB2 = DllGetPixel(wHWND,Pos2.x,Pos2.y) & 0xffffffff
while not RGB2 == 0xfff5f5f5:
RGB2 = DllGetPixel(wHWND,Pos2.x,Pos2.y) & 0xffffffff
elif flag == 4:
RGB3 = DllGetPixel(wHWND,Pos3.x,Pos3.y) & 0xffffffff
while not RGB3 == 0xfff5f5f5:
RGB3 = DllGetPixel(wHWND,Pos3.x,Pos3.y) & 0xffffffff
print 'end'
ctypes 教程
注意:在本教程中的示例代码使用 doctest 进行过测试,保证其正确运行。由于有些代码在Linux,Windows或Mac OS X下的表现不同,这些代码会在 doctest 中包含相关的指令注解。
注意:部分示例代码引用了 ctypes c_int 类型。在 sizeof(long) == sizeof(int) 的平台上此类型是 c_long 的一个别名。所以,在程序输出 c_long 而不是你期望的 c_int 时不必感到迷惑 --- 它们实际上是同一种类型。
载入动态连接库
ctypes 导出了 cdll 对象,在 Windows 系统中还导出了 windll 和 oledll 对象用于载入动态连接库。
通过操作这些对象的属性,你可以载入外部的动态链接库。cdll 载入按标准的 cdecl 调用协议导出的函数,而 windll 导入的库按 stdcall 调用协议调用其中的函数。 oledll 也按 stdcall 调用协议调用其中的函数,并假定该函数返回的是 Windows HRESULT 错误代码,并当函数调用失败时,自动根据该代码甩出一个 OSError 异常。
在 3.3 版更改: 原来在 Windows 下甩出的异常类型 WindowsError 现在是 OSError 的一个别名。
这是一些 Windows 下的例子。注意:msvcrt 是微软 C 标准库,包含了大部分 C 标准函数,这些函数都是以 cdecl 调用协议进行调用的。
>>> from ctypes import *
>>> print(windll.kernel32)
<WinDLL 'kernel32', handle ... at ...>
>>> print(cdll.msvcrt)
<CDLL 'msvcrt', handle ... at ...>
>>> libc = cdll.msvcrt
>>>
Windows会自动添加通常的 .dll 文件扩展名。
关于怎么在python中利用ctypes模拟一个点击事件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。