不止一個(gè)人問(wèn)過(guò)我怎樣用VBS控制鼠標(biāo)操作,比如:怎么用VBS獲取鼠標(biāo)指針的坐標(biāo)位置?怎么用VBS移動(dòng)鼠標(biāo)
怎么用VBS模擬鼠標(biāo)左鍵單擊、左鍵雙擊、右鍵單擊?……
網(wǎng)上搜到的答案普遍是VBS無(wú)法實(shí)現(xiàn),或者是要用第三方COM(ActiveX?)組件。我對(duì)第三方組件是很反感的,使用第三方組件毫無(wú)可移植性可言,因?yàn)閯e人的系統(tǒng)中不一定注冊(cè)了這個(gè)組件。我的建議是,盡量不要在VBS代碼中調(diào)用第三方組件,除非你的程序只是寫(xiě)來(lái)自己用。(順便說(shuō)一下,也盡量不要用不靠譜的Sendkeys方法,原因不解釋)
好了,廢話就說(shuō)這么多,現(xiàn)在說(shuō)說(shuō)用VBS控制鼠標(biāo)的方法。我以前寫(xiě)過(guò)一篇《VBS調(diào)用Windows API函數(shù)》,本以為既然都能調(diào)用API了,用VBS控制鼠標(biāo)還不是很簡(jiǎn)單是事?事實(shí)證明我錯(cuò)了,不明真相的同學(xué)永遠(yuǎn)是大多數(shù),不知道API是什么的VBSer大有人在。不貼出實(shí)實(shí)在在的代碼,他們根本不會(huì)自己寫(xiě)!
使用此代碼的前提是你的系統(tǒng)上安裝了Excel,因?yàn)橐玫紼xcel.Application對(duì)象(如果你偏要認(rèn)為這算第三方組件我也沒(méi)話說(shuō)):
代碼如下:
Option Explicit
Dim WshShell
Dim oExcel, oBook, oModule
Dim strRegKey, strCode, x, y
Set oExcel = CreateObject("Excel.Application") '創(chuàng)建 Excel 對(duì)象
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") '獲取鼠標(biāo) X 坐標(biāo)
y = oExcel.Run("GetYCursorPos") '獲取鼠標(biāo) Y 坐標(biāo)
WScript.Echo x, y
oExcel.Run "SetCursorPos", 30, 30 '設(shè)置鼠標(biāo) X Y 坐標(biāo)
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
'模擬鼠標(biāo)左鍵單擊
oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
'模擬鼠標(biāo)左鍵雙擊(即快速的兩次單擊)
oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
'模擬鼠標(biāo)右鍵單擊
oExcel.Run "mouse_event", MOUSEEVENTF_RIGHTDOWN + MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
'模擬鼠標(biāo)中鍵單擊
oExcel.Run "mouse_event", MOUSEEVENTF_MIDDLEDOWN + MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0
'關(guān)閉 Excel
oExcel.DisplayAlerts = False
oBook.Close
oExcel.Quit
注釋已經(jīng)夠詳細(xì)了,要知道我很少寫(xiě)注釋的,如果還看不懂,說(shuō)明你的水平有待提高!