1.認識運用的函數

mouse_event API

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)'聲明API函數

定義

Const MOUSEEVENTF_LEFTDOWN = &H2 '左鍵按下
Const MOUSEEVENTF_LEFTUP = &H4 '左鍵彈起
Const MOUSEEVENTF_MIDDLEDOWN = &H20 '中鍵按下
Const MOUSEEVENTF_MIDDLEUP = &H40 '中鍵彈起
Const MOUSEEVENTF_MOVE = &H1 '移動鼠標
Const MOUSEEVENTF_ABSOLUTE = &H8000 鼠標指定絕對座標
Const MOUSEEVENTF_RIGHTDOWN = &H8 '右鍵按下
Const MOUSEEVENTF_RIGHTUP = &H10'右鍵彈起

滑鼠按下

mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 '左鍵按下,彈起
mouse_event MOUSEEVENTF_RIGHTDOWN Or MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 '右鍵按下,彈起

亦可這樣寫

mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0

mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

這樣就是按一次滑鼠左鍵

通常滑鼠連點 就是將這段 放到一個Timer裡面

Timer的Interval值改成適當值 '當Timer執行完一次 隔100豪秒在執行一次 無限循環

如果改成100豪秒 則每100豪秒按一下滑鼠左鍵

不過 Timer的Enabled的值要改為False

否則一開啟程式就會不斷的連點

Private Sub Timer1_Timer()
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

 

通常滑鼠連點程式都會搭配一個快捷鍵

GetAsyncKeyState這個API函數

該函數會偵測按下鍵的值

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Long

放在一個Timer裡面 做無限檢測

Timer的Interval值改為100  '當Timer執行完一次 隔100豪秒在執行一次 無限循環

Private Sub Timer2_Timer()
If GetAsyncKeyState(vbKeyF1) Then '若按下F1鍵則執行以下代碼

Timer1.Enabled = True  'Timer1的可用性開放
ElseIf GetAsyncKeyState(vbKeyF2) then  若不是按下F1 而是按下F2 則執行以下代碼

Timer1.Enabled = False 'Timer1的可用性關閉

End If
End Sub

2.完整代碼

為了測試是否有成功連點 

加入一個Label  ,按鈕


Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Long
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10

 

Private Sub Command1_Click()

Label1.Caption = Label1.Caption + 1
End Sub

 

Private Sub Form_Load()
Label1.Caption = "0"

Timer1.Enabled = False

Timer2.Enabled = True
Timer1.Interval = 100
Timer2.Interval = 100
Timer2.Enabled = True

End Sub

 

Private Sub Timer1_Timer()
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

 

Private Sub Timer2_Timer()
If GetAsyncKeyState(vbKeyF1) Then
Timer1.Enabled = True
ElseIf GetAsyncKeyState(vbKeyF2) Then
Timer1.Enabled = False
End If
End Sub

介面  

 


pua0156k 發表在 痞客邦 PIXNET 留言(0) 人氣()