VBS怎样判断当前程序窗口是否为指定标题???

2026年09月10日 19:27
有1个网友回答
网友(1):

这个问题看你问了好久了啊,贴的这段代码也前后不搭的。。。安装个免费的小工具AutoIt,然后再运行以下vbs代码吧,参考中有AutoIt的官方下载地址
根据你所要求的,代码运行后会3秒判断一次当前窗口,直到success或者点no不继续等待。本机运行已经测试通过了。

WaitSpecifiedTitleActivated "想要等待的Title"

Function WaitSpecifiedTitleActivated(strTitle)

Set oAutoIt = WScript.CreateObject("AutoItX3.Control")
Do
WScript.Sleep 3000
intFlag = oAutoIt.WinActive(strTitle)
strCurTitle = CStr(oAutoIt.WinGetTitle("[Active]"))
WScript.Echo strCurTitle
If intFlag = 1 Then
MsgBox "Success"
ElseIf (strCurTitle <> "a.txt") And (strCurTitle <> "b.txt") And (strCurTitle <> "c.exe") Then
MsgBox "Not Exist"
btnFlag = MsgBox("Continue wait window with title "&strTitle&"?",vbYesNo,"Friendly Information")
End If

Loop Until (intFlag = 1 Or btnFlag = vbNo)
End Function

补充:请确认下所指定的窗口是Active的,另外就是确定标题是正确咯,比如文本窗口后面a.txt - Notepad需要写完整的,当然也可以使用正则表达式来匹配,这需要稍微修改下代码,可以用标题很简单的窗口测试下看看