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

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

在小编学习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

标签:python,ctypes,模拟点击
0
投稿

猜你喜欢

  • Python复制Word内容并使用格式设字体与大小实例代码

    2023-01-10 05:48:20
  • Tkinter组件Entry的具体使用

    2023-03-21 00:41:40
  • eWebEditor_v280_Free_Final最好用的网页编辑器下载

    2022-05-26 22:30:24
  • Python中的进程操作模块(multiprocess.process)

    2022-09-17 23:10:32
  • Python利用3D引擎制作一个3D迷宫游戏

    2021-02-18 21:17:54
  • Python入门学习之类的相关知识总结

    2021-12-18 10:02:38
  • 详解django使用include无法跳转的解决方法

    2023-04-08 06:13:54
  • python实现简单名片管理系统

    2023-06-13 08:03:12
  • XPath 11个实例

    2008-09-05 15:06:00
  • 你的网站使用了微格式了么

    2009-05-21 12:10:00
  • 使用python实现时间序列白噪声检验方式

    2023-11-13 23:06:53
  • python可视化之颜色映射详解

    2021-01-27 01:23:52
  • JS获取网页图片name属性的方法

    2024-04-28 09:46:19
  • Prometheus开发中间件Exporter过程详解

    2023-04-18 16:14:13
  • Python Pandas中DataFrame.drop_duplicates()删除重复值详解

    2021-11-10 09:55:20
  • pandas DataFrame运算的实现

    2021-06-02 21:08:22
  • 实例讲解Python脚本成为Windows中运行的exe文件

    2023-07-15 02:14:31
  • openCV入门学习基础教程第三篇

    2022-05-20 00:00:59
  • Mysql中LAST_INSERT_ID()的函数使用详解

    2024-01-16 06:50:32
  • 用Python实现简单的人脸识别功能步骤详解

    2022-05-15 15:52:00
  • asp之家 网络编程 m.aspxhome.com