VBS发送鼠标点击?

时间:2016-03-30 13:26:07

标签: vbscript click

我需要从VBS发送鼠标点击。像SendKeys一样。我搜索了整个谷歌,似乎VBS没有这样的功能。你能给我一些解决方案吗?

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

单独使用VBScript是不可能的。您需要使用像nircmd这样的第三方工具。您可以使用其setcursorsetcursorwinmovecursorsendmouse命令来操作鼠标。

例如,以下是如何将光标移动到屏幕坐标(从左上角开始测量)并执行右键单击:

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

有关参数信息,请参阅documentation

答案 2 :(得分:0)

尝试

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

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

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

如果这些都不适合你,我会建议使用像Autoitautohotkey这样的东西,使用AutoHotKey你可以写一个宏来点击,然后从你的VBScript中调用脚本。

答案 3 :(得分:0)

如果要发送密钥,则可以轻松地在记事本中键入该密钥并将其保存为VBS

Set WshShell = WScript.CreateObject("WScript.Shell")
Bracket=")"
WshShell.SendKeys ":"
WshShell.SendKeys "{"&Bracket&"}"
WshShell.SendKeys "{ENTER}"

如果您希望它发送不同的密钥,则基本上在第5行编辑回车键,使其类似于以下示例

Backspace
{BACKSPACE}, {BKSP} or {BS}
Break
{BREAK}
Caps Lock
{CAPSLOCK}
Delete
{DELETE} or {DEL}
Down Arrow
{DOWN}
End
{END}
Enter
{ENTER} or ~
Escape
{ESC}
Help
{HELP}
Home
{HOME}
Insert
{INSERT} or {INS}
Left Arrow
{LEFT}
Num Lock
{NUMLOCK}
Page Down
{PGDN}
Page Up
{PGUP}
Print Screen
{PRTSC}
Right Arrow
{RIGHT}
Scroll Lock
{SCROLLLOCK}
Tab
{TAB}
Up Arrow
{UP}
F1
{F1}
F2
{F2}
F3
{F3}
F4
{F4}
F5
{F5}
F6
{F6}
F7
{F7}
F8
{F8}
F9
{F9}
F10
{F10}
F11
{F11}
F12
{F12}
F13
{F13}
F14
{F14}
F15
{F15}
F16
{F16}
`````````````
I got this information from **Microsoft**

答案 4 :(得分:0)

VBS是脚本,而不是应用程序; VBScript可以调用其他应用程序或 Component Objects 来访问主机环境的元素,就像批处理文件一样;例如。 FileSystemObject来操作文件。

没有为鼠标提供的鼠标,因此要移动鼠标或发送鼠标点击,您需要调用某个应用程序或COM对象来进行操作,或进行一次操作。 可以操纵鼠标的某些应用是MSWord和MSExcel(通过WinAPI调用),NirCmd,AutoIt,AutoHotKey等

这是一个VBApp示例,它调用用户组件的功能:user32.dll

(请注意,参数在发送到DLL之前是如何格式化的。这在VBS或批处理文件中是不可能的,因为它们只能将字符串作为args传递;某些函数需要数据类型,例如Int32,窗口句柄或对象引用)

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


    <StructLayout(LayoutKind.Sequential)>
    Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure

    <DllImport("user32.dll")> _
    Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    Private Shared Function SetForegroundWindow(ByVal hwnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> 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

以下是调用AutoIt来移动鼠标并单击的VBScript:

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

参考文献:
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