|
Const WM_GETTEXT = &HD
Const BM_CLICK = &HF5
' 定义必要的数据类型和结构
Type POINTAPI
x As Long
y As Long
End Type
Function Main()
Dim hwnd, childHwnd, buttonHwnd, buf, text
' 无限循环
Do
hwnd = FindWindowByText("亲,您已经学了30分钟了,点击确定继续学习..")
If hwnd <> 0 Then
' 查找按钮窗口并点击
childHwnd = FindWindowEx(hwnd, 0, "Button", vbNullString)
While childHwnd <> 0
buf = Space(256)
Call SendMessage(childHwnd, WM_GETTEXT, 256, ByVal buf)
If InStr(buf, "确定") > 0 Then
buttonHwnd = childHwnd
Exit While
End If
childHwnd = FindWindowEx(hwnd, childHwnd, "Button", vbNullString)
Wend
If buttonHwnd <> 0 Then
Call SendMessage(buttonHwnd, BM_CLICK, 0, 0)
End If
End If
' 每隔5秒检查一次对话框
WScript.Sleep 5000
Loop
End Function
Function FindWindowByText(windowText)
Dim hwnd, buf, length
hwnd = FindWindowEx(0, 0, vbNullString, vbNullString)
While hwnd <> 0
buf = Space(256)
length = GetWindowText(hwnd, buf, Len(buf))
If InStr(Left(buf, length), windowText) > 0 Then
FindWindowByText = hwnd
Exit Function
End If
hwnd = FindWindowEx(0, hwnd, vbNullString, vbNullString)
Wend
FindWindowByText = 0
End Function
' 声明 Windows API 函数
Private Declare Function FindWindowEx Lib "user32" _
Alias "FindWindowExA" (ByVal hwndParent As Long, _
ByVal hwndChildAfter As Long, ByVal lpszClass As String, _
ByVal lpszWindow As String) As Long
Private Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _
ByVal cch As Long) As Long
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Any) As Long
' 主函数调用
Main()
=======================
FindWindowByText 函数遍历所有顶层窗口,查找窗口标题中包含指定文本的窗口。
在找到目标窗口后,脚本通过 FindWindowEx 和 SendMessage 使用 WM_GETTEXT 消息来查找包含“确定”按钮的子窗口。
找到按钮后,脚本发送 BM_CLICK 消息来模拟点击按钮。
主循环每隔5秒检查一次目标对话框是否出现,如果出现则自动点击确定按钮。
此脚本需要管理员权限,确保在一个安全且受控的环境中运行此类脚本。将脚本保存为 .vbs 文件并运行。
VBS本身没有直接调用 Windows API 的能力,我们需要通过WMI(Windows Management Instrumentation)和 Win32 API 来实现。
看看有无借鉴作用 |
|