无法接收到消息
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPublic Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_USER = &H400
Public Const GWL_WNDPROC = (-4)
Private OldWndProc As Long
Public Const WM_MYMESSAGE = WM_USER + 1 '自定义的消息一定要比WM_USER大,不然就会和Windows原有的消息重复的
Public Function WindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_MYMESSAGE Then
MsgBox "自定义消息!"
Exit Function
End If
WindowProc = CallWindowProc(OldWndProc, hwnd, Msg, wParam, lParam)
End Function
Public Sub HookWindow(hwnd As Long)
OldWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnHook(hwnd As Long)
SetWindowLong hwnd, GWL_WNDPROC, OldWndProc
End Sub
这是模块代码
Private Sub Form_Load()
HookWindow Me.hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHook Me.hwnd
End Sub
下面是发送消息代码,下面代码是另一个工程里,跨进程发送消息的
Private Sub Command1_Click()
Dim lRetu As Long
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "example")
If hwnd <> 0 Then lRetu = SendMessage(hwnd, WM_MYMESSAGE, 0, 0)
End Sub
我发送出去消息,钩子捕获不到,大家帮我看一下 --------------------编程问答-------------------- --------------------编程问答-------------------- 一般检查一下你发送端的句柄是不是你接受端的句柄,我估计是没发对句柄。 --------------------编程问答-------------------- 使用MsgHoo32.OCX控件
补充:VB , API