VB为自己的程序设定消息(可接收处理) | 雨律在线

  用API函数 SetWindowLong 指定处理消息的窗口过程为自定义的函数 WindowProc,捕获消息ID为 WM_USER+1 的自定义消息或系统消息。并且,为了保证窗口能正确的响应消息,需要保存原来默认的窗口过程并在自定义函数WindowProc中调用。如下例,在按钮事件中发送自定义消息,WindowProc 捕获了窗口大小变化的系统定义消息和用户自定义的消息并显示用户自定义的消息参数 wParam。

Form 部分代码

Private Sub Command1_Click()
Dim wParam As Long
Dim
lParam As Long
Dim
lResult As Long
 wParam = 12345
 lResult = SendMessage(Me.hwnd, WM_USER + 1, wParam, lParam)
End Sub

Private Sub
Form_Load()
 Me.Tag = Hook(Me.hwnd)
End Sub

Private Sub
Form_Unload(Cancel As Integer)
 Unhook Me.hwnd, Me.Tag
End Sub


Mod 部分代码

Option Explicit

Private 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
GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(
ByVal hwnd As Long, ByVal nIndex As Long) As Long

Private Declare Function
SetWindowLong Lib "user32" Alias _
"SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong 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

Private Const
GWL_WNDPROC = -4
Public Const GWL_USERDATA = (-21)
Public Const WM_SIZE = &H5
Public Const WM_USER = &H400

Public Function Hook(ByVal hwnd As Long) As Long
 Dim pOld As Long
'指定自定义的窗口过程
 pOld = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
'保存原来默认的窗口过程指针
 SetWindowLong hwnd, GWL_USERDATA, pOld
 Hook = pOld
End Function

Public Sub
Unhook(ByVal hwnd As Long, ByVal lpWndProc As Long)
 Dim temp
As Long
 '注释:Cease subclassing.
 temp = SetWindowLong(hwnd, GWL_WNDPROC, lpWndProc)
End Sub

Function
WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
 If uMsg = WM_SIZE Then
 '处理WM_SIZE消息
MsgBox "收到消息 WM_SIZE"
 End If
 If uMsg = WM_USER + 1 Then
MsgBox wParam
 End
If
 Dim lpPrevWndProc As Long
'查询原来默认的窗口过程指针

 
目前有0条回应
Comment
Trackback
你目前的身份是游客,请输入昵称和电邮!