【问题标题】:SetWindowsHookEx for WM_MOUSEWHEEL为 WM_MOUSEWHEEL 设置 WindowsHookEx
【发布时间】:2013-05-27 06:43:15
【问题描述】:

我需要一个用 VB.NET 编写的代码示例,以使用带有 user32.dll 和 的低级挂钩捕获鼠标滚轮滚动事件在表单之外 WM_MOUSEWHEEL 就像 Hans Passant 在我的另一个问题中回答的那样:Record mouse Middle button and wheel scroll

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

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

更新

试过了,但它只在表单内有效,我也不知道如何获取增量值:

   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

【问题讨论】:

  • 这是一个 thread 的帖子,其中的代码应该可以满足您的需求。

标签: .net vb.net mousewheel user32 windows-messages


【解决方案1】:

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

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

在你的初始化程序中,如下使用它

Hook = SetWindowsHookEx(14, AddressOf Proc, Process.GetCurrentProcess().MainModule.BaseAddress.ToInt32(), 0)

Proc 是这样的函数回调:

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

【讨论】:

  • 你知道如何复制卷轴吗?我的意思是如何向 Windows 发送消息以向上或向下滚动...?
  • 看看SendInput
猜你喜欢
  • 2012-06-26
  • 2016-02-28
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多