在VB里怎么用sendmessage实现组合键ctrl+end,要确实可行的办法,请写下详细代码

2024-12-15 10:56:10
推荐回答(1个)
回答1:

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
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const VK_CONTROL = &H11
Private Const VK_END = &H23

Private Sub Command1_Click()
PostMessage Text1.hwnd, WM_KEYDOWN, VK_CONTROL, 0
PostMessage Text1.hwnd, WM_KEYDOWN, VK_END, 0
PostMessage Text1.hwnd, WM_KEYUP, VK_END, 0
PostMessage Text1.hwnd, WM_KEYUP, VK_RETURN, 0
'Text1.SetFocus
End Sub
这是在本地窗口上试验的,先在窗口上添加一个文本框和一个命令按钮,然后把上面的代码复制进去就可以了,如果是其他程序窗口中的文本框,先用findwindowex找到该文本框的句柄,替换掉上面的Text1.hwnd就行了

对于记事本这样界面非常简单(打开程序后焦点直接位于文本框控件之中)的程序,也可以用类似下面的代码:
Private Sub Command1_Click()
Dim lpid As Long
lpid = Shell("notepad.exe c:\a.txt", vbNormalFocus)
AppActivate lpid
SendKeys "^{END}"
End Sub