用VBS控制鼠标(获取鼠标坐标鼠标移动鼠标单击鼠标双击鼠标右击)

Posted 吃饭了吗

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用VBS控制鼠标(获取鼠标坐标鼠标移动鼠标单击鼠标双击鼠标右击)相关的知识,希望对你有一定的参考价值。

Demon‘s Blog

忘记了,喜欢一个人的感觉

用VBS控制鼠标(获取鼠标坐标、鼠标移动、鼠标单击、鼠标双击、鼠标右击)

不止一个人问过我怎样用VBS控制鼠标操作,比如:怎么用VBS获取鼠标指针的坐标位置?怎么用VBS移动鼠标?怎么用VBS模拟鼠标左键单击、左键双击、右键单击?……

网上搜到的答案普遍是VBS无法实现,或者是要用第三方COM(ActiveX?)组件。我对第三方组件是很反感的,使用第三方组件毫无可移植性可言,因为别人的系统中不一定注册了这个组件。我的建议是,尽量不要在VBS代码中调用第三方组件,除非你的程序只是写来自己用。(顺便说一下,也尽量不要用不靠谱的Sendkeys方法,原因不解释)

 

好了,废话就说这么多,现在说说用VBS控制鼠标的方法。我以前写过一篇《VBS调用Windows API函数》,本以为既然都能调用API了,用VBS控制鼠标还不是很简单是事?事实证明我错了,不明真相的同学永远是大多数,不知道API是什么的VBSer大有人在。不贴出实实在在的代码,他们根本不会自己写!

使用此代码的前提是你的系统上安装了Excel,因为要用到Excel.Application对象(如果你偏要认为这算第三方组件我也没话说):

Option Explicit
Dim WshShell
Dim oExcel, oBook, oModule
Dim strRegKey, strCode, x, y
Set oExcel = CreateObject("Excel.Application") ‘创建 Excel 对象
set WshShell = CreateObject("wscript.Shell")

strRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\$\Excel\Security\AccessVBOM"
strRegKey = Replace(strRegKey, "$", oExcel.Version)
WshShell.RegWrite strRegKey, 1, "REG_DWORD"

Set oBook = oExcel.Workbooks.Add ‘添加工作簿
Set oModule = obook.VBProject.VBComponents.Add(1) ‘添加模块
strCode = _
"‘Author: Demon"            & vbCrLf & _
"‘Website: http://demon.tw" & vbCrLf & _
"‘Date: 2011/5/10"          & vbCrLf & _
"Private Type POINTAPI : X As Long : Y As Long : End Type"                                                                                                                            & vbCrLf & _
"Private Declare Function SetCursorPos Lib ""user32"" (ByVal x As Long, ByVal y As Long) As Long"                                                                                     & vbCrLf & _
"Private Declare Function GetCursorPos Lib ""user32"" (lpPoint As POINTAPI) As Long"                                                                                                  & vbCrLf & _
"Private Declare Sub mouse_event Lib ""user32"" Alias ""mouse_event"" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)" & vbCrLf & _
"Public Function GetXCursorPos() As Long"                                                                                                                                             & vbCrLf & _
    "Dim pt As POINTAPI : GetCursorPos pt : GetXCursorPos = pt.X"                                                                                                                     & vbCrLf & _
"End Function"                                                                                                                                                                        & vbCrLf & _
"Public Function GetYCursorPos() As Long"                                                                                                                                             & vbCrLf & _
    "Dim pt As POINTAPI: GetCursorPos pt : GetYCursorPos = pt.Y"                                                                                                                      & vbCrLf & _
"End Function"
oModule.CodeModule.AddFromString strCode ‘在模块中添加 VBA 代码
‘Author: Demon
‘Website: http://demon.tw
‘Date: 2011/5/10
x = oExcel.Run("GetXCursorPos") ‘获取鼠标 X 坐标
y = oExcel.Run("GetYCursorPos") ‘获取鼠标 Y 坐标
WScript.Echo x, y
oExcel.Run "SetCursorPos", 30, 30 ‘设置鼠标 X Y 坐标
Const MOUSEEVENTF_MOVE       = &H1
Const MOUSEEVENTF_LEFTDOWN   = &H2
Const MOUSEEVENTF_LEFTUP     = &H4
Const MOUSEEVENTF_RIGHTDOWN  = &H8
Const MOUSEEVENTF_RIGHTUP    = &H10
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP   = &H40
Const MOUSEEVENTF_ABSOLUTE   = &H8000
‘模拟鼠标左键单击
oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
‘模拟鼠标左键双击(即快速的两次单击)
oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
‘模拟鼠标右键单击
oExcel.Run "mouse_event", MOUSEEVENTF_RIGHTDOWN + MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
‘模拟鼠标中键单击
oExcel.Run "mouse_event", MOUSEEVENTF_MIDDLEDOWN + MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0
‘关闭 Excel
oExcel.DisplayAlerts = False
oBook.Close
oExcel.Quit

注释已经够详细了,要知道我很少写注释的,如果还看不懂,说明你的水平有待提高!

随机文章:

  1. Windows 7在桌面创建宽带连接快捷方式
  2. 批处理技术内幕:IF命令
  3. JavaScript 中小数和大整数的精度丢失
  4. MySQL的Unicode支持
  5. 批处理技术内幕:ECHO命令

12 条评论 发表在“用VBS控制鼠标(获取鼠标坐标、鼠标移动、鼠标单击、鼠标双击、鼠标右击)”上

  1. 技术分享1说道:



以上是关于用VBS控制鼠标(获取鼠标坐标鼠标移动鼠标单击鼠标双击鼠标右击)的主要内容,如果未能解决你的问题,请参考以下文章

如何用VBS代码实现模拟点击鼠标左键

用键盘怎么移动鼠标

matlab没有鼠标怎么操作

api 易语言 获取鼠标点击时的坐标

vbs/vb如何模拟鼠标点击

vb后台模拟鼠标点击网页