用API函數(shù)mouse_event可以做到
濟(jì)源網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,濟(jì)源網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為濟(jì)源上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的濟(jì)源做網(wǎng)站的公司定做!
函數(shù)及常數(shù)聲明:
Public 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)
Public Const MOUSEEVENTF_ABSOLUTE = H8000 ' absolute move
Public Const MOUSEEVENTF_LEFTDOWN = H2 ' left button down
Public Const MOUSEEVENTF_LEFTUP = H4 ' left button up
Public Const MOUSEEVENTF_MIDDLEDOWN = H20 ' middle button down
Public Const MOUSEEVENTF_MIDDLEUP = H40 ' middle button up
Public Const MOUSEEVENTF_MOVE = H1 ' mouse move
Public Const MOUSEEVENTF_RIGHTDOWN = H8 ' right button down
Public Const MOUSEEVENTF_RIGHTUP = H10 ' right button up
Public Const MOUSEEVENTF_WHEEL = H800
測(cè)試代碼:建立一個(gè)窗體,加一個(gè)按鈕和一個(gè)timer
Option Explicit
Private Sub Command1_Click()
Timer1.Interval = 5000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim i As Integer
For i = 0 To 100
DoEvents
mouse_event MOUSEEVENTF_WHEEL, 0, 0, 10, 0
Next
For i = 0 To 100
DoEvents
mouse_event MOUSEEVENTF_WHEEL, 0, 0, -10, 0
Next
End Sub
調(diào)用api
在Visual Baisc點(diǎn)虐 中的聲明:
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)
Public Const MOUSEEVENTF_LEFTDOWN = H2 '模擬鼠標(biāo)左鍵按下
Public Const MOUSEEVENTF_LEFTUP = H4 ’模擬鼠標(biāo)左鍵釋放
Public Const MOUSEEVENTF_MIDDLEDOWN = H20 '模擬鼠標(biāo)中間鍵按下
Public Const MOUSEEVENTF_MIDDLEUP = H40 '模擬鼠標(biāo)中間鍵釋放
Public Const MOUSEEVENTF_RIGHTDOWN = H8 '模擬鼠標(biāo)右鍵按下
Public Const MOUSEEVENTF_RIGHTUP = H10 '模擬鼠標(biāo)右鍵釋放
Public Const MOUSEEVENTF_MOVE = H1 '模擬鼠標(biāo)指針移動(dòng)
例:
mouse_event MOUSEEVENTF_LEFTDOWN,10,10,0,0
'在(10,10)模擬鼠標(biāo)左鍵按下
1. Case 3 改成 Case 4
另外,在你不知道原因的情況下你可以在這個(gè)過(guò)程中加入debug.print button
這樣 你在窗體上任意按下鼠標(biāo)的鍵,你就能從“立即”窗口中知道這個(gè)鍵的botton值是多少。這個(gè)辦法是經(jīng)常有用的辦法!我覺(jué)得你應(yīng)該學(xué)會(huì)用這類辦法去解決一些比較普通的問(wèn)題。
如這里你可以這樣寫:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
debug.print "Botton=";botton,"Shift=";shift,"X=";x,"Y=";y
End Sub
這樣你單擊鼠標(biāo) 你就知道了各個(gè)值 (逆向思維),然后編程的時(shí)候就寫這個(gè)值 ……
Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
Dim postion1 As New Point
mouse_event(H1 Or H8000, postion1.X * 65535 / 1366, postion1.Y * 65535 / 768, 0, 0)
'按絕對(duì)位置移動(dòng)鼠標(biāo)
'在鼠標(biāo)坐標(biāo)系統(tǒng)中,屏幕在水平和垂直方向上均勻分割成65535×65535個(gè)單元,
'當(dāng)前所用顯示屏分辨率為1366*768
'所以要進(jìn)行如上轉(zhuǎn)換
mouse_event(H2, 0, 0, 0, 0) '鼠標(biāo)左鍵按下
mouse_event(H4, 0, 0, 0, 0) '鼠標(biāo)左鍵彈起
msdn上有 mouse_event 的詳解,大致使用如上