'向记事本中写入和获取记事本中内容的程序: Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Private Const WM_GETTEXT = &HD Private Const WM_SETTEXT = &HC Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate Function Getsum() As Long Dim tempstr As String, strlong As Long, rtn As Long Dim winHwnd As Long Dim winHwnd1 As Long Dim s As String Dim RetVal As Long winHwnd = FindWindow(vbNullString, "无标题 - 记事本") If winHwnd <> 0 Then winHwnd1 = FindWindowEx(winHwnd, 0&, "Edit", vbNullString) s = Space(25700) strlong = Len(s) + 1 rtn = SendMessage(winHwnd1, WM_GETTEXT, strlong, s) MsgBox Trim(s) Else MsgBox "记事本程序没有运行?" End If End Function Private Function setsum() As Long Dim Phwnd As Long Dim ChildHwnd As Long Phwnd = FindWindow(vbNullString, "无标题 - 记事本") If Phwnd <> 0 Then ChildHwnd = FindWindowEx(Phwnd, 0, "Edit", vbNullString) If ChildHwnd <> 0 Then SendMessage ChildHwnd, WM_SETTEXT, 0, ByVal Text1.Text Else MsgBox "找不到相关控件" End If Else MsgBox "计算器程序没有运行" End If End FunctionPrivate Sub Command1_Click() setsum End SubPrivate Sub Command2_Click() Getsum End SubPrivate Sub Form_Load() Command1.Caption = "向记事本写入文本" Command2.Caption = "获取记事本中的文本" Dim ReturnValue, I ReturnValue = Shell("notepad.exe", 1) ' 运行记事 AppActivate ReturnValue ' 激活记事 Me.AutoRedraw = True End Sub
针对某些无边框窗口 或许可以用GetForegroundWindow来获取它的 Private Declare Function GetForegroundWindow Lib "user32" () As Long Private Sub Command1_Click() '测试本窗口hwnd Text2 = Me.hWnd '测试某个活动窗口hwnd Me.Hide Text1 = GetForegroundWindow Me.Show End Sub End Sub
参考此程序:http://www.symental.com/sfw/GetHwndAndClass.rar
把Form1的
Caption设置为工程1
WindowState设置为Minimized
LinkMode设为Source
LinkTopic设为Form1在Form1添加TextBox,text1
设置text1的text值为:这是EXE文件中TEXT1中的数据编译生成 工程1.EXE 再建一个VB工程
把Form1的
LinkMode设为Source
LinkTopic设为Form1
在Form1添加TextBox,text1
把工程保存在一个文件夹内,把上面所做的工程1.EXE复制到这个文件夹内.
不用编译,在VB环境中写以下程序调试就可以了.
Private Sub Form_Click()
If Text1.LinkMode = vbNone Then
Z = Shell("C:\Documents and Settings\Administrator\桌面\dde\工程1.exe", 4)
Text1.LinkTopic = "工程1|Form1" ' 设置连接主题。
Text1.LinkItem = "text1" ' 设置连接项目。
Text1.LinkMode = vbLinkManual ' 设置连接模式。
End If
Text1.LinkItem = "text1" ' 设置连接项目。
Text1.LinkRequest
End Sub按F5运行程序,单击form1,就可以看到从工程1.EXE的text1中读回来的数据了.这是DDE工程
Text1.text="xxxxxx"
Text1.LinkPoke ' 发送数据"xxxxxx"以上可以给工程1.EXE的TEXT1发数据.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Const WM_GETTEXT = &HD
Private Const WM_SETTEXT = &HC
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate Function Getsum() As Long
Dim tempstr As String, strlong As Long, rtn As Long
Dim winHwnd As Long
Dim winHwnd1 As Long
Dim s As String
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "无标题 - 记事本")
If winHwnd <> 0 Then
winHwnd1 = FindWindowEx(winHwnd, 0&, "Edit", vbNullString)
s = Space(25700)
strlong = Len(s) + 1
rtn = SendMessage(winHwnd1, WM_GETTEXT, strlong, s)
MsgBox Trim(s)
Else
MsgBox "记事本程序没有运行?"
End If
End Function
Private Function setsum() As Long
Dim Phwnd As Long
Dim ChildHwnd As Long
Phwnd = FindWindow(vbNullString, "无标题 - 记事本")
If Phwnd <> 0 Then
ChildHwnd = FindWindowEx(Phwnd, 0, "Edit", vbNullString)
If ChildHwnd <> 0 Then
SendMessage ChildHwnd, WM_SETTEXT, 0, ByVal Text1.Text
Else
MsgBox "找不到相关控件"
End If
Else
MsgBox "计算器程序没有运行"
End If
End FunctionPrivate Sub Command1_Click()
setsum
End SubPrivate Sub Command2_Click()
Getsum
End SubPrivate Sub Form_Load()
Command1.Caption = "向记事本写入文本"
Command2.Caption = "获取记事本中的文本"
Dim ReturnValue, I
ReturnValue = Shell("notepad.exe", 1) ' 运行记事
AppActivate ReturnValue ' 激活记事
Me.AutoRedraw = True
End Sub
然而无奈的是有些软件的Caption是假的,不知道能不能通过点击窗口的方式来获得它的hWnd.
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Sub Command1_Click()
'测试本窗口hwnd
Text2 = Me.hWnd
'测试某个活动窗口hwnd
Me.Hide
Text1 = GetForegroundWindow
Me.Show
End Sub
End Sub