在VB中,并没有包含鼠标滚轮的滚动事件,所以必须用API自己捕获滚动信息并加以处理.主要需要用到CallWindowProc和SetWindowLong两个函数,请参考以下代码:
'在模块中
'声明方法
Public PrevWndProc As Long
Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long '写自己处理鼠标滚动的事件,这里让Form上下滚动
Dim t(0 To 1) As Integer
If uMsg = WM_MOUSEWHEEL Then
If wParam 0 Then 'backward
Form1.Top = Form1.Top + 10
Else 'forforward
Form1.Top = Form1.Top - 10
End If
Else
WndProc = CallWindowProc(PrevWndProc, hwnd, uMsg, wParam, lParam) '让Windows处理其他事件
End Function
然后在Form中写入:
Option Explicit
Private Sub Form_Load()
PrevWndProc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WndProc) '让WndProc来处理该窗体的事件
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim lResult As Long
lResult = SetWindowLong(Me.hwnd, GWL_WNDPROC, PrevWndProc) '让Windows默认的函数来处理事件
关于CallWindowProc和SetWindowLong您可以参考以下文章:
SetWindowLong
CallWindowProc
(pDest As Any, pSource As Any, ByVal ByteLen As Long)
(ByVal hWnd As Long, ByVal nIndex As Long) As Long
(ByVal hWnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
(ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, _
ByVal Msg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
(ByVal hWnd As Long, ByVal lpString As String, _
ByVal hData As Long) As Long
(ByVal hWnd As Long, ByVal lpString As String) As Long
Public Function HIWORD(LongIn As Long) As Integer
HIWORD = (LongIn And HFFFF0000) \ H10000
End Function
Public Function MWheelProc(ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
Dim OldProc As Long
Dim CtlWnd As Long
Dim CtlPtr As Long
Dim IntObj As Object
Dim MWObject As MWheel
CtlWnd = GetProp(hWnd, "WheelWnd")
CtlPtr = GetProp(CtlWnd, "WheelPtr")
OldProc = GetProp(CtlWnd, "OldWheelProc")
If wMsg = WM_MOUSEWHEEL Then
Set MWObject = IntObj
MWObject.WndProc hWnd, wMsg, wParam, lParam
Set MWObject = Nothing
Exit Function
End If
MWheelProc = CallWindowProc(OldProc, hWnd, wMsg, wParam, lParam)
Public Sub Subclass(MWCtl As MWheel, ParentWnd As Long)
If GetProp(MWCtl.hWnd, "OldWheelProc") 0 Then
Exit Sub
SetProp MWCtl.hWnd, "OldWheelProc", _
GetWindowLong(ParentWnd, GWL_WNDPROC)
SetProp MWCtl.hWnd, "WheelPtr", ObjPtr(MWCtl)
SetProp ParentWnd, "WheelWnd", MWCtl.hWnd
SetWindowLong ParentWnd, GWL_WNDPROC, AddressOf MWheelProc
End Sub
Public Sub UnSubclass(MWCtl As MWheel, ParentWnd As Long)
OldProc = GetProp(MWCtl.hWnd, "OldWheelProc")
If OldProc = 0 Then Exit Sub
SetWindowLong ParentWnd, GWL_WNDPROC, OldProc
RemoveProp ParentWnd, "WheelWnd"
RemoveProp MWCtl.hWnd, "WheelPtr"
RemoveProp MWCtl.hWnd, "OldWheelProc"
然后,定义用户控件MWheel,实现对相关控件鼠标滚轮事件的处理.用户控件(MWheel)代码
Option Explicit
Dim m_CapWnd As Long
Dim m_Subclassed As Boolean
Event WheelScroll(Shift As Integer, zDelta As Integer, _
X As Single, Y As Single)
Private Sub UserControl_Resize()
Public Sub DisableWheel()
If m_CapWnd = 0 Then Exit Sub
If m_Subclassed = False Then Exit Sub
UnSubclass Me, m_CapWnd
m_Subclassed = False
Public Sub EnableWheel()
m_Subclassed = True
Subclass Me, m_CapWnd
Friend Property Get hWnd() As Long
hWnd = UserControl.hWnd
End Property
Public Property Get hWndCapture() As Long
hWndCapture = m_CapWnd
Public Property Let hWndCapture(ByVal vNewValue As Long)
m_CapWnd = vNewValue
Friend Sub WndProc(ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
Dim wShift As Integer
Dim wzDelta As Integer
Dim wX As Single, wY As Single
wzDelta = HIWORD(wParam)
wY = HIWORD(lParam)
RaiseEvent WheelScroll(wShift, wzDelta, wX, wY)
End Sub最后,就可以将定义的用户控件用在vb窗体编程中,实现对鼠标滚轮事件的监听和处理,测试主窗体如下:Option Explicit
Dim KAs As Long
Dim KA1 As Long
Private Sub Picture1_Click()
MWheel1.hWndCapture = Picture1.hWnd
MWheel1.EnableWheel
Private Sub List1_Click()
KA1 = List1.ListCount
Private Sub File1_Click()
KA1 = File1.ListCount
If KAs 0 Then
KAs = KAs - 1
List1.ListIndex = KAs
If KAs KA1 - 1 Then
KAs = KAs + 1
Private Sub MWheel1_WheelScroll(Shift As Integer, zDelta As Integer, X As Single, Y As Single)
File1.ListIndex = KAs
End Sub/SPAN
Private?Sub?Panel1_Click(ByVal?sender?As?Object,?ByVal?e?As?System.EventArgs)?Handles?Panel1.Click
Panel1.Focus()
End?Sub
拦截窗口程序消息可以解决
参考 VB王国荣API讲座 讲消息的那章
几个API就可以搞定
根据我的经验,应该是PICtureBox没有获取焦点,而win10下不知道什么原因能自动获取焦点,所以凑巧成功了,所以呢你应该让图形框获取焦点
如:picturebox1.focus()
不知道是不是解决了你的问题
以上就是土嘎嘎小编为大家整理的vb.net鼠标滚轮事件的简单介绍vb.net鼠标滚轮事件的简单介绍相关咨询咨询主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!