溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在python中利用ctypes模擬一個(gè)點(diǎn)擊事件

發(fā)布時(shí)間:2020-11-27 14:38:30 來(lái)源:億速云 閱讀:258 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家介紹怎么在python中利用ctypes模擬一個(gè)點(diǎn)擊事件,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

代碼如下:

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 進(jìn)行過(guò)測(cè)試,保證其正確運(yùn)行。由于有些代碼在Linux,Windows或Mac OS X下的表現(xiàn)不同,這些代碼會(huì)在 doctest 中包含相關(guān)的指令注解。

注意:部分示例代碼引用了 ctypes c_int 類(lèi)型。在 sizeof(long) == sizeof(int) 的平臺(tái)上此類(lèi)型是 c_long 的一個(gè)別名。所以,在程序輸出 c_long 而不是你期望的 c_int 時(shí)不必感到迷惑 --- 它們實(shí)際上是同一種類(lèi)型。

載入動(dòng)態(tài)連接庫(kù)
ctypes 導(dǎo)出了 cdll 對(duì)象,在 Windows 系統(tǒng)中還導(dǎo)出了 windll 和 oledll 對(duì)象用于載入動(dòng)態(tài)連接庫(kù)。

通過(guò)操作這些對(duì)象的屬性,你可以載入外部的動(dòng)態(tài)鏈接庫(kù)。cdll 載入按標(biāo)準(zhǔn)的 cdecl 調(diào)用協(xié)議導(dǎo)出的函數(shù),而 windll 導(dǎo)入的庫(kù)按 stdcall 調(diào)用協(xié)議調(diào)用其中的函數(shù)。 oledll 也按 stdcall 調(diào)用協(xié)議調(diào)用其中的函數(shù),并假定該函數(shù)返回的是 Windows HRESULT 錯(cuò)誤代碼,并當(dāng)函數(shù)調(diào)用失敗時(shí),自動(dòng)根據(jù)該代碼甩出一個(gè) OSError 異常。

在 3.3 版更改: 原來(lái)在 Windows 下甩出的異常類(lèi)型 WindowsError 現(xiàn)在是 OSError 的一個(gè)別名。

這是一些 Windows 下的例子。注意:msvcrt 是微軟 C 標(biāo)準(zhǔn)庫(kù),包含了大部分 C 標(biāo)準(zhǔn)函數(shù),這些函數(shù)都是以 cdecl 調(diào)用協(xié)議進(jìn)行調(diào)用的。

>>> from ctypes import *
>>> print(windll.kernel32) 
<WinDLL 'kernel32', handle ... at ...>
>>> print(cdll.msvcrt)   
<CDLL 'msvcrt', handle ... at ...>
>>> libc = cdll.msvcrt   
>>>

Windows會(huì)自動(dòng)添加通常的 .dll 文件擴(kuò)展名。

關(guān)于怎么在python中利用ctypes模擬一個(gè)點(diǎn)擊事件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI