用VBS控制鼠标的实现代码(获取鼠标坐标、鼠标移动、鼠标单击、鼠标双击、鼠标右击)
程序员文章站
2022-03-07 12:45:00
怎么用vbs模拟鼠标左键单击、左键双击、右键单击?…… 网上搜到的答案普遍是vbs无法实现,或者是要用第三方com(activex?)组件。我对第三方组件是很反感的,使用第...
怎么用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
网上搜到的答案普遍是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
上一篇: 四步轻松实现ajax发送异步请求
下一篇: 致远OA 文件上传漏洞202010