VB postmessage的使用方法要详细
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
Private Const VK_A = &H41
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()
End Sub
Private Sub Timer1_Timer()
Dim Phwnd As Long
Dim hwnd As Long
Phwnd = FindWindow(vbNullString, "新建 文本文档.txt - 记事本")
If Phwnd = 0 Then
Text1.Text = "fill"
Else
Text1.Text = "phwnd"
End If
hwnd = FindWindowEx(Phwnd, 0, "edit", vbNullString)
If hwnd = 0 Then
Text2.Text = "fill"
Else
Text2.Text = "ChildHwnd"
End If
PostMessage hwnd, WM_KEYDOWN, VK_A, MakeKeyLparam(VK_A, WM_KEYDOWN) ''按下A键
PostMessage hwnd, WM_CHAR, Asc("你"), MakeKeyLparam(VK_A, WM_KEYDOWN)
PostMessage hwnd, WM_CHAR, Asc("好"), MakeKeyLparam(VK_A, WM_KEYDOWN)
PostMessage hwnd, WM_CHAR, Asc("啊"), MakeKeyLparam(VK_A, WM_KEYDOWN)
PostMessage hwnd, WM_CHAR, Asc("!"), MakeKeyLparam(VK_A, WM_KEYDOWN) ''输入字符A
PostMessage hwnd, WM_UP, VK_A, MakeKeyLparam(VK_A, WM_UP) ''释放A键
End Sub
这个自动向那个记事本发送文字,”你好啊!“可以在不是活动窗口的时候发送,也就是说可以在你用QQ和别人在聊天这个程序也会运行,不会干扰你和别人聊天。。~~
第一个参数就是要投递消息的控件或窗口句柄~
第二个是消息常数
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Dim hwd As Long
Private Sub Command1_Click()
Shell "notepad.exe", 1
hwd = FindWindow(vbNullString, "无标题 - 记事本")
End Sub
Private Sub Command2_Click()
PostMessage hwd, &H10, 0, 0
End Sub
Private Sub Form_Load()
Command1.Caption = "打开记事本"
Command2.Caption = "关闭记事本"
End Sub