Delegated VB.NET出错

时间:2017-12-06 08:41:05

标签: vb.net

我正在尝试创建一个类,它将检测每次按下什么鼠标按钮,但是我在Hookmouse()子中遇到了2个与委托相关的错误。有人能帮助我理解我做错了什么吗?如果有人可以向我解释更多关于代表的事情,我会非常感激,除非纠正这段代码。

我得到的2个错误:

  

方法'私有函数MouseProc(nCode为整数,wParam为IntPtr,ByRef lParam为MouseHook.MouseHookStruct)As Integer'没有与委托'委托函数MouseHook.MouseProcDelegate兼容的签名(nCode为Integer,wParam为IntPtr, lParam As MouseHook.MouseHookStruct)As Integer'。

     

'MouseHook.MouseProcDelegate'类型的值无法转换为'MouseHook.CallBack'。

Imports System.Runtime.InteropServices
Public Class MouseHook
    Public Delegate Function CallBack(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As MouseHookStruct) As Integer
'Declare the mouse hook constant.
'For other hook types, obtain these values from Winuser.h in Microsoft SDK.
Dim WH_MOUSE As Integer = 7
Shared hHook As Integer = 0

'Keep the reference so that the delegate is not garbage collected.
Private hookproc As CallBack

'Import for the SetWindowsHookEx function.
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Overloads Shared Function SetWindowsHookEx _
      (ByVal idHook As Integer, ByVal HookProc As CallBack,
       ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
End Function

'Import for the CallNextHookEx function.
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Overloads Shared Function CallNextHookEx _
      (ByVal idHook As Integer, ByVal nCode As Integer,
       ByVal wParam As IntPtr, ByVal lParam As MouseHookStruct) As Integer
End Function
'Import for the UnhookWindowsHookEx function.
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Overloads Shared Function UnhookWindowsHookEx _
          (ByVal idHook As Integer) As Boolean
End Function
Private Delegate Function MouseProcDelegate(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As MouseHookStruct) As Integer



'Point structure declaration.
<StructLayout(LayoutKind.Sequential)> Public Structure Point
    Public x As Integer
    Public y As Integer
End Structure

'MouseHookStruct structure declaration.
<StructLayout(LayoutKind.Sequential)> Public Structure MouseHookStruct
    Public pt As Point
    Public hwnd As Integer
    Public wHitTestCode As Integer
    Public dwExtraInfo As Integer
End Structure
Private Const HC_ACTION As Integer = 0
Private Const WH_MOUSE_LL As Integer = 14
Private Const WM_MOUSEMOVE As Integer = &H200
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_LBUTTONUP As Integer = &H202
Private Const WM_LBUTTONDBLCLK As Integer = &H203
Private Const WM_RBUTTONDOWN As Integer = &H204
Private Const WM_RBUTTONUP As Integer = &H205
Private Const WM_RBUTTONDBLCLK As Integer = &H206
Private Const WM_MBUTTONDOWN As Integer = &H207
Private Const WM_MBUTTONUP As Integer = &H208
Private Const WM_MBUTTONDBLCLK As Integer = &H209
Private Const WM_MOUSEWHEEL As Integer = &H20A

Private MouseHook As Integer
Private MouseHookDelegate As MouseProcDelegate

Public Event Mouse_Move(ByVal ptLocat As Point)
Public Event Mouse_Left_Down(ByVal ptLocat As Point)
Public Event Mouse_Left_Up(ByVal ptLocat As Point)
Public Event Mouse_Left_DoubleClick(ByVal ptLocat As Point)
Public Event Mouse_Right_Down(ByVal ptLocat As Point)
Public Event Mouse_Right_Up(ByVal ptLocat As Point)
Public Event Mouse_Right_DoubleClick(ByVal ptLocat As Point)
Public Event Mouse_Middle_Down(ByVal ptLocat As Point)
Public Event Mouse_Middle_Up(ByVal ptLocat As Point)
Public Event Mouse_Middle_DoubleClick(ByVal ptLocat As Point)
Public Event Mouse_Wheel(ByVal ptLocat As Point, ByVal Direction As Wheel_Direction)

Public Enum Wheel_Direction
    WheelUp
    WheelDown
End Enum

Private Function MouseProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As MouseHookStruct) As Integer
    If (nCode = HC_ACTION) Then
        Select Case wParam
            Case WM_MOUSEMOVE
                RaiseEvent Mouse_Move(lParam.pt)
            Case WM_LBUTTONDOWN
                RaiseEvent Mouse_Left_Down(lParam.pt)
            Case WM_LBUTTONUP
                RaiseEvent Mouse_Left_Up(lParam.pt)
            Case WM_LBUTTONDBLCLK
                RaiseEvent Mouse_Left_DoubleClick(lParam.pt)
            Case WM_RBUTTONDOWN
                RaiseEvent Mouse_Right_Down(lParam.pt)
            Case WM_RBUTTONUP
                RaiseEvent Mouse_Right_Up(lParam.pt)
            Case WM_RBUTTONDBLCLK
                RaiseEvent Mouse_Right_DoubleClick(lParam.pt)
            Case WM_MBUTTONDOWN
                RaiseEvent Mouse_Middle_Down(lParam.pt)
            Case WM_MBUTTONUP
                RaiseEvent Mouse_Middle_Up(lParam.pt)
            Case WM_MBUTTONDBLCLK
                RaiseEvent Mouse_Middle_DoubleClick(lParam.pt)
            Case WM_MOUSEWHEEL
                Dim wDirection As Wheel_Direction
                If lParam.hwnd < 0 Then
                    wDirection = Wheel_Direction.WheelDown
                Else
                    wDirection = Wheel_Direction.WheelUp
                End If
                RaiseEvent Mouse_Wheel(lParam.pt, wDirection)
        End Select
    End If
    Return CallNextHookEx(MouseHook, nCode, wParam, lParam)
End Function
Protected Overrides Sub Finalize()
    UnhookWindowsHookEx(MouseHook)
    MyBase.Finalize()
End Sub

Public Sub HookMouse()
    MouseHookDelegate = New MouseProcDelegate(AddressOf MouseProc)
    MouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
End Sub

Public Sub UnhookMouse()
    UnhookWindowsHookEx(MouseHook)
End Sub
 End Class

0 个答案:

没有答案