参考:
http://demon.tw/programming/vbs-control-mouse.html
https://blog.csdn.net/tiantuanzi/article/details/47838161
图片来自网络
'wscript c:\Users\Administrator\Desktop\VBS\SL_test.vbs
Option Explicit
Dim WshShell
Dim oExcel, oBook, oModule
Dim strRegKey, strCode, x, y
Dim time
'Dim longtime
time = 1000
'longtime = 500
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 = _
"Private Type POINTAPI : X As Long : Y As Long : End Type" & vbCrLf & _
"Private Declare PtrSafe Function SetCursorPos Lib ""user32"" (ByVal x As Long, ByVal y As Long) As Long" & vbCrLf & _
"Private Declare PtrSafe Function GetCursorPos Lib ""user32"" (lpPoint As POINTAPI) As Long" & vbCrLf & _
"Private Declare PtrSafe 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)" & vbCrLf & _
"Private Const MOUSEEVENTF_MOVE = &H1" & vbCrLf & _
"Private Const MOUSEEVENTF_LEFTDOWN = &H2" & vbCrLf & _
"Private Const MOUSEEVENTF_LEFTUP = &H4" & vbCrLf & _
"Private Const MOUSEEVENTF_RIGHTDOWN = &H8" & vbCrLf & _
"Private Const MOUSEEVENTF_RIGHTUP = &H10" & vbCrLf & _
"Private Const MOUSEEVENTF_MIDDLEDOWN = &H20" & vbCrLf & _
"Private Const MOUSEEVENTF_MIDDLEUP = &H40" & vbCrLf & _
"Private Const MOUSEEVENTF_ABSOLUTE = &H8000" & 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" & vbCrLf & _
"Private Sub SetCursor(x,y)" & vbCrLf & _
"SetCursorPos x, y" & vbCrLf & _
"End Sub" & vbCrLf & _
"Private Sub Setmouse_L_0()" & vbCrLf & _
"SetCursor 10,10" & vbCrLf & _
"mouse_event MOUSEEVENTF_LEFTDOWN,0,0,0,0" & vbCrLf & _
"mouse_event MOUSEEVENTF_LEFTUP,0,0,0,0" & vbCrLf & _
"End Sub" & vbCrLf & _
"Private Sub Setmouse_R_1()" & vbCrLf & _
"SetCursor 200,360" & vbCrLf & _
"mouse_event MOUSEEVENTF_RIGHTDOWN,0,0,0,0" & vbCrLf & _
"mouse_event MOUSEEVENTF_RIGHTUP,0,0,0,0" & vbCrLf & _
"End Sub" & vbCrLf & _
"Private Sub Setmouse_R_2()" & vbCrLf & _
"SetCursor 252,587" & vbCrLf & _
"mouse_event MOUSEEVENTF_RIGHTDOWN,0,0,0,0" & vbCrLf & _
"mouse_event MOUSEEVENTF_RIGHTUP,0,0,0,0" & vbCrLf & _
"End Sub"
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 "SetCursor", 30, 30 '设置鼠标 X Y 坐标
'窗口坐标1,1
'oExcel.Run "SetCursor", 1, 1 '设置鼠标 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
WScript.Sleep 1000
WScript.Sleep time
'模拟鼠标左键单击
'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
'调用Setmouse_L_0函数
'oExcel.Run "Setmouse_L_0"
'WScript.Sleep 1000
'关闭 Excel
'oExcel.DisplayAlerts = False
'oBook.Close
'oExcel.Quit
'Set WshShell= WScript.Createobject("WScript.shell")
'LOOP
Dim count
Dim count_flag_IO
Dim count_flag_Factor
do while 0<1
count = count + 1
'显示时长2s
Print count,2,"count开始"
'选择窗口
oExcel.Run "Setmouse_L_0"
WScript.Sleep 1000
'PLC-坐标1-120,355-右键
oExcel.Run "Setmouse_R_1"
WScript.Sleep 1000
'配置界面向上移动一下进入属性界面
WScript.Sleep time
WshShell.SendKeys "{UP}"
WScript.Sleep time
WshShell.SendKeys "{ENTER}"
'打开PLC属性窗口-ALT+ENTER
'WScript.Sleep time
'WshShell.SendKeys "%~"
'进入常规窗口
WScript.Sleep time
WshShell.SendKeys "{TAB}"
'进入IO通信窗口
WScript.Sleep time
WshShell.SendKeys "{DOWN}"
'进入IO周期配置窗口
WScript.Sleep time
WshShell.SendKeys "{TAB 3}"
'配置不同IO时钟-4为单位
'先恢复到4.00ms
WScript.Sleep time
WshShell.SendKeys "{DOWN 30}"
'计算次数-取余
count_flag_IO = count mod 5
'设置IO到不同的IO周期
WScript.Sleep time
'3.5ms-3.0ms-2.5ms-2.0ms-1.5ms-4.0ms
if count_flag_IO = 0 then
WshShell.SendKeys "{UP 4}"
elseif count_flag_IO = 1 then
WshShell.SendKeys "{UP 8}"
elseif count_flag_IO = 2 then
WshShell.SendKeys "{UP 12}"
elseif count_flag_IO = 3 then
WshShell.SendKeys "{UP 16}"
elseif count_flag_IO = 4 then
WshShell.SendKeys "{UP 20}"
else
count_flag_IO = 0
'WshShell.SendKeys "{UP 0}"
end if
'IO周期配置完成-确认
WScript.Sleep time
WshShell.SendKeys "{ENTER}"
'MC-Servo[OB91]-坐标190,585-右键
'向下移动-进入属性配置
oExcel.Run "Setmouse_R_2"
WScript.Sleep 1000
'配置界面向上移动一下进入属性界面
WScript.Sleep time
WshShell.SendKeys "{UP}"
WScript.Sleep time
WshShell.SendKeys "{ENTER}"
'进入常规窗口
WScript.Sleep time
WshShell.SendKeys "{TAB}"
'进入周期窗口
WScript.Sleep time
WshShell.SendKeys "{DOWN}"
'进入因子配置选框
WScript.Sleep time
WshShell.SendKeys "{TAB 6}"
'因子恢复为1-向上20次,因子最小就是1
WScript.Sleep time
WshShell.SendKeys "{UP 20}"
'计算次数-取余
count_flag_Factor = count mod 4
'设置因子为不同的值
WScript.Sleep time
'3-5-7-9-1
if count_flag_Factor = 0 then
WshShell.SendKeys "{DOWN 2}"
elseif count_flag_Factor = 1 then
WshShell.SendKeys "{DOWN 4}"
elseif count_flag_Factor = 2 then
WshShell.SendKeys "{DOWN 6}"
elseif count_flag_Factor = 3 then
WshShell.SendKeys "{DOWN 8}"
else
count_flag_Factor = 0
'WshShell.SendKeys "{DOWN 0}"
end if
'同步因子配置完成-确认
WScript.Sleep time
WshShell.SendKeys "{ENTER}"
'下载程序
'PLC-坐标1-120,355-右键
oExcel.Run "Setmouse_R_1"
WScript.Sleep 1000
'选中下载
WScript.Sleep time
WshShell.SendKeys "{DOWN 13}"
WScript.Sleep time
WshShell.SendKeys "{RIGHT}"
'进入下载
WScript.Sleep time
WshShell.SendKeys "{ENTER}"
'确认下载-等待检查程序-编译
WScript.Sleep 10000
WshShell.SendKeys "{ENTER}"
'等待下载
WScript.Sleep 10000
'确认完成下载
WScript.Sleep time
WshShell.SendKeys "{ENTER}"
'结束后等一等
WScript.Sleep 1000
'显示时长5s
Print count,5,"count结束"
loop
'END
WScript.Sleep time
WScript.Sleep 1000
'关闭 Excel
oExcel.DisplayAlerts = False
oBook.Close
oExcel.Quit
'FUNC
'类似msgbox
'定时停留弹出框函数
'Print 1,2,3
Sub Print(text,timeout,title)
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.Popup text,timeout,title
Set WshShell = nothing
End Sub