2013-05-31 3 views
1

Мне нужен пример кода, написанный на VB.NET, чтобы захватить прокрутки колесика мыши события вне формы с использованием низкоуровневого крюк с user32.dll и WM_MOUSEWHEEL как сказано Hans Passant ответить на мой другой вопрос: Record mouse Middle button and wheel scrollSetWindowsHookEx для WM_MOUSEWHEEL

Это псевдо-пример того, что мне нужно сделать:

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 

UPDATE

попытался это, но она работает только внутри формы, и я не знаю, как получить значение дельты:

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 
+0

Вот [нить] (HTTP: // социальный. msdn.microsoft.com/Forums/en-US/vblanguage/thread/5f9a962e-8bb8-455f-8f21-9841c962721b) с сообщением, в котором есть код, который должен делать то, что вы хотите. – tinstaafl

ответ

4

Вы должны использовать функцию 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 
+0

Thankyou так много работает отлично. – ElektroStudios

+0

Вы знаете, как воспроизводить свиток? Я имею в виду, как отправить сообщение в Windows для прокрутки вверх или вниз ...? – ElektroStudios

+1

Взгляните на [SendInput] (http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310 (v = vs.85) .aspx) – ains

Смежные вопросы