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

不止一個(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ō)明你的水平有待提高!

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

2025國(guó)考·省考課程試聽(tīng)報(bào)名

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