在www.887551.com学习python中的模拟点击之前,我们想要对某一项操作进行自动指令的重复,可以选择大家熟知的按键精灵。那么对比python的模拟点击,www.887551.com还是觉得python中使用更加方便。这样说不能让有些小伙伴信服,下面www.887551.com就以一个以小游戏为例,在我们写完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 文件扩展名。

到此这篇关于python中用ctypes模拟点击的实例讲解的文章就介绍到这了,更多相关python中如何用ctypes模拟点击内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!