2016-03-30 293 views
2

我需要從VBS發送鼠標點擊。像SendKeys一樣。我搜索了整個谷歌,似乎VBS沒有這樣的功能。你能給我一些解決方案嗎?VBS發送鼠標點擊?

回答

0

嘗試

Dim x 
set x=createobject("wscript.shell") 

x.sendkeys"{CLICK LEFT,50,60}" 

x.SendKeys("+{F10}") 'for a right click 

如果沒有適合你的工作,我會建議使用像Autoitautohotkey,使用AutoHotkey的,你可以寫一個宏,做的點擊,然後的從你的VBScript調用腳本。

0

單獨使用VBScript是不可能的。您需要使用第三方工具,如nircmd。您可以使用它的setcursor,setcursorwin,movecursorsendmouse命令來操縱鼠標。

例如,這裏是如何將光標移動到屏幕座標(自左上角測量)並執行右鍵單擊:

With CreateObject("WScript.Shell") 
    .Run "nircmd setcursor 100 100", 0, True 
    .Run "nircmd sendmouse right click", 0, True 
End With 

的參數信息,請參閱documentation

3

這是一個在VBA for Excel中向窗口(使用相對引用)發送左鍵或右鍵單擊的例程。與AppActivate類似,您只需要窗口標題。

的參數,當調用SendClick程序是:

  • 窗口標題(字符串)
  • 按鈕(1 =左,2 =右,-1 =移動鼠標只;沒有點擊)
  • X(相對位置窗口左)
  • Y(相對位置窗口頂部)

享受!

'Declare mouse events 
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long 
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long) 
Public Const MOUSEEVENTF_LEFTDOWN = &H2 
Public Const MOUSEEVENTF_LEFTUP = &H4 
Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8 
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10 
'Declare sleep 
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 

' Window location 
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 
Public Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long 
Type RECT 
    Left As Long 
    Top As Long 
    Right As Long 
    Bottom As Long 
End Type 

Public Function WindowHandle(ByVal sTitle As String) As Long 
    WindowHandle = FindWindow(vbNullString, sTitle) 
End Function 

Public Sub SendClick(sWnd As String, b As Integer, x As Long, y As Long) 
    Dim pWnd As Long, pRec As RECT 

    pWnd = WindowHandle(sWnd) 
    GetWindowRect pWnd, pRec 

    SetCursorPos pRec.Left + x, pRec.Top + y 
    Sleep 50 
    If b = 2 Then 
     mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0 
     Sleep 50 
     mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 
    ElseIf b <> -1 Then 
     mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 
     Sleep 50 
     mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 
    End If 
End Sub