VBS send mouse clicks?

后端 未结 5 2042
陌清茗
陌清茗 2020-12-18 12:58

I need send mouse clicks from VBS. Like SendKeys. I have searched whole google, it seems there is no such function for VBS. Can you give me some solution?

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-18 13:47

    VBS is a script, not an application; VBScripts can call other applications or Component Objects to access elements of the host environment, just like batch files; eg. FileSystemObject to manipulate files.

    There isn't one provided for mouse, so to move mouse or send mouse clicks, you'd need to call some app or COM object to do it, or make one.
    Some apps that can manipulate the mouse are MSWord & MSExcel (via WinAPI calls), NirCmd, AutoIt, AutoHotKey, etc

    Here's a VBApp example that calls functions of the User Component: user32.dll:

    (Notice how the arguments are formatted before being sent to the DLL. This is not possible in VBS or batch files since they can only pass Strings as args; some functions expect data types eg. Int32, window handles or object references)

    Option Strict On
    Option Explicit On
    Option Infer On
    
    Imports System.Runtime.InteropServices
    
    Public Class Mousing
        Private Declare Auto Sub mouse_event Lib "user32" (ByVal dwFlags As Int32, ByVal dx As Int32, ByVal dy As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As IntPtr)
        Private Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2
        Private Const MOUSEEVENTF_LEFTUP As Int32 = &H4
        Private Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
        Private Const MOUSEEVENTF_RIGHTUP As Long = &H10
    
    
        
        Private Structure RECT
            Public Left As Integer
            Public Top As Integer
            Public Right As Integer
            Public Bottom As Integer
        End Structure
    
         _
        Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
        End Function
    
         _
        Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function
    
         _
        Private Shared Function SetForegroundWindow(ByVal hwnd As IntPtr) As  Boolean
        End Function
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            ' find the window 
            Dim hWnd As IntPtr = FindWindow(Nothing, "Some Window")
            ' check if window found
            If hWnd.Equals(IntPtr.Zero) Then
                MessageBox.Show("Window Not Found!", "Aborting", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Return ' exit
            Else
                ' bring the window to the foreground
                SetForegroundWindow(hWnd)
    
                ' get the windows size and location
                Dim r As New RECT
                GetWindowRect(hWnd, r)
    
                'Move the cursor to the windows location plus our offset (x + 50 , y + 100)
                Windows.Forms.Cursor.Position = New System.Drawing.Point(r.Left + 50, r.Top + 100)
            ' To move relative to screen, just enter coordinates above without offsetting
    
               ' click the left mouse button at the current mouse position
                mouse_event(MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, IntPtr.Zero)
            End If
        End Sub
    
    End Class
    

    The following is a VBScript calling AutoIt to move mouse & click:

    Set oAutoIt = WScript.CreateObject("AutoItX.Control")
    set oShell = CreateObject("WScript.Shell")
    oAutoIt.MouseMove x,y,0
    WScript.Sleep 500
    oAutoIt.MouseClick($MOUSE_CLICK_PRIMARY)
    

    References:
    http://www.vbforums.com/showthread.php?672196-RESOLVED-SetCursorPos
    http://www.ericphelps.com/batch/rundll/
    https://www.dostips.com/forum/viewtopic.php?t=3931
    https://support.microsoft.com/en-au/help/152969/visual-basic-procedure-to-get-set-cursor-position
    https://microsoft.public.scripting.vbscript.narkive.com/ZO09Cxnz/moving-mouse-pointer-with-vbs-file

提交回复
热议问题