网络编程
位置:首页>> 网络编程>> Python编程>> python中用ctypes模拟点击的实例讲解

python中用ctypes模拟点击的实例讲解

作者:小妮浅浅  发布时间:2023-10-29 23:18:42 

标签:python,ctypes,模拟点击

在小编学习python中的模拟点击之前,我们想要对某一项操作进行自动指令的重复,可以选择大家熟知的按键精灵。那么对比python的模拟点击,小编还是觉得python中使用更加方便。这样说不能让有些小伙伴信服,下面小编就以一个以小游戏为例,在我们写完ctypes模拟点击后用python运行,看看游戏体验效果。

按键精灵提供的窗口api性能并不算的上太好。但是将整个逻辑搬到python上,并提供了自己所写的api后,速度有了很大的提升。

直接用python调用,获取特定点位置上的颜色,非白色就发送点击指令。然后循环等待下一个黑色块的到来。同时设定定时时间,若长时间依旧是这个颜色,证明游戏结束,直接退出。代码如下:


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 文件扩展名。

来源:https://www.py.cn/jishu/jichu/21290.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com