怎么用 按钮的句柄 触发按钮
同上。 --------------------编程问答----------------------------------------编程问答-------------------- 没看明白 --------------------编程问答-------------------- 何处不明? --------------------编程问答-------------------- Command1.hwnd 在哪呢 --------------------编程问答-------------------- 还有 我是要 触发其他程序的按钮 --------------------编程问答-------------------- 呵呵
Option Explicit
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
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 Sub Command1_Click()
Command1.Caption = CStr(CLng(Command1.Caption) + 1)
End Sub
Private Sub Form_Load()
Command1.Caption = "0"
Timer1.Interval = 5000
End Sub
'按下鼠标左键马上松开就完成了一次单击
Private Sub Timer1_Timer()
Dim lngP As Long
lngP = PostMessage(Command1.hwnd, WM_LBUTTONDOWN, &H0, &H0) '按下鼠标左键
lngP = PostMessage(Command1.hwnd, WM_LBUTTONUP, &H0, &H0) '松开鼠标左键
End Sub
兄弟,Command1是一个按钮控件,hwnd就是他的句柄属性。通过hwnd可以去的它的句柄。和你的问题完全吻合 --------------------编程问答-------------------- 比如 某个窗口 的某个按钮 的句柄是87675 我要触发它 怎么写 简单点就可以 --------------------编程问答-------------------- 这个的看是不是同一进程,如果不是同一进程中的就首先要使用API函数查找到它的句柄,在发送单击消息,如果是同一进程,那么直接调用它的句柄即可,
不同的窗体,在按钮签名加上窗体名就可以了,比如Form2中的按钮Command1,那么就是:Form2.Command1.hwnd --------------------编程问答-------------------- 额。看来你还是没明白我的意思 比如 我要按计算器 上面的 1 键 怎么做。 这样明白了吧。。 --------------------编程问答-------------------- 那个啥怎么发送单机信息。。。 --------------------编程问答-------------------- 这个很困难,因为你很有可能取不到它的句柄,反正思路是固定的:
1、取得要操作控件的句柄
2、向其发送单击消息
你可以尝试一下使用FindWindow来查找相应按键的句柄。 --------------------编程问答-------------------- 就当作有已经有句柄了吧 SendMessage 句柄 。。。。。然后咋整啊? --------------------编程问答-------------------- 俺是新手 很新很新的 所以问题比较。。。 只想知道方法就可以了 没有要做什么 所以 你简单 的写段代码就可以了 假设已经获得句柄 --------------------编程问答--------------------
??
你把消息发出去了,他就会触发按钮Click事件啊,不就OK了? --------------------编程问答-------------------- SendMessage和PostMessage应该基本一致,我习惯用PostMessage --------------------编程问答-------------------- a = SendMessage(1115258, BM_CLICK, 0, 0) 没效果。 返回0 --------------------编程问答-------------------- a = SendMessage(1115258, BM_CLICK, 0, 0) 没效果。 返回0
红色字体部分错误,请您仔细看我给你的代码,依照它来 --------------------编程问答-------------------- 你刚才写的PostMessage 的那个也没效果 --------------------编程问答-------------------- 这个代码我是通过了调试没有问题的 --------------------编程问答-------------------- --------------------编程问答-------------------- 啊 不行不行啊 你拿计算器 试试 能不能点的。 加我QQ讲吧 这样好麻烦 675942084 --------------------编程问答-------------------- 我都说了啊,计算器你很可能是取不到正确的句柄的,兄弟我要出门了,晚上回来再说吧 --------------------编程问答-------------------- 看看这个,可以点击计算器,如有问题,可以给我留言,我晚上回来在探讨,出去了,拜拜!!
Option Explicit
Dim lngWindowHwnd As Long
Dim lngButton_01_Hwnd As Long
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
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 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 Sub Command1_Click()
Command1.Caption = CStr(CLng(Command1.Caption) + 1)
End Sub
Private Sub Form_Load()
Command1.Caption = "0"
Timer1.Interval = 2000
lngWindowHwnd = FindWindow(vbNullString, "计算器")
lngButton_01_Hwnd = FindWindowEx(lngWindowHwnd, &H0, vbNullString, "1")
End Sub
'按下鼠标左键马上松开就完成了一次单击
Private Sub Timer1_Timer()
Dim lngP As Long
lngP = PostMessage(lngButton_01_Hwnd, WM_LBUTTONDOWN, &H0, &H0) '按下鼠标左键
lngP = PostMessage(lngButton_01_Hwnd, WM_LBUTTONUP, &H0, &H0) '松开鼠标左键
End Sub
前提是把计算器给打开哈,可以点击按钮:1 --------------------编程问答-------------------- 设置按钮 Click 事件一般都用 BM_CLICK
SendMessage 句柄, BM_CLICK, ByVal 0&, ByVal 0&
lz 看看是不是sendmessage 的最后两个参数写错了吧
--------------------编程问答-------------------- 本身按钮的句柄就直接是它的 Hwnd
但是不同的外部程序就得找到它的句柄,它里面的按钮又得往下再找的,
以计算器为例 你打开并故意搞些数字,你按下Command1 则模拟点击计算器的清除按钮 C
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 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const BM_CLICK = &HF5
Dim ChkHwnd&, ChildHwnd&
Private Sub Command1_Click()
ChkHwnd = FindWindow(vbNullString, "计算器") '获取计算器窗口句柄
ChildHwnd = FindWindowEx(ChkHwnd, 0&, "Button", "C") '获取 "C" 按钮句柄
If ChildHwnd <> 0 Then SendMessage ChildHwnd, BM_CLICK, ByVal 0&, ByVal 0&
End Sub --------------------编程问答-------------------- 单击应该是发送两个消息。一个左键按下消息 ,一个左键抬起消息。WM_LBUTTONDOWN,WM_LBUTTONUP构成单击。 --------------------编程问答-------------------- --------------------编程问答-------------------- 这是个好问题 不过看样子还没解决? --------------------编程问答--------------------
老帖子了......
Const BM_CLICK = &HF5
重点在这行 你没宣告常量 --------------------编程问答-------------------- 你的触发按钮是指达到像单击按钮那样的效果吗?还有就是在什么情况下用句柄出发按钮?没有这些背景很难答。 --------------------编程问答-------------------- --------------------编程问答-------------------- 触发按钮是指达到像单击按钮那样的效果,谁能给一段简单的程序,谢谢 --------------------编程问答--------------------
上面两位大侠已经给了你完整代码了,,你还想怎样。。。呵呵 --------------------编程问答-------------------- 我来给你解释下 如果还不明白。。那你。。。
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'这个是查找父窗口的api函数,如果窗口被找到,函数返回该窗口的句柄,即hwnd,如果没有即为0
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
'这个也是查找窗口的函数,与上一个函数的不同处在于,这个是查找二级或其下的子窗口(某个大窗口里面的东西),如要查询计算器窗口的句柄就用第一个api函数,得到后,再使用第二个函数继续查找子窗口,当然这里要指定父窗口的句柄,这个句柄就是你使用第一个api函数的值
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const BM_CLICK = &HF5
'此api函数是实现你功能的重要函数,即消息的投递,字面意思是“发送消息”
Dim ChkHwnd&, ChildHwnd&
Private Sub Command1_Click()
ChkHwnd = FindWindow(vbNullString, "计算器") '获取计算器窗口句柄
ChildHwnd = FindWindowEx(ChkHwnd, 0&, "Button", "C") '根据子窗口文本或取 "C" 按钮句柄
If ChildHwnd <> 0 Then SendMessage ChildHwnd, BM_CLICK, ByVal 0&, ByVal 0&
如果反回事为0,即没有找到,如果不为0,即发送消息Const BM_CLICK = &HF5给找到的句柄,
End Sub
一切OK --------------------编程问答-------------------- 我的C#
const int WM_KEYDOWN = 0x100;//按下一个键
const int WM_KEYUP = 0x101;//释放一个键
PostMessage(childHwnd4, (int)WM_KEYDOWN, IntPtr.Zero, 0);
PostMessage(childHwnd4, (int)WM_KEYUP, IntPtr.Zero, 0);
怎么就实现不了呢? --------------------编程问答-------------------- 今天第二次见到了成功的代码!
我用记事本中写入
wscript.sleep 5000
msgbox"请单击“取消”继续学习!",1,"来自网页的消息"
后缀名保存为vbs即可!
下载了API大全
'api声明
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'findwindow的API声明
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
'findwindowex的API声明
Private Declare Function SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Private Declare Function SendMessageBynum& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
Private Declare Function SendMessageByString& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String)
'sendmessage的API声明,注:我用的vb6.0如果Declare前面不加Private API声明不能用!
'方法一
Const BM_CLICK = &HF5 ’加入了29楼的关键语句!成功了!
Private Sub Command1_Click()
Dim i, j As Long
i = FindWindow(vbNullString, "来自网页的消息")
If i > 0 Then
j = FindWindowEx(i, 0, "button", "取消")
MsgBox i
MsgBox j
a = SendMessage(j, BM_CLICK, 0, 0)'这句还可改为SendMessage j, BM_CLICK, 0, 0
End If
End Sub
'方法二
Private Const WM_LBUTTONDOWN = &H201 '声明常量
Private Const WM_LBUTTONUP = &H202
Private Sub Command1_Click()
Dim i, j As Long
i = FindWindow(vbNullString, "来自网页的消息")
If i > 0 Then
j = FindWindowEx(i, 0, "button", "取消")
MsgBox i
MsgBox j
PostMessage j, WM_LBUTTONDOWN, 0, ByVal (1 + 1 * 65536)
PostMessage j, WM_LBUTTONUP, 0, ByVal (1 + 1 * 65536)
'我一直奇怪这句这样写就不会成功!!!
PostMessage j, WM_LBUTTONDOWN, 0, 0
PostMessage j, WM_LBUTTONUP, 0, 0
End If
End Sub
这是我查了一上午的百度 到现在只成功了两个例子!
补充:VB , API