温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

在python中怎么用ctypes模拟点击

发布时间:2020-11-30 10:13:37 来源:亿速云 阅读:309 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关在python中怎么用ctypes模拟点击,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

按键精灵提供的窗口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:
                time.sleep(0.05)
                cot += 1
                if cot > 20:
                    quit=True
                    break
                RGB1 = DllGetPixel(wHWND,Pos1.x,Pos1.y) & 0xffffffff
        elif flag == 3:
            RGB2 = DllGetPixel(wHWND,Pos2.x,Pos2.y) & 0xffffffff
            while not RGB2 == 0xfff5f5f5:
                time.sleep(0.05)
                cot += 1
                if cot > 20:
                    quit=True
                    break                
                RGB2 = DllGetPixel(wHWND,Pos2.x,Pos2.y) & 0xffffffff
        elif flag == 4:
            RGB3 = DllGetPixel(wHWND,Pos3.x,Pos3.y) & 0xffffffff
            while not RGB3 == 0xfff5f5f5:
                time.sleep(0.05)
                cot += 1
                if cot > 20:
                    quit=True
                    break                
                RGB3 = DllGetPixel(wHWND,Pos3.x,Pos3.y) & 0xffffffff   
    print 'end'

关于在python中怎么用ctypes模拟点击就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI