您好,登錄后才能下訂單哦!
小編給大家分享一下怎么用VBS控制鼠標,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
怎么用VBS模擬鼠標左鍵單擊、左鍵雙擊、右鍵單擊?……
網(wǎng)上搜到的答案普遍是VBS無法實現(xiàn),或者是要用第三方COM(ActiveX?)組件。我對第三方組件是很反感的,使用第三方組件毫無可移植性可言,因為別人的系統(tǒng)中不一定注冊了這個組件。我的建議是,盡量不要在VBS代碼中調(diào)用第三方組件,除非你的程序只是寫來自己用。(順便說一下,也盡量不要用不靠譜的Sendkeys方法,原因不解釋)
好了,廢話就說這么多,現(xiàn)在說說用VBS控制鼠標的方法。我以前寫過一篇《VBS調(diào)用Windows API函數(shù)》,本以為既然都能調(diào)用API了,用VBS控制鼠標還不是很簡單是事?事實證明我錯了,不明真相的同學(xué)永遠是大多數(shù),不知道API是什么的VBSer大有人在。不貼出實實在在的代碼,他們根本不會自己寫!
使用此代碼的前提是你的系統(tǒng)上安裝了Excel,因為要用到Excel.Application對象(如果你偏要認為這算第三方組件我也沒話說):
代碼如下:
Option Explicit
Dim WshShell
Dim oExcel, oBook, oModule
Dim strRegKey, strCode, x, y
Set oExcel = CreateObject("Excel.Application") '創(chuàng)建 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 '設(shè)置鼠標 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
'關(guān)閉 Excel
oExcel.DisplayAlerts = False
oBook.Close
oExcel.Quit
以上是“怎么用VBS控制鼠標”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。