在python中怎么用ctypes模拟点击

这篇文章将为大家详细讲解有关在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模拟点击就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站标题:在python中怎么用ctypes模拟点击
浏览路径:http://ybzwz.com/article/jhpccp.html