运维开发网

用VBS控制鼠标的实现代码(获取鼠标坐标、鼠标移动、鼠标单击、鼠标双击、鼠标右击)

运维开发网 https://www.qedev.com 2020-01-28 09:38 出处:网络 作者: 网络整理
不止一个人问过我怎样用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

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

原文:http://demon.tw/programming/vbs-control-mouse.html

0

精彩评论

暂无评论...
验证码 换一张
取 消