VBS发送鼠标点击? [英] VBS send mouse clicks?

查看:1290
本文介绍了VBS发送鼠标点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

这是一个例程来发送左或右鼠标点击窗口)在VBA for Excel中。类似于AppActivate,你只需要窗口标题。

Here is a routine to send a left or right click to a window (using relative references) in VBA for Excel. Similar to AppActivate, you just need the window title.

调用 SendClick 例程时的参数是:


  • 窗口标题(字符串)

  • 按钮(1 =左,2 =右,-1 = )

  • x(窗口左侧的相对位置)

  • y(相对于窗口顶部的相对位置)

  • Window Title (String)
  • Buttons (1 = Left, 2 = Right, -1 = Move mouse only; no click)
  • x (Relative position to window Left)
  • y (Relative position to window Top)

享受!

'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

这篇关于VBS发送鼠标点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆