[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转贴] VBS脚本获取鼠标坐标、移动、单击、双击等控制动作

不止一个人问过我怎样用VBS控制鼠标操作,比如:怎么用VBS获取鼠标指针的坐标位置?怎么用VBS移动鼠标
怎么用VBS模拟鼠标左键单击、左键双击、右键单击?……

网上搜到的答案普遍是VBS无法实现,或者是要用第三方 COM(ActiveX?)组件。我对第三方组件是很反感的,使用第三方组件毫无可移植性可言,因为别人的系统中不一定注册了这个组件。我的建议是,尽量不要在VBS代码中调用第三方组件,除非你的程序只是写来自己用。(顺便说一下,也尽量不要用不靠谱的Sendkeys方法,原因不解释)


好了,废话就说这么多,现在说说用VBS控制鼠标的方法。我以前写过一篇《VBS调用Windows API函数》,本以为既然都能调用API了,用VBS控制鼠标还不是很简单是事?事实证明我错了,不明真相的同学永远是大多数,不知道API是什么的 VBSer大有人在。不贴出实实在在的代码,他们根本不会自己写!

使用此代码的前提是你的系统上安装了Excel,因为要用到Excel.Application对象(如果你偏要认为这算第三方组件我也没话说):

  1. Option Explicit
  2. Dim WshShell
  3. Dim oExcel, oBook, oModule
  4. Dim strRegKey, strCode, x, y
  5. Set oExcel = CreateObject("Excel.Application") '创建 Excel 对象
  6. set WshShell = CreateObject("wscript.Shell")
  7. strRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\$\Excel\Security\AccessVBOM"
  8. strRegKey = Replace(strRegKey, "$", oExcel.Version)
  9. WshShell.RegWrite strRegKey, 1, "REG_DWORD"
  10. Set oBook = oExcel.Workbooks.Add '添加工作簿
  11. Set oModule = obook.VBProject.VBComponents.Add(1) '添加模块
  12. strCode = _
  13. "'Author: Demon" & vbCrLf & _
  14. "'Website: http://demon.tw" & vbCrLf & _
  15. "'Date: 2011/5/10" & vbCrLf & _
  16. "Private Type POINTAPI : X As Long : Y As Long : End Type" & vbCrLf & _
  17. "Private Declare Function SetCursorPos Lib ""user32"" (ByVal x As Long, ByVal y As Long) As Long" & vbCrLf & _
  18. "Private Declare Function GetCursorPos Lib ""user32"" (lpPoint As POINTAPI) As Long" & vbCrLf & _
  19. "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 & _
  20. "Public Function GetXCursorPos() As Long" & vbCrLf & _
  21. "Dim pt As POINTAPI : GetCursorPos pt : GetXCursorPos = pt.X" & vbCrLf & _
  22. "End Function" & vbCrLf & _
  23. "Public Function GetYCursorPos() As Long" & vbCrLf & _
  24. "Dim pt As POINTAPI: GetCursorPos pt : GetYCursorPos = pt.Y" & vbCrLf & _
  25. "End Function"
  26. oModule.CodeModule.AddFromString strCode '在模块中添加 VBA 代码
  27. 'Author: Demon
  28. 'Website: http://demon.tw
  29. 'Date: 2011/5/10
  30. x = oExcel.Run("GetXCursorPos") '获取鼠标 X 坐标
  31. y = oExcel.Run("GetYCursorPos") '获取鼠标 Y 坐标
  32. WScript.Echo x, y
  33. oExcel.Run "SetCursorPos", 30, 30 '设置鼠标 X Y 坐标
  34. Const MOUSEEVENTF_MOVE = &H1
  35. Const MOUSEEVENTF_LEFTDOWN = &H2
  36. Const MOUSEEVENTF_LEFTUP = &H4
  37. Const MOUSEEVENTF_RIGHTDOWN = &H8
  38. Const MOUSEEVENTF_RIGHTUP = &H10
  39. Const MOUSEEVENTF_MIDDLEDOWN = &H20
  40. Const MOUSEEVENTF_MIDDLEUP = &H40
  41. Const MOUSEEVENTF_ABSOLUTE = &H8000
  42. '模拟鼠标左键单击
  43. oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
  44. '模拟鼠标左键双击(即快速的两次单击)
  45. oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
  46. oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
  47. '模拟鼠标右键单击
  48. oExcel.Run "mouse_event", MOUSEEVENTF_RIGHTDOWN + MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
  49. '模拟鼠标中键单击
  50. oExcel.Run "mouse_event", MOUSEEVENTF_MIDDLEDOWN + MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0
  51. '关闭 Excel
  52. oExcel.DisplayAlerts = False
  53. oBook.Close
  54. oExcel.Quit
复制代码


注释已经够详细了,要知道我很少写注释的,如果还看不懂,说明你的水平有待提高!
原文:http://demon.tw/programming/vbs-control-mouse.html

请问LZ!能不能实现 鼠标拖动的啊?

TOP

好强啊
楼主天才

TOP

Excel.Application的確是ActiveX
带内地用语或带里语的文章,在下读写总觉有点吃力;
如误解了各位意思的讲勿见怪   ^_^

TOP

实在……太厉害了!膜拜大神

TOP

返回列表