VBS发送鼠标点击?
答
尝试
Dim x
set x=createobject("wscript.shell")
x.sendkeys"{CLICK LEFT,50,60}"
或
x.SendKeys("+{F10}") 'for a right click
如果没有适合你的工作,我会建议使用像Autoit或autohotkey,使用AutoHotkey的,你可以写一个宏,做的点击,然后的从你的VBScript调用脚本。
答
单独使用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。
答
这是一个在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