| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5238 人关注过本帖
标题:如何实现vb 模拟鼠标点击其他应用程序内的命令按键
只看楼主 加入收藏
jklqwe111
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:35
帖 子:336
专家分:1135
注 册:2014-4-13
收藏
得分:4 
SetCursorPos是设置光标而不是鼠标。
使用mouse_event 移动鼠标到指定位置。
以下代码供参考:
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_MOVE =1 '移动鼠标标志
Private Const MOUSEEVENTF_ABSOLUTE =32768 '使用绝对坐标
Private sub command1
mouse_event MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE ,Val(Text1.Text), Val(Text2.Text),0,0

mouse_event MOUSEEVENTF_LEFTDOWN or MOUSEEVENTF_LEFTUP , 0, 0, 0, 0

end sub
Private sub command2
msgbox"模拟鼠标按键"
end sub
2022-05-16 15:15
yjz2022
Rank: 1
等 级:新手上路
威 望:1
帖 子:17
专家分:0
注 册:2022-4-27
收藏
得分: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
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
'鼠标按下
Const WM_LBUTTONDOWN = &H201
'鼠标弹出
Const WM_LBUTTONUP = &H202
'鼠标双击
Const WM_COMMAND = &H203
 
Private Sub Command1_Click()
Dim lngHWnd1 As Long, lngHWnd2 As Long
 
lngHWnd1 = FindWindow(vbNullString, "你要控制窗体的标题")
  If lngHWnd1 <> 0 Then
  lngHWnd2 = FindWindowEx(lngHWnd1, 0, vbNullString, "里面窗体或控件的标题")
   
  If lngHWnd2 <> 0 Then
  SendMessage lngHWnd2, WM_LBUTTONDOWN, 0&, 0&
  SendMessage lngHWnd2, WM_LBUTTONUP, 0&, 0&
   
  End If
  Else
  lngHWnd2 = 0
  End If
End Sub
2022-05-16 16:09
wds1
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:49
帖 子:393
专家分:2025
注 册:2016-3-10
收藏
得分:4 
简单鼠标控制.zip (5.26 KB)

1、可以获取屏幕鼠标位置
2、可以点击设置位置的鼠标
3、源程序

Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Private Type POINTAPI
 x As Long
 y As Long
End Type

'启动获得坐标定时器
Private Sub Command1_Click()
  If Command1.Caption = "获取屏幕坐标" Then
    Timer1.Interval = 200
    Command1.Caption = "F12停止获取"
  End If
End Sub
'设置鼠标位置并点击
Private Sub Command2_Click()
  SetCursorPos Val(Text1.Text), Val(Text2.Text)                      '设置鼠标位置
  mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 '点击坐标位置鼠标
  DoEvents
End Sub
'获得鼠标位置和键盘输入
Private Sub Timer1_Timer()
  Dim P As POINTAPI
  GetCursorPos P    '取的鼠标位置
  Text1.Text = P.x
  Text2.Text = P.y
  KeyResult = GetAsyncKeyState(123) '取的F12
  If KeyResult = -32767 Then
    Timer1.Interval = 0
    Command1.Caption = "获取屏幕坐标"
  End If
End Sub
2022-05-16 16:44
kings12333
Rank: 2
等 级:论坛游民
帖 子:114
专家分:66
注 册:2012-11-29
收藏
得分:4 
回复 4楼 风吹过b
版主,请教一个问题:VB如何实现对第三方界面的Botton按钮实现用 控件的"相对坐标"实现后台点击,
网上没看到相关案例;一般模拟鼠标需要将界面置前,鼠标再移到按钮那里API实现点击;
因为针对toolbar控件无法找到子按钮的句柄,所以我的想法是想找到窗体,再找到toolbar控件在窗体的位置,再根据坐标来实现后台点击,一直没找到更好办法,望解答,谢谢!
2022-05-21 20:20
yuma
Rank: 12Rank: 12Rank: 12
来 自:银河系
等 级:贵宾
威 望:37
帖 子:1934
专家分:3012
注 册:2009-12-22
收藏
得分:4 
回复 14楼 kings12333
不需要坐标,通过遍历第三方程序的控件句柄,直接向控件句柄发送单击或双击消息,即可完成单击或双击。

下面代码通过第三方程序的句柄,遍历其中包含的控件句柄。

程序代码:
'注意本程序有些控件句柄是获取不到的。

Private Const GW_CHILD = 5
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long


Private Sub FillChild(hWndParent As Long)
Dim hWndChild As Long
Dim szCaption As String
Dim buffer As String
Dim i As Long

hWndChild = GetWindow(hWndParent, GW_CHILD)
If (hWndChild = 0) Then Exit Sub
hWndChild = GetWindow(hWndChild, GW_HWNDFIRST)
If hWndChild = 0 Then Exit Sub

While (hWndChild <> 0)
szCaption = String$(255, 0)
GetClassName hWndChild, szCaption, 250
szCaption = Left$(szCaption, InStr(szCaption, String$(1, 0)) - 1)
buffer = CStr(hWndChild) & "--" & szCaption
i = GetWindowTextLength(hWndChild)
szCaption = String$(255, 0)
GetWindowText hWndChild, szCaption, 250
szCaption = Left$(szCaption, i)
buffer = buffer & "--" & szCaption

Debug.Print buffer
FillChild hWndChild
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
Wend
End Sub

Private Sub GetChildWindow(hwnd As Long)
Dim szCaption As String
Dim buffer As String

Dim i As Long


szCaption = String$(255, 0)
GetClassName hwnd, szCaption, 250
szCaption = Left$(szCaption, InStr(szCaption, String$(1, 0)) - 1)

buffer = CStr(hwnd)
buffer = buffer & "--" & szCaption
i = GetWindowTextLength(hwnd)
szCaption = String$(255, 0)
GetWindowText hwnd, szCaption, 250
szCaption = Left$(szCaption, i)
buffer = buffer & "--" & szCaption
Debug.Print buffer
FillChild hwnd
End Sub

Private Sub Form_Load()
GetChildWindow 3277732 '3277732是指定的窗口句柄,hwnd则为自身句柄
End Sub

心生万象,万象皆程序!
本人计算机知识网:http://bbs.为防伸手党,本站已停止会员注册。
2022-05-21 22:12
kings12333
Rank: 2
等 级:论坛游民
帖 子:114
专家分:66
注 册:2012-11-29
收藏
得分:0 
回复 15楼 yuma
高手,ToolBar的子按钮没有句柄啊。。常见的Botton是很简单可以搞定,但针对ToolBar 这类控件我能想到的就是通过坐标来触发是最好的;你有这方面的案例吗?
2022-05-21 22:32
yuma
Rank: 12Rank: 12Rank: 12
来 自:银河系
等 级:贵宾
威 望:37
帖 子:1934
专家分:3012
注 册:2009-12-22
收藏
得分:0 
API操作其他程序中的按钮
https://blog.

心生万象,万象皆程序!
本人计算机知识网:http://bbs.为防伸手党,本站已停止会员注册。
2022-05-21 22:35
yuma
Rank: 12Rank: 12Rank: 12
来 自:银河系
等 级:贵宾
威 望:37
帖 子:1934
专家分:3012
注 册:2009-12-22
收藏
得分:0 
LookHandles 2.3.zip (37.28 KB)


用这个软件查下看,是不是真的没有句柄。

查不到的句柄,可能是隐藏了。

[此贴子已经被作者于2022-5-21 22:40编辑过]


心生万象,万象皆程序!
本人计算机知识网:http://bbs.为防伸手党,本站已停止会员注册。
2022-05-21 22:35
kings12333
Rank: 2
等 级:论坛游民
帖 子:114
专家分:66
注 册:2012-11-29
收藏
得分:0 
回复 18楼 yuma
ToolBar控件有句柄,但里面有子按钮,子按钮没找到句柄
2022-05-21 22:43
yuma
Rank: 12Rank: 12Rank: 12
来 自:银河系
等 级:贵宾
威 望:37
帖 子:1934
专家分:3012
注 册:2009-12-22
收藏
得分:0 
找不到句柄,你是无法通过第三方程序操作它的。获取它的坐标也做不到。

心生万象,万象皆程序!
本人计算机知识网:http://bbs.为防伸手党,本站已停止会员注册。
2022-05-22 12:43
快速回复:如何实现vb 模拟鼠标点击其他应用程序内的命令按键
数据加载中...
 
   



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

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