| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4428 人关注过本帖
标题:VB Sendmessage问题
取消只看楼主 加入收藏
lkflsj
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-9-22
结帖率:0
收藏
 问题点数:0 回复次数:3 
VB Sendmessage问题
Private Declare Function SendMessage& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Private Declare Function PostMessage& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Const WM_KEYDOWN = &H100
Const WM_KEYUP = &H101
Const VK_F1 = &H70
Const VK_F2 = &H71
Const VK_F3 = &H72
Const VK_F4 = &H73
Const VK_F5 = &H74
Const VK_A = &H41
Const WM_CLOSE = &H10
Private Sub Command1_Click()
Shell "notepad.exe", 1
End Sub
Private Sub Command2_Click()
Dim hwd As Long
hwd = FindWindow(vbNullString, "无标题 - 记事本")
SendMessage hwd, WM_KEYDOWN, VK_F5, 0&
End Sub
Private Sub Form_Load()
Command1.Caption = "打开记事本"
Command2.Caption = "关闭记事本"
End Sub
该例子模拟按下F5有效
Private Declare Function SendMessage& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Private Declare Function PostMessage& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Const WM_KEYDOWN = &H100
Const WM_KEYUP = &H101
Const VK_F1 = &H70
Const VK_F2 = &H71
Const VK_F3 = &H72
Const VK_F4 = &H73
Const VK_F5 = &H74
Const VK_A = &H41
Const WM_CLOSE = &H10
Private Sub Command1_Click()
Shell "notepad.exe", 1
End Sub
Private Sub Command2_Click()
Dim hwd As Long
hwd = FindWindow(vbNullString, "无标题 - 记事本")
SendMessage hwd, WM_KEYDOWN, VK_A, 0&
End Sub
Private Sub Form_Load()
Command1.Caption = "打开记事本"
Command2.Caption = "关闭记事本"
End Sub
该例子模拟按下A无效,求解
搜索更多相关主题的帖子: 问题 
2011-05-28 21:25
lkflsj
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-9-22
收藏
得分:0 
一般目标程序都会含有多个控件,并不是每个控件都会对键盘消息作出反应,只有把键盘消息发送给接受它的控件才会得到期望 的反应。那记事本来说,它的编辑框其实是个edit类,只有这个控件才对键盘事件有反应,如果只是把消息发给记事本的窗体,那是没有用的。

hwnd = FindWindow("Notepad", "无标题 - 记事本") '得到记事本句柄
Thwnd = FindWindowEx(hwnd, 0, "Edit", vbNullString) '得到记事本编辑区的句柄

SendMessage Thwnd , WM_KEYDOWN, VK_A, 0& '按下A键


希望对你有帮助!
这是百度提问里一位高人给的答案,还是谢谢楼上的回答
2011-05-29 07:01
lkflsj
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-9-22
收藏
得分:0 
不知道论坛有没有知道怎么获取目标程序的控件类名,前面提到的记事本
Thwnd = FindWindowEx(hwd, 0, "Edit", vbNullString) '得到记事本编辑区的句柄
"edit"是属于类名,这个是记事本的,但比如我打开某个网络游戏的话,界面就有很多控件,每个控件都有个类名,VB中怎么查找出这些控件的类名是什么?
2011-05-29 11:17
lkflsj
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-9-22
收藏
得分:0 
3.SendMessage Thwnd, &H286, l, 0& '写单个字符(之前写VK_F5可以直接带出时间字符串~怎这次要用循环来带出?)

SendMessage Thwnd, &H100, &H74, 0& 直接输出时间字符串,这里向notepad发送按下F5的按键,并不是发送时间字符串到记事本
2011-05-29 11:29
快速回复:VB Sendmessage问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038428 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved