Demon ' s Blog
Forget, like a person's feeling
Demon ' s Blog? Programming? control the mouse with VBS (GET mouse coordinates, mouse movement, mouse click, double click, mouse right click)
? Bbpress Integral plug-in--virtual Moneyvbs call WMI to monitor registry changes? Use VBS to control the mouse (get mouse coordinates, mouse movement, mouse click, mouse double click, mouse right click)
Tags: excel.application, VB, VBS, VBScript, Windows API, mouse
Title: Control mouse with VBS (GET mouse coordinates, mouse movement, mouse click, double click, mouse right click)
Author: Demon
Links: http://demon.tw/programming/vbs-control-mouse.html
Copyright: All articles of this blog are subject to the terms "Attribution-NonCommercial use-share 2.5 mainland China" in the same way.
More than one person asked me how to use VBS to control mouse operations, such as: How to use VBS to get the coordinates of the mouse pointer position? How do I move a mouse with VBS? How to use VBS to simulate the left mouse button click, left double click, right click? ......
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):
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 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 ‘Add VBA code to the module
‘Author: Demon
‘Website: http://demon.tw
‘Date: 2011/5/10
x = oExcel.Run("GetXCursorPos") ‘Get the mouse X coordinate
y = oExcel.Run("GetYCursorPos") ‘Get the mouse Y coordinate
WScript.Echo x, y
oExcel.Run "SetCursorPos", 30, 30 ‘Set the mouse X and 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 click of the mouse
oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
‘Simulate a double click with the left mouse button (that is, two quick 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 a right mouse 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!
Related articles:
- VBS calls Windows API functions
- Relative paths in the Workbooks.Open
- VBS analog POST Upload file
- No Vbhide constants in VBS
- Using VBS for Traditional Chinese and Simplified Chinese conversion
Random article:
- Windows 7 Creates a broadband connection shortcut on the desktop
- Batch Technology Insider: if command
- Loss of precision for JavaScript decimal and large integers
- Unicode support for MySQL
- Batch Technology Insider: Echo Command
This article was posted on May 10, 2011, Tuesday at 15:46, classified in programming. You can track comments on this post via RSS 2.0 feed. You can leave a comment or trackback from your site.
12 reviews posted in "Control mouse with VBS (GET mouse coordinates, mouse movement, mouse click, mouse double click, mouse right click)" On
- 1said:
Control the mouse with VBS (GET mouse coordinates, mouse movement, mouse click, double click, mouse right click)