1 用postmessage发送组合键很复杂,不能简单的照搬上面的代码(何况,你照搬的方法也不对)2 对记事本上的edit窗口发送ctrl-f4,本身就应该什么也不发生3 可以用下面的代码: Option ExplicitPrivate Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long 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 LongPrivate Sub Command1_Click() Dim jsb As Long 'jsb = FindWindow("notepad", vbNullString) '可以把vbnullstring改为你需要的 jsb = FindWindow(vbNullString, "记事本 - 记事本") '不知道你为什么一定要这么写,难道你的程序不是系统自带的notepad吗? If jsb = 0 Then MsgBox "没有符合要求的窗口" Exit Sub End If Dim mhwnd As Long mhwnd = FindWindowEx(jsb, 0, "edit", vbNullString) Dim tid As Long, pid As Long tid = GetWindowThreadProcessId(mhwnd, pid) AttachThreadInput tid, App.ThreadID, True Putfocus mhwnd SendKeys "this is test" SendKeys "^v", True '发送ctrl-v SendKeys "%{F4}", True '发送alt-f4 '如果你要发送ctrl-f4的话,就取消下面的注释 'SendKeys "^{F4}", True '发送ctrl-f4 AttachThreadInput tid, App.ThreadID, False Putfocus Me.hwnd End Sub 这个也是暴风雪的
sendkeys:
SendKeys "%A" 'alt-a
sendkeys "^b" 'ctrl-b
需要你注意的是,要想发送成功,需要使目标窗口处于活动状态(可用showwindow、setfocus之类的api,也可用vb自带的AppActivate)当年的暴风雪老大说的
Option ExplicitPrivate Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
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 LongPrivate Sub Command1_Click()
Dim jsb As Long
'jsb = FindWindow("notepad", vbNullString) '可以把vbnullstring改为你需要的
jsb = FindWindow(vbNullString, "记事本 - 记事本") '不知道你为什么一定要这么写,难道你的程序不是系统自带的notepad吗?
If jsb = 0 Then
MsgBox "没有符合要求的窗口"
Exit Sub
End If
Dim mhwnd As Long
mhwnd = FindWindowEx(jsb, 0, "edit", vbNullString)
Dim tid As Long, pid As Long
tid = GetWindowThreadProcessId(mhwnd, pid)
AttachThreadInput tid, App.ThreadID, True
Putfocus mhwnd
SendKeys "this is test"
SendKeys "^v", True '发送ctrl-v
SendKeys "%{F4}", True '发送alt-f4
'如果你要发送ctrl-f4的话,就取消下面的注释
'SendKeys "^{F4}", True '发送ctrl-f4
AttachThreadInput tid, App.ThreadID, False
Putfocus Me.hwnd
End Sub
这个也是暴风雪的