How to use the VBS to simulate the left mouse button click, left-click, right click? ......
The answer that is commonly found on the Internet is that VBS cannot be implemented, or is it to use a third party COM (ActiveX?) Component I'm disgusted with third-party components, and there's no portability to using third-party components, because this component is not necessarily registered in someone else's system. My advice is to try not to call a Third-party component in the VBS code unless your program is written for your own use. (By the way, also try not to use the unreliable SendKeys method, reason does not explain)
OK, so much for the nonsense, now talk about the method of controlling the mouse with the VBS. I have written a "VBS call Windows API function", I think since all can invoke the API, with a VBS control mouse is not very simple thing? It turns out that I was wrong, the students of unknown truth is always the majority, do not know what the API is Vbser. Do not post real code, they do not write their own!
The premise of using this code is that you have Excel installed on your system because you want to use the Excel.Application object (if you want to think of it as a third-party component I have nothing to say):
Copy Code code as follows:
Option Explicit
Dim WshShell
Dim oexcel, obook, omodule
Dim strRegKey, Strcode, X, y
Set oexcel = CreateObject ("Excel.Application") ' Create an 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 ' Add VBA code to the module
' Author:demon
' website:http://demon.tw
' DATE:2011/5/10
x = Oexcel.run ("Getxcursorpos") ' Get mouse X coordinates
y = Oexcel.run ("Getycursorpos") ' Get mouse Y coordinate
WScript.Echo X, Y
Oexcel.run "Setcursorpos", 30, 30 ' Set mouse X Y coordinates
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 the 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
' Simulate the right mouse button click
Oexcel.run "Mouse_event", Mouseeventf_rightdown + mouseeventf_rightup, 0, 0, 0, 0
' Simulate the middle mouse button click
Oexcel.run "Mouse_event", Mouseeventf_middledown + mouseeventf_middleup, 0, 0, 0, 0
' Close Excel
Oexcel.displayalerts = False
Obook.close
oExcel.Quit
Note has been enough detail, to know that I rarely write notes, if you do not understand, you need to improve the level!
Original: http://demon.tw/programming/vbs-control-mouse.html