高分求解.关于一个激活指定窗体的事件.
我想实现一个激活指定窗体的事件,运行一段时间后会出现SEHException异常,不知道是哪里出的问题,请达人们指教,感激不尽!
,代码如下:
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 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
Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Declare Function GetCurrentThreadId Lib "kernel32" Alias "GetCurrentThreadId" () As Integer
Public hnextHookproc As Integer
Public Const HCBT_ACTIVATE = 5
Public Const HSHELL_WINDOWACTIVATED = 4
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetHook()
End Sub
Public Function SetHook() '设置Hook
If hnextHookproc <> 0 Then
Exit Function
End If
hnextHookproc = SetWindowsHookEx(HookType.WH_SHELL, AddressOf MyKeyboardProc, 0, GetCurrentThreadId())
End Function
Public Sub UnHook() '解Hook
If hnextHookproc <> 0 Then
UnhookWindowsHookEx(hnextHookproc)
hnextHookproc = 0
End If
End Sub
Public Function MyKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Dim i As Integer
MyKeyboardProc = 0
If nCode < 0 Then
MyKeyboardProc = CallNextHookEx(hnextHookproc, nCode, wParam, lParam)
Exit Function
End If
If nCode = HSHELL_WINDOWACTIVATED Then
'此处添加我想要的处理
End If
End Function
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Call UnHook()
End Sub