VBS获取鼠标或窗口坐标代码示例
下面是一个获取鼠标位置并弹出消息框显示坐标的VBS代码:
Set objShell = CreateObject('WScript.Shell')
While True
x = objShell.AppActivate('计算器') '将计算器窗口设置为活动窗口
If x Then
Set objWshScriptExec = objShell.Exec('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command 'Add-Type -AssemblyName PresentationCore; [System.Windows.Forms.Cursor]::Position''') '执行PowerShell命令获取鼠标位置
strOutput = objWshScriptExec.StdOut.ReadAll()
arrOutput = Split(strOutput, ' ')
xCoord = arrOutput(0)
yCoord = arrOutput(1)
objShell.Popup '鼠标位置:' & xCoord & ', ' & yCoord, 1, '提示', 0 '弹出消息框显示坐标
End If
WScript.Sleep 100
Wend
需要注意的是,这段代码需要在计算机上安装PowerShell才能运行。如果需要获取鼠标位置而不是计算器窗口位置,可以将第4行的'计算器'改为'鼠标'。
原文地址: https://www.cveoy.top/t/topic/mlPs 著作权归作者所有。请勿转载和采集!