Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName 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 PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long'Post
Private Const WM_KEYDOWN = &H100 '发送一个常规按键(该按键被按下)
Private Const WM_KEYUP = &H101 '发送一个常规按键(该按键被抬起)
Private Const WM_SYSKEYDOWN = &H104 '发送一个系统按键(该按键被按下)
Private Const WM_SYSKEYUP = &H105 '发送一个系统按键(该按键被抬起)
Private Const WM_CHAR = &H102 '定义大写常量(好像是的,呵呵,我也不晓得,我猜的)
Private Const WM_SYSCOMMAND = &H112 '发送一个关闭消息给指定句柄
Private Sub Command1_Click()'向记事本发送消息
Dim a As Long
Dim b As Long
Dim hei As Integer
Dim hao As String
hao = "欢迎使用VB程序!" '设置要发送的文字
a = FindWindow(vbNullString, "新建 文本文档.txt - 记事本") '查找记事本的父句柄
b = FindWindowEx(a, 0, "Edit", vbNullString) '查找记事本编辑框的句柄(子句柄)
hei = 0
1:
hei = hei + 1
PostMessage b, WM_CHAR, Asc(Mid(hao, hei, 1)), 0 'Asc("文字")表示将文字转成ascii码
If hei < Len(hao) Then GoTo 1:
End Sub
Private Sub Command2_Click()'向QQ聊天窗口发送消息
Dim a As Long
Dim b As Long
Dim hei As Integer
Dim hao As String
hao = "欢迎使用VB程序!" '设置要发送的文字
a = FindWindow("TXGuiFoundation", vbNullString) '根据QQ类名查找QQ的父句柄
'这个方法查QQ聊天窗口的句柄好像不行,请使用别的方法
hei = 0
1:
hei = hei + 1
PostMessage a, WM_CHAR, Asc(Mid(hao, hei, 1)), 0 'Asc("文字")表示将文字转成ascii码
If hei < Len(hao) Then GoTo 1:
PostMessage a, WM_KEYDOWN, vbKeyReturn, 0'按键被按下
PostMessage a, WM_KEYUP, vbKeyReturn, 0 '按键被抬起
PostMessage a, WM_SYSCOMMAND, SC_CLOSE, 0'关闭QQ聊天窗口
End Sub
'-----------------------------方法一-----------------------------
Private Declare Function SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Private Const WM_SETTEXT = &HC
Private Sub Command1_Click()
SendMessage Me.hwnd, WM_SETTEXT, 0, ByVal "欢迎使用VB程序!" '
End Sub
'-----------------------------方法二-----------------------------