VB中如何使用自定义消息?
在SDI的程序中,希望在一个弹出的模态对话框中向主窗体发送一条消息然后主窗体对显示的内容进行更新.
网上找了很多的相关内容,但基本都说得不清楚,请教会的朋友,尽量详细说明一下,谢谢.
[解决办法]
下面代码添加带FORM1中
Private Const WM_COPYDATA = &H4A
Private 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 Declare Function FindWindow Lib "user32 " Alias "FindWindowA " (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Type COPYDATASTRUCT
dwData As Long
cbData As Long
lpData As Long
End Type
Private Sub Command1_Click()
Dim Cd As COPYDATASTRUCT
Dim Temp As String
Dim Data() As Byte
Dim m As Long
m = FindWindow(vbNullString, "CopyData Test Form! ")
MsgBox m
Temp = "This is a Test Message! "
Data = Temp
Cd.cbData = UBound(Data) + 1
Cd.lpData = VarPtr(Data(0))
Cd.dwData = 0
SendMessage m, WM_COPYDATA, 0, Cd
End Sub
Private Sub Form_Load()
Form2.Show
End Sub
下面代码添加到FORM2中
'模块
Public Declare Function GetWindowLong Lib "user32 " Alias "GetWindowLongA " (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32 " Alias "SetWindowLongA " (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public 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
Private Declare Sub CopyMemory Lib "kernel32 " Alias "RtlMoveMemory " (Destination As Any, Source As Any, ByVal Length As Long)
Public Const WM_COPYDATA = &H4A
Public Const GWL_WNDPROC = (-4)
Public prevWndProc As Long
Private Type COPYDATASTRUCT
dwData As Long
cbData As Long
lpData As Long
End Type
Function Windproc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim Cd As COPYDATASTRUCT
Dim Temp As String
Select Case Msg
Case WM_COPYDATA
CopyMemory Cd, ByVal lParam, Len(Cd)
Temp = Space(Cd.cbData)
CopyMemory ByVal Temp, ByVal Cd.lpData, Cd.cbData
Form1.Print StrConv(Temp, vbFromUnicode)
End Select
Windproc = CallWindowProc(prevWndProc, hWnd, Msg, wParam, lParam)
End Function
[解决办法]
高射炮打蚊子。