首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SetWindowsHookEx for WM_MOUSEWHEEL

SetWindowsHookEx for WM_MOUSEWHEEL
EN

Stack Overflow用户
提问于 2013-05-31 12:32:03
回答 1查看 3K关注 0票数 3

我需要一个用VB.NET编写的代码示例来捕获表单之外的鼠标轮滚动事件,使用带有user32.dll和WM_MOUSEWHEEL的低级钩子,就像Hans 在我的另一个问题:记录鼠标中间按钮和滚轮滚动中所说的那样。

这是我需要做的一个伪例子:

代码语言:javascript
复制
Dim mousewheel_up as boolean
Dim mousewheel_down as boolean

Sub that Overides the windows messages to set the mousewheel booleans

    If mousewheel_up then msgbox("MouseWheel up")
    If mousewheel_down then msgbox("MouseWheel down")

End sub

更新

尝试过这种方法,但是它只在表单中工作,而且我也不知道如何获得增量值:

代码语言:javascript
复制
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Application.AddMessageFilter(New MouseWheelMessageFilter())
    End Sub

Public Class MouseWheelMessageFilter : Implements IMessageFilter

    Public Function PreFilterMessage1(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage

        ' Filter out WM_MOUSEWHEEL messages, which raise the MouseWheel event,
        ' whenever the Ctrl key is pressed. Otherwise, let them through.
        Const WM_MOUSEWHEEL As Integer = &H20A

        'If m.Msg = WM_MOUSEWHEEL & My.Computer.Keyboard.CtrlKeyDown Then
        If m.Msg = WM_MOUSEWHEEL Then
            ' Process the message here.
            If Form.ActiveForm IsNot Nothing Then
                MsgBox("Mouse scrolled!")
                ' TODO: Insert your code here to adjust the size of the active form.
                ' As shown above in the If statement, you can retrieve the form that
                ' is currently active using the static Form.ActiveForm property.
                ' ...
            End If
            Return True  ' swallow this particular message
        End If
        Return False    ' but let all other messages through
    End Function

End Class
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-10 08:21:57

您需要使用SetWindowsHookEx函数并将钩子类型指定为*WH_MOUSE_LL* (14)。有以下声明。

代码语言:javascript
复制
Public Structure Point
    Public X As Integer
    Public Y As Integer
End Structure

Public Structure Msllhookstruct
    Public Location As Point
    Public MouseData As Integer
    Public Flags As Integer
    Public Time As Integer
    Public ExtraInfo As Integer
End Structure

Private Delegate Function HookProc(nCode As Integer, wParam As Integer, ByRef lParam As Msllhookstruct) As Integer

<DllImport("user32.dll", SetLastError:=True)> _
Private Function SetWindowsHookEx(ByVal hookType As Integer, ByVal lpfn As HookProc, ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
End Function

<DllImport("user32.dll")> _
Private Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As Msllhookstruct) As IntPtr
End Function

Private Hook As IntPtr

在初始化例程中,请按以下方式使用

代码语言:javascript
复制
Hook = SetWindowsHookEx(14, AddressOf Proc, Process.GetCurrentProcess().MainModule.BaseAddress.ToInt32(), 0)

其中Proc是一个函数回调,如下所示:

代码语言:javascript
复制
Private Function Proc(nCode As Integer, wParam As Integer, ByRef lParam As Msllhookstruct) As IntPtr
    If wParam = 522 Then
        Dim Delta = CShort(lParam.MouseData >> 16)

        If Delta > 0 Then
            ' Up
        ElseIf Delta < 0 Then
            ' Down
        End If
    End If

    Return CallNextHookEx(Hook, nCode, wParam, lParam)
End Function
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16857370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档