|
发表于 2020-1-25 09:27:01
|
显示全部楼层
'利用WM_COPYDATA 发消息
'程序1
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("ThunderRT6FormDC", "CopyData Test Form!")
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
'程序2
'窗体
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const SPI_GETWORKAREA As Long = 48
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Form_Load()
Dim ret As Long
prevWndProc = GetWindowLong(Me.hWnd, GWL_WNDPROC)
ret = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf Windproc)
Me.Caption = "CopyData Test Form!"
Me.AutoRedraw = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim ret As Long
ret = SetWindowLong(Me.hWnd, GWL_WNDPROC, prevWndProc)
End Sub
'模块
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
|
|