The answer commonly found on the web is that VBS cannot be implemented, or that a third-party COM (ActiveX? Components I am disgusted with third-party components, and using third-party components is not portable, because this component is not necessarily registered in someone else's system. My advice is to try not to call third-party components in the VBS code unless your program just writes for your own use. (By the way, also try not to use the SendKeys method, the reason does not explain)
Okay, so much nonsense, now talk about how to control the mouse with VBS. I used to write a "VBS call Windows API function", I thought since all can invoke API, with VBS control Mouse is not very simple? It turns out that I was wrong, the students who do not know the truth are always the majority, not knowing what the API is Vbser. Do not post real code, they do not write their own!
The premise of using this code is that Excel is installed on your system because you want to use the Excel.Application object (if you want to think of this as a third-party component, I have nothing to say):
Copy CodeThe code is as follows:
Option Explicit
Dim WshShell
Dim oexcel, obook, omodule
Dim strRegKey, Strcode, X, y
Set oexcel = CreateObject ("Excel.Application") ' Create Excel Object
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 ' Add workbook
Set omodule = obook. VBPROJECT.VBCOMPONENTS.ADD (1) ' Add module
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 A s 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 ' adding VBA code in the module
' Author:demon
' website:http://demon.tw
' DATE:2011/5/10
x = Oexcel.run ("Getxcursorpos") ' Get mouse x coordinate
y = Oexcel.run ("Getycursorpos") ' Get mouse Y coordinate
WScript.Echo X, Y
Oexcel.run "Setcursorpos", 30, 30 ' Set mouse X Y coordinate
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
' Simulate left mouse button click
Oexcel.run "Mouse_event", Mouseeventf_leftdown + mouseeventf_leftup, 0, 0, 0, 0
' Simulate the left mouse button double-click (ie quick two clicks)
Oexcel.run "Mouse_event", Mouseeventf_leftdown + mouseeventf_leftup, 0, 0, 0, 0
Oexcel.run "Mouse_event", Mouseeventf_leftdown + mouseeventf_leftup, 0, 0, 0, 0
' Analog Mouse right-click
Oexcel.run "Mouse_event", Mouseeventf_rightdown + mouseeventf_rightup, 0, 0, 0, 0
' Simulate middle mouse click
Oexcel.run "Mouse_event", Mouseeventf_middledown + mouseeventf_middleup, 0, 0, 0, 0
' Close Excel
Oexcel.displayalerts = False
Obook.close
oExcel.Quit
Note has enough detail, to know that I rarely write comments, if still do not understand, indicating that your level needs to be improved!
Original: http://demon.tw/programming/vbs-control-mouse.html