| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5198 人关注过本帖
标题:如何实现vb 模拟鼠标点击其他应用程序内的命令按键
只看楼主 加入收藏
kings12333
Rank: 2
等 级:论坛游民
帖 子:114
专家分:66
注 册:2012-11-29
收藏
得分:0 
回复 29楼 William1949
按照你说的方法已经测试好了,经测试发现对于类型为:ToolbarWindow32的可以有效,但针对VB ToolBar控件类型 msvb_lib_toolbar 无效,但可以变更按钮状态,不知为何。
2022-05-24 18:57
kings12333
Rank: 2
等 级:论坛游民
帖 子:114
专家分:66
注 册:2012-11-29
收藏
得分:0 
回复 30楼 yuma
我在WIN7 测试的
2022-05-24 18:58
William1949
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:111
专家分:0
注 册:2009-3-17
收藏
得分:0 
以下是引用kings12333在2022-5-24 18:57:25的发言:

按照你说的方法已经测试好了,经测试发现对于类型为:ToolbarWindow32的可以有效,但针对VB ToolBar控件类型 msvb_lib_toolbar 无效,但可以变更按钮状态,不知为何。


不会吧?你试以下代码

Option Explicit
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const PROCESS_VM_OPERATION = &H8
Private Const PROCESS_VM_READ = &H10
Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Const MEM_COMMIT = &H1000
Private Const PAGE_READWRITE = &H4
Private Const MEM_RELEASE = &H8000&
Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long

Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) 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
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_COMMAND = &H111

Private Type TBBUTTON
   iBitmap      As Long
   idCommand    As Long
   fsState      As Byte
   fsStyle      As Byte
   bReserved1   As Byte
   bReserved2   As Byte
   dwData       As Long
   iString      As Long
End Type
Private Const TB_GETBUTTON = &H417

Private Sub Command1_Click()
    Dim hWnd As Long, Index As Long
   
    hWnd = 指定 toolbar 句柄
    Index = 指定 toolbar 索引,范围 = 0 ~ 按钮总数-1
    Call GotoButtonClick(hWnd, Index)
End Sub

Private Sub GotoButtonClick(ByVal hWnd As Long, ByVal Index As Long)
'hWnd      toolbar 句柄;
'Index     toolbar 索引,范围 = 0 ~ 按钮总数-1;
    Dim hParent As Long, ButtonID As Long
  
    hParent = GetParent(hWnd)
    ButtonID = GetButtonID(hWnd, Index)
    Call PostMessage(hParent, WM_COMMAND, ButtonID, ByVal hWnd)
End Sub

Private Function GetButtonID(ByVal hWnd As Long, ByVal Index As Long) As Long
    Dim ProcesshWnd As Long, ProcessId As Long, Buffer As Long, Len1 As Long
    Dim iButton     As TBBUTTON
   
    Len1 = LenB(iButton)
    Call GetWindowThreadProcessId(hWnd, ProcessId)
    ProcesshWnd = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ, 0, ProcessId)
    Buffer = VirtualAllocEx(ProcesshWnd, 0, Len1, MEM_COMMIT, PAGE_READWRITE)
   
    Call SendMessage(hWnd, TB_GETBUTTON, Index, ByVal Buffer)
    Call ReadProcessMemory(ProcesshWnd, Buffer, iButton, Len1, 0)
   
    GetButtonID = iButton.idCommand
    Call VirtualFreeEx(ProcesshWnd, Buffer, 0, MEM_RELEASE)
    Call CloseHandle(ProcesshWnd)
End Function

2022-05-24 19:11
kings12333
Rank: 2
等 级:论坛游民
帖 子:114
专家分:66
注 册:2012-11-29
收藏
得分:0 
回复 33楼 William1949
经测试代码对 VB ToolBar控件类 msvb_lib_toolbar,可以有效实现点击效果(ToolBarWin32类型的也同样有效),我对比了一下我的和你的使用函数的方式不同而已:
SendClick = PostMessage(Hwnd_JY, WM_COMMAND, lButtonID, 0) 'PostMessage(主窗口句柄, WM_COMMAND, 控件ID号, 0)
'--------------------------------------------'
Call PostMessage(hParent, WM_COMMAND, ButtonID, ByVal hWnd)
但是,接下来我用你的代码测试了FoxMail软件的TooBar类型为:TTBXToolbar ,代码是无效,


[此贴子已经被作者于2022-5-24 19:49编辑过]

2022-05-24 19:47
kings12333
Rank: 2
等 级:论坛游民
帖 子:114
专家分:66
注 册:2012-11-29
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
2022-05-24 19:49
William1949
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:111
专家分:0
注 册:2009-3-17
收藏
得分:0 
以下是引用kings12333在2022-5-24 19:47:35的发言:

经测试代码对 VB ToolBar控件类 msvb_lib_toolbar,可以有效实现点击效果(ToolBarWin32类型的也同样有效),我对比了一下我的和你的使用函数的方式不同而已:
SendClick = PostMessage(Hwnd_JY, WM_COMMAND, lButtonID, 0) 'PostMessage(主窗口句柄, WM_COMMAND, 控件ID号, 0)
'--------------------------------------------'
Call PostMessage(hParent, WM_COMMAND, ButtonID, ByVal hWnd)
但是,接下来我用你的代码测试了FoxMail软件的TooBar类型为:TTBXToolbar ,代码是无效,


第一:
你那个“Hwnd_JY”是什么?“主窗口句柄”是什么?是顶级窗口吗?如果是,那可能错了,你可发送到TooBar的父级,或直接发送到TooBar的窗口过程。

第二:
lparam,你设为0?应该指定TooBar的句柄吧?参考以下网址的Remarks部份:
https://docs.
图片附件: 游客没有浏览图片的权限,请 登录注册


第三:
看你的图,那个按钮旁有个小箭头,是下拉按钮吗?如果是?那可能是带有BTNS_WHOLEDROPDOWN 样式的按钮,其单击事件是由TBN_DROPDOWN消息产生的,而不是WM_COMMAND,所以发送失败。(不确定用你自己的代码,针对这种下拉按钮,是否成功,好奇!)

第四:
TTBXToolbar 类是什么?我没有FoxMail软件,没办法测试,它是由ToolbarWindow32派生出来的?还是用gdi自己画出来的“山寨 toolbar”

第五:
在想........
2022-05-25 10:31
kings12333
Rank: 2
等 级:论坛游民
帖 子:114
专家分:66
注 册:2012-11-29
收藏
得分:0 
回复 36楼 William1949
1.SendClick = PostMessage(Hwnd_JY, WM_COMMAND, lButtonID, 0) 'PostMessage(主窗口句柄, WM_COMMAND, 控件ID号, 0) 此种写法也是成功的,Hwnd_JY是主窗口句柄;
2.此种方式类似于发送菜单ID的方式,经测试没有问题
3.Foxmail软件百度随便可以下载安装,TTBXToolbar 类有可能是它自己的,搞不明白,不是toolBarWindows32的类 不会起作用的
2022-05-25 10:42
kings12333
Rank: 2
等 级:论坛游民
帖 子:114
专家分:66
注 册:2012-11-29
收藏
得分:0 
回复 36楼 William1949
又有个新东西,也是模拟点击Sys32TreeView,目前也同样获取了 节点的ID,发送点击事件WM_COMMAND没反应.
2022-06-09 09:47
快速回复:如何实现vb 模拟鼠标点击其他应用程序内的命令按键
数据加载中...
 
   



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

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