请教各位大虾有关在vb.net中使用SetWindowsHookEx的问题
我在vb.net中使用SetWindowsHookEx函数老是出错,方法很简单,建立一个vb.net的工程,它就有个缺省的form,拷贝如下代码,运行,然后在出现的窗口上反复晃鼠标,必然要出错。
出错信息是:未处理的“System.NullReferenceException”类型的异常出现在 system.windows.forms.dll 中。
其他信息: 未将对象引用设置到对象的实例。
请教这是怎么回事?
下面是代码:
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
Private isMousehooked As Boolean
Private hMousehook As Integer
Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
'Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As HookType, ByVal lpfn As HOOKPROC, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HOOKPROC, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Declare Function GetCurrentThreadId Lib "kernel32" Alias "GetCurrentThreadId" () As Integer
Private Const WH_MOUSE = 7
Private isMouseLeaved As Boolean = True
Private prevMousePoint As POINT
'Public Delegate Function HookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
#Region " Enums and structs"
Public Enum Msg
WM_MOUSEMOVE = &H200
End Enum
Public Structure POINT
Public x As Integer
Public y As Integer
End Structure
Public Structure MOUSEHOOKSTRUCT
Public pt As POINT
Public hwnd As IntPtr
Public wHitTestCode As Integer
Public dwExtraInfo As IntPtr
End Structure
#End Region
Public Sub SetMouseHook()
If isMousehooked Then Exit Sub
hMousehook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseProc, 0, GetCurrentThreadId())
isMousehooked = True
End Sub
Public Sub RemoveMouseHook()
UnhookWindowsHookEx(hMousehook)
isMousehooked = False
End Sub
Public Function MouseProc(ByVal uCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
If wParam = Msg.WM_MOUSEMOVE Then ' And isMouseLeaved Then
Dim c As MOUSEHOOKSTRUCT = CType(Marshal.PtrToStructure( _
IntPtr.op_Explicit(lParam), GetType(MOUSEHOOKSTRUCT)), MOUSEHOOKSTRUCT)
If prevMousePoint.x = c.pt.x And prevMousePoint.y = c.pt.y Then
Else
If isMouseLeaved Then
isMouseLeaved = False
End If
prevMousePoint.x = c.pt.x
prevMousePoint.y = c.pt.y
End If
End If
MouseProc = CallNextHookEx(hMousehook, uCode, wParam, lParam)
End Function
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetMouseHook()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
RemoveMouseHook()
End Sub
End Class