请教,如何用vb实现模拟鼠标中键滚动!
答案:2 悬赏:0 手机版
解决时间 2021-03-16 13:29
- 提问者网友:相思似海深
- 2021-03-16 03:35
用mouse_event可以模拟鼠标左中右键点击,但我找不到中键滚动如何用,请大侠指教一下。
最佳答案
- 五星知识达人网友:十年萤火照君眠
- 2021-03-16 04:31
用API函数mouse_event可以做到
函数及常数声明:
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
测试代码:建立一个窗体,加一个按钮和一个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
函数及常数声明:
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
测试代码:建立一个窗体,加一个按钮和一个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
全部回答
- 1楼网友:往事隔山水
- 2021-03-16 06:03
可以通过hook ,但是调试的时候ide容易挂掉,记住保存 下面是模块代码 option explicit '模块代码 '*********************************************************** 'mmousewheel '鼠标滚轮的事件检测 '*********************************************************** private declare function callwindowproc lib "user32" alias "callwindowproca" (byval lpprevwndfunc as long, byval hwnd as long, byval msg as long, byval wparam as long, byval lparam as long) as long private declare function setwindowlong lib "user32" alias "setwindowlonga" (byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long private const gwl_wndproc = -4 private const wm_mousewheel = &h20a global lpprevwndproca as long public bmouseflag as boolean '鼠标事件激活标志 public sub hookmouse(byval hwnd as long) lpprevwndproca = setwindowlong(hwnd, gwl_wndproc, addressof windowproc) end sub public sub unhookmouse(byval hwnd as long) setwindowlong hwnd, gwl_wndproc, lpprevwndproca end sub private function windowproc(byval hw as long, byval umsg as long, byval wparam as long, byval lparam as long) as long select case umsg case wm_mousewheel '滚动 dim wzdelta, wkeys as integer 'wzdelta传递滚轮滚动的快慢,该值小于零表示滚轮向后滚动(朝用户方向), '大于零表示滚轮向前滚动(朝显示器方向) wzdelta = hiword(wparam) 'wkeys指出是否有ctrl=8、shift=4、鼠标键(左=2、中=16、右=2、附加)按下,允许复合 wkeys = loword(wparam) '-------------------------------------------------- if wzdelta < 0 then 小于零表示滚轮向后滚动(朝用户方向) else '大于零表示滚轮向前滚动(朝显示器方向) end if 'if getactivewindow() = f_set.hwnd then 'end if '-------------------------------------------------- case else windowproc = callwindowproc(lpprevwndproca, hw, umsg, wparam, lparam) end select end function private function hiword(longin as long) as integer hiword = (longin and &hffff0000) \ &h10000 '取出32位值的高16位 end function private function loword(longin as long) as integer loword = longin and &hffff& '取出32位值的低16位 end function
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯