www.pudn.com > VBSendText.zip > frmSendText.frm
VERSION 5.00
Begin VB.Form frmSendText
Caption = "SendText"
ClientHeight = 1110
ClientLeft = 60
ClientTop = 345
ClientWidth = 5235
LinkTopic = "Form3"
ScaleHeight = 1110
ScaleWidth = 5235
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text1
Height = 375
Left = 120
TabIndex = 0
Text = "Text1"
Top = 480
Width = 4695
End
Begin VB.Label Label1
Caption = "再起动一个新实例,它会用WN_SETTEXT消息来通知。"
Height = 255
Left = 120
TabIndex = 1
Top = 120
Width = 4695
End
End
Attribute VB_Name = "frmSendText"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'*******************************************************
' Form Module Name: frmSendText
' Created By: AdamBear 熊超 2002-2-2 1:37
' Purpose:
' 此程序用来演示不用窗口子类化传递消息给另一进程
' 本窗口使用SetWindowText来触发Text1的Chage事件
' NOTE: 程序必须用VB6编译才能正常运行
'*********************************************************
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const strCaption As String = "SendText" '窗口的Caption
Private Const strOrg As String = "Text1" '文本框的初始值
Private Sub Form_Load()
'
Dim childTxtHwnd As Long, frmHwnd As Long
Me.Caption = ""
'通过SPY++得到FORM的ClassName为ThunderRT6FormDC,从而得到其句柄,注意其在不同版本或调试环境中不同
frmHwnd = FindWindow("ThunderRT6FormDC", strCaption)
If frmHwnd <> 0 Then
'通过SPY++得到TEXT1的ClassName为ThunderRT6TextBox
childTxtHwnd = FindWindowEx(frmHwnd, 0, "ThunderRT6TextBox", strOrg)
If childTxtHwnd = 0 Then ApiRaise Err.LastDllError
'NOTE:注意,我在贴子上说错了,SetWindowText只能在同一进程中使用,所以下面这样做是行不通的
'害我调试了半天,不过可以考虑用VC做个DLL,在DLL里收别消息,再用SetWindowText来通知VB
'SetWindowText childTxtHwnd, "Data Changed"
Me.Visible = False
Dim strMsg As String
strMsg = "新进程发来一个随机数:" & Str(Rnd())
'下面被注释掉的东西是为了制成Null结尾的ANSI字串,多余,只需在SendMessage的声明上动动
' 就可以了,老的坏习惯,要改!
' Dim abMsg() As Byte
' abMsg = StrConv(strMsg, vbFromUnicode)
' Dim c As Long
' c = UBound(abMsg)
' ReDim Preserve abMsg(c + 1) As Byte
' abMsg(c + 1) = 0
' SendMessage childTxtHwnd, WM_SETTEXT, 0, abMsg(0)
SendMessageStr childTxtHwnd, WM_SETTEXT, 0, strMsg
Unload Me
Exit Sub
End If
Me.Caption = strCaption
Text1 = strOrg
End Sub
Private Sub Text1_Change()
'
If Text1 = strOrg Then
Exit Sub '使此文本框除初值外不能人为更改,且不会引起层叠。见本子程序最后
Else
Me.SetFocus
MsgBox "数据来了!"
End If
Text1.Text = strOrg '还原,以使文本框句柄可被找到。
End Sub