- 版主
- 211464
- 21164
- 13
- 258 朵
- 47817 个
- 60 个
- 263800
- 2008-12-23
|
1#
t
T
发表于 2009-02-05 13:59
|
|只看楼主
postmessage向记事本发送字符 - Option Explicit
- Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
- Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType 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 Long
- Private Const WM_KEYDOWN = &H100
- Private Const WM_KEYUP = &H101
- Private Const WM_CHAR = &H102
- Function MakeKeyLparam(ByVal VirtualKey As Long, ByVal flag As Long) As Long
- Dim s As String
- Dim Firstbyte As String 'lparam参数的24-31位
- If flag = WM_KEYDOWN Then '如果是按下键
- Firstbyte = "00"
- Else
- Firstbyte = "C0" '如果是释放键
- End If
- Dim Scancode As Long
- '获得键的扫描码
- Scancode = MapVirtualKey(VirtualKey, 0)
- Dim Secondbyte As String 'lparam参数的16-23位,即虚拟键扫描码
- Secondbyte = Right("00" & Hex(Scancode), 2)
- s = Firstbyte & Secondbyte & "0001" '0001为lparam参数的0-15位,即发送次数和其它扩展信息
- MakeKeyLparam = Val("&H" & s)
- End Function
- Private Sub Form_Load()
- Shell "notepad.exe", vbNormalFocus
- Dim NotepadHwnd As Long, hwnd As Long
- NotepadHwnd = FindWindow("notepad", vbNullString)
- '得到窗口类名为Edit的窗口句柄
- hwnd = FindWindowEx(NotepadHwnd, 0, "Edit", vbNullString)
- PostMessage hwnd, WM_KEYDOWN, vbKeyA, MakeKeyLparam(vbKeyA, WM_KEYDOWN) '按下A键
- PostMessage hwnd, WM_CHAR, vbKeyS, MakeKeyLparam(vbKeyS, WM_KEYDOWN) '输入字符A
- PostMessage hwnd, WM_KEYUP, vbKeyY, MakeKeyLparam(vbKeyY, WM_KEYUP) '释放A键
复制代码
|