VB为自己的程序设定消息(可接收处理)
Posted by JiaJia 5 March,2009
(0)Comment
用API函数 SetWindowLong 指定处理消息的窗口过程为自定义的函数 WindowProc,捕获消息ID为 WM_USER+1 的自定义消息或系统消息。并且,为了保证窗口能正确的响应消息,需要保存原来默认的窗口过程并在自定义函数WindowProc中调用。如下例,在按钮事件中发送自定义消息,WindowProc 捕获了窗口大小变化的系统定义消息和用户自定义的消息并显示用户自定义的消息参数 wParam。
@H_502_23@
@H_502_23@Form 部分代码
@H_502_23@
@H_502_23@Mod 部分代码 @H_502_23@Private Sub Command1_Click()@H_502_23@ Dim wParam As Long@H_502_23@Dim lParam As Long@H_502_23@Dim lResult As Long@H_502_23@ wParam = 12345@H_502_23@ lResult = SendMessage(Me.hwnd,WM_USER + 1 ,wParam,lParam)@H_502_23@ End Sub@H_502_23@@H_502_23@Private Sub Form_Load()@H_502_23@ Me.Tag = Hook(Me.hwnd)@H_502_23@ End Sub@H_502_23@@H_502_23@Private Sub Form_Unload(Cancel As Integer )@H_502_23@ Unhook Me.hwnd,Me.Tag@H_502_23@ End Sub
@H_502_23@在VB中用自定义过程捕获并处理消息的详细信息和例子请参考: @H_502_23@Q170570 HOWTO: Build a Windows Message Handler with AddressOf in Visual @H_502_23@ http://support.microsoft.com/support/kb/articles/q170/5/70.asp @H_502_23@Q168795 HOWTO: Hook Into a Windows Messages Using AddressOf @H_502_23@ http://support.microsoft.com/support/kb/articles/q168/7/95.aspOption Explicit@H_502_23@@H_502_23@ Private Declare Function CallWindowProc Lib "user32" Alias _@H_502_23@ "CallWindowProcA" ( ByVal lpPrevWndFunc As Long ,_@H_502_23@ ByVal hwnd As Long , ByVal Msg As Long ,_@H_502_23@ ByVal wParam As Long , ByVal lParam As Long ) As Long@H_502_23@@H_502_23@Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _@H_502_23@( ByVal hwnd As Long , ByVal nIndex As Long ) As Long@H_502_23@@H_502_23@Private Declare Function SetWindowLong Lib "user32" Alias _@H_502_23@ "SetWindowLongA" ( ByVal hwnd As Long ,_@H_502_23@ ByVal nIndex As Long , ByVal dwNewLong As Long ) As Long@H_502_23@@H_502_23@Public Declare Function SendMessage Lib "user32" Alias _@H_502_23@ "SendMessageA" ( ByVal hwnd As Long , ByVal wMsg As Long ,lParam As Any) As Long@H_502_23@@H_502_23@Private Const GWL_WNDPROC = - 4@H_502_23@ Public Const GWL_USERDATA = (- 21 )@H_502_23@ Public Const WM_SIZE = &H5@H_502_23@ Public Const WM_USER = &H400@H_502_23@@H_502_23@ Public Function Hook( ByVal hwnd As Long ) As Long@H_502_23@ Dim pOld As Long@H_502_23@ '指定自定义的窗口过程@H_502_23@ pOld = SetWindowLong(hwnd,GWL_WNDPROC, AddressOf WindowProc) @H_502_23@ '保存原来默认的窗口过程指针@H_502_23@ SetWindowLong hwnd,GWL_USERDATA,pOld@H_502_23@ Hook = pOld@H_502_23@ End Function@H_502_23@@H_502_23@Public Sub Unhook( ByVal hwnd As Long , ByVal lpWndProc As Long )@H_502_23@ Dim temp As Long@H_502_23@ '注释:Cease subclassing.@H_502_23@ temp = SetWindowLong(hwnd,lpWndProc)@H_502_23@ End Sub@H_502_23@@H_502_23@Function WindowProc( ByVal hw As Long , ByVal uMsg As Long , ByVal wParam As Long , ByVal lParam As Long ) As Long@H_502_23@ If uMsg = WM_SIZE Then@H_502_23@ '处理WM_SIZE消息@H_502_23@ MsgBox "收到消息 WM_SIZE"@H_502_23@ End If@H_502_23@ If uMsg = WM_USER + 1 Then@H_502_23@ MsgBox wParam@H_502_23@ End If@H_502_23@ Dim lpPrevWndProc As Long@H_502_23@ '查询原来默认的窗口过程指针@H_502_23@ lpPrevWndProc = GetWindowLong(hw,GWL_USERDATA)@H_502_23@ '调用原来的窗口过程@H_502_23@ WindowProc = CallWindowProc(lpPrevWndProc,hw,uMsg,lParam)@H_502_23@ End Function