【发布时间】:2016-03-30 13:26:06
【问题描述】:
我需要从 VBS 发送鼠标点击。像 SendKeys。我已经搜索了整个谷歌,似乎VBS没有这样的功能。你能给我一些解决方案吗?
【问题讨论】:
我需要从 VBS 发送鼠标点击。像 SendKeys。我已经搜索了整个谷歌,似乎VBS没有这样的功能。你能给我一些解决方案吗?
【问题讨论】:
这是在 VBA for Excel 中向窗口发送左键或右键单击(使用相对引用)的例程。与 AppActivate 类似,您只需要窗口标题即可。
调用 SendClick 例程时的参数是:
享受吧!
'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
【讨论】:
单独使用 VBScript 是不可能的。您需要使用第三方工具,例如nircmd。您可以使用它的setcursor、setcursorwin、movecursor 和sendmouse 命令来操作鼠标。
例如,以下是如何将光标移动到屏幕坐标(从左上角开始测量)并执行右键单击:
With CreateObject("WScript.Shell")
.Run "nircmd setcursor 100 100", 0, True
.Run "nircmd sendmouse right click", 0, True
End With
有关参数信息,请参阅documentation。
【讨论】:
试试
Dim x
set x=createobject("wscript.shell")
x.sendkeys"{CLICK LEFT,50,60}"
或
x.SendKeys("+{F10}") 'for a right click
如果这些都不适合你,我建议使用Autoit 或autohotkey 之类的东西,使用 AutoHotKey 你可以编写一个宏来执行点击,然后从你的 VBScript 调用脚本。
【讨论】:
VBS 是脚本,而不是应用程序; VBScripts 可以调用其他应用程序或组件对象来访问宿主环境的元素,就像批处理文件一样;例如。 FileSystemObject 操作文件。
没有提供鼠标,因此要移动鼠标或发送鼠标点击,您需要调用某个应用程序或 COM 对象来执行此操作,或者制作一个。
一些可以操作鼠标的应用程序是 MSWord 和 MSExcel(通过 WinAPI 调用)、NirCmd、AutoIt、AutoHotKey 等
这是一个调用用户组件函数的VBApp示例:user32.dll:
(注意参数在发送到 DLL 之前是如何格式化的。这在 VBS 或批处理文件中是不可能的,因为它们只能将字符串作为参数传递;某些函数需要数据类型,例如 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
【讨论】: