用VBS控制鼠標(biāo)的實現(xiàn)代碼(獲取鼠標(biāo)坐標(biāo)、鼠標(biāo)移動、鼠標(biāo)單擊、鼠標(biāo)雙擊、鼠標(biāo)右擊)
來源:易賢網(wǎng) 閱讀:4826 次 日期:2016-07-05 14:45:03
溫馨提示:易賢網(wǎng)小編為您整理了“用VBS控制鼠標(biāo)的實現(xiàn)代碼(獲取鼠標(biāo)坐標(biāo)、鼠標(biāo)移動、鼠標(biāo)單擊、鼠標(biāo)雙擊、鼠標(biāo)右擊)”,方便廣大網(wǎng)友查閱!

不止一個人問過我怎樣用VBS控制鼠標(biāo)操作,比如:怎么用VBS獲取鼠標(biāo)指針的坐標(biāo)位置?怎么用VBS移動鼠標(biāo)

怎么用VBS模擬鼠標(biāo)左鍵單擊、左鍵雙擊、右鍵單擊?……

網(wǎng)上搜到的答案普遍是VBS無法實現(xiàn),或者是要用第三方COM(ActiveX?)組件。我對第三方組件是很反感的,使用第三方組件毫無可移植性可言,因為別人的系統(tǒng)中不一定注冊了這個組件。我的建議是,盡量不要在VBS代碼中調(diào)用第三方組件,除非你的程序只是寫來自己用。(順便說一下,也盡量不要用不靠譜的Sendkeys方法,原因不解釋)

好了,廢話就說這么多,現(xiàn)在說說用VBS控制鼠標(biāo)的方法。我以前寫過一篇《VBS調(diào)用Windows API函數(shù)》,本以為既然都能調(diào)用API了,用VBS控制鼠標(biāo)還不是很簡單是事?事實證明我錯了,不明真相的同學(xué)永遠(yuǎn)是大多數(shù),不知道API是什么的VBSer大有人在。不貼出實實在在的代碼,他們根本不會自己寫!

使用此代碼的前提是你的系統(tǒng)上安裝了Excel,因為要用到Excel.Application對象(如果你偏要認(rèn)為這算第三方組件我也沒話說):

代碼如下:

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") '獲取鼠標(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ì)了,要知道我很少寫注釋的,如果還看不懂,說明你的水平有待提高!

更多信息請查看腳本欄目
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機(jī)號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)