vbs鼠标方法——模拟鼠标按键

简介: '*********************************************************************** ' 代码开始 '*********************************************************************** Set mouse=New SetMouse mouse.

'***********************************************************************


' 代码开始

'***********************************************************************

Set mouse=New SetMouse

mouse.getpos x,y ''获得鼠标当前位置坐标

MsgBox x & " " & y


mouse.move 60,14 '把鼠标移动到坐标
WScript.Sleep 200
mouse.clik "dbclick" ’左击

' "right" 右击, "middle" 中间键点击

 

'*****************将以下代码加入到vbs文件末就能如以上方法调用*******************************************************************************************

 

'****************************************************************************************************************************************************

Class SetMouse
private S
private xls, wbk, module1
private reg_key, xls_code, x, y


Private Sub Class_Initialize()
Set xls = CreateObject("Excel.Application")
Set S = CreateObject("wscript.Shell")
'vbs 完全控制excel
reg_key = "HKEY_CURRENT_USER\Software\Microsoft\Office\$\Excel\Security\AccessVBOM"
reg_key = Replace(reg_key, "$", xls.Version)
S.RegWrite reg_key, 1, "REG_DWORD"
'model 代码
xls_code = _
"Private Type POINTAPI : X As Long : Y As Long : End Type" & vbCrLf & _
"Private Declare Function SetCursorPos Lib ""user32"" (ByVal x As Long, ByVal y As Long) As Long" & vbCrLf & _
"Private Declare Function GetCursorPos Lib ""user32"" (lpPoint As POINTAPI) As Long" & vbCrLf & _
"Private Declare Sub mouse_event Lib ""user32"" Alias ""mouse_event"" " _
& "(ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)" & vbCrLf & _
"Public Function getx() As Long" & vbCrLf & _
"Dim pt As POINTAPI : GetCursorPos pt : getx = pt.X" & vbCrLf & _
"End Function" & vbCrLf & _
"Public Function gety() As Long" & vbCrLf & _
"Dim pt As POINTAPI: GetCursorPos pt : gety = pt.Y" & vbCrLf & _
"End Function"
Set wbk = xls.Workbooks.Add
Set module1 = wbk.VBProject.VBComponents.Add(1)
module1.CodeModule.AddFromString xls_code
End Sub


'关闭
Private Sub Class_Terminate
xls.DisplayAlerts = False
wbk.Close
xls.Quit
End Sub

'可调用过程


Public Sub getpos( x, y)
x = xls.Run("getx")
y = xls.Run("gety")
End Sub


Public Sub move(x,y)
xls.Run "SetCursorPos", x, y
End Sub

 

Public Sub clik(keydown)
Select Case UCase(keydown)
Case "LEFT"
xls.Run "mouse_event", &H2 + &H4, 0, 0, 0, 0
Case "RIGHT"
xls.Run "mouse_event", &H8 + &H10, 0, 0, 0, 0
Case "MIDDLE"
xls.Run "mouse_event", &H20 + &H40, 0, 0, 0, 0
Case "DBCLICK"
xls.Run "mouse_event", &H2 + &H4, 0, 0, 0, 0
xls.Run "mouse_event", &H2 + &H4, 0, 0, 0, 0
End Select
End Sub


End Class

 


'***********************************************************************


'代码结束

'***********************************************************************



'****************************************************************************************************************************************************

技术改变世界! --狂诗绝剑
目录
相关文章
尝试用easyx窗口实现鼠标的操作
尝试用easyx窗口实现鼠标的操作
179 0
按下鼠标右键的时候如何截图
点击右键,出现要操作的菜单项 鼠标移动到,想要操作的菜单项上 这时候按下鼠标左键 最后再按下crtl+a,有的电脑是crtl+alt+a,就截图成功了,这时候是所有屏幕的截图 打开word文件,或者打开画图工具,ctrl+v,就把整张截图粘贴上面了 可以跟进自己需求,再次截图,选择自己想要的部分
158 0
按下鼠标右键的时候如何截图
|
API
用VBS控制鼠标的实现代码(获取鼠标坐标、鼠标移动、鼠标单击、鼠标双击)
网上搜到的答案普遍是VBS无法实现,或者是要用第三方COM(ActiveX?)组件。我对第三方组件是很反感的,使用第三方组件毫无可移植性可言,因为别人的系统中不一定注册了这个组件。我的建议是,尽量不要在VBS代码中调用第三方组件,除非你的程序只是写来自己用。
2282 0
|
Windows 安全 搜索推荐