IfWinExist / IfWinNotExist


检查指定的窗口是否存在。

IfWinExist [, WinTitle, WinText, ExcludeTitle, ExcludeText]
IfWinNotExist [, WinTitle, WinText, ExcludeTitle, ExcludeText]
UniqueID := WinExist("WinTitle", "WinText", "ExcludeTitle", "ExcludeText")

参数

WinTitle 目标窗口的标题或部分标题(匹配模式由 SetTitleMatchMode 来决定)。如果省略此参数和其他 3 个参数,Last Found Window(最后找到的窗口) 将被使用(即检查指定窗口看它是否还存在)。要使用一个窗口类,指定 ahk_class 确切的类名 (通过 Window Spy 显示)。要使用一个 process identifier (PID)(进程标识符),指定 ahk_pid %包含PID的变量% 。要使用一个 window group(窗口组),指定 ahk_group GroupName 。要使用一个窗口的 unique ID number(唯一标识符编号),指定 ahk_id %包含ID的变量% 。通过指定 multiple criteria(多个条件)缩小搜索范围。例如:My File.txt ahk_class Notepad
WinText 如果用到,此参数必须是目标窗口的一个单独文本对象的子字串(像内置的 Window Spy 工具显示的一样)。如果 DetectHiddenText 是 ON 的状态,隐藏的文本对象将被探测。
ExcludeTitle 标题含有此参数值的窗口将不被考虑。注意:由于反向兼容 .aut 脚本 ,如果此参数刚好符合一个命令的名称,它会被解释为一个命令。要绕弯解决这种情况,使用 WinExist() 函数 来代替此命令。
ExcludeText 文本含有此参数值的窗口将不被考虑。

注意

如果省略所有参数,Last Found Window 将被检查看它是否还存在(或者在使用 IfWinNotExist 时看它是否不存在)。

如果这两个命令中任何一个确定有一个合格的窗口存在,Last Found Window 将更新为那个窗口。换句话说,如果 IfWinExist 赋值为“真”或者 IfWinNotExist 赋值为“假”,最后找到的窗口将被更新。

WinExist() 函数返回首个匹配窗口的 Unique ID (HWND) (如果无匹配将为 0 )。由于所有非零数字被视为“真”,在 WinTitle 存在时 if WinExist("WinTitle") 语句为真。

要探索一个控件的 HWND (为了和 Post/SendMessage 或 DllCall 一起使用),请用 ControlGet Hwnd 或者 MouseGetPos 。

SetWinDelay 不适用于 IfWinExist/IfWinActive 。

窗口标题和文本是区分大小写的。隐藏的窗口将不被探测,除非 DetectHiddenWindows 已被打开。

相关命令

IfWinActive, SetTitleMatchMode, DetectHiddenWindows, Last Found Window, Process, WinActivate, WinWaitActive, WinWait, WinWaitClose, #IfWinActive/Exist

示例

IfWinExist, 无标题 - 记事本
{
    WinActivate  ; 自动地使用上面找到的窗口。
    WinMaximize  ; 同上
    Send, Some text.{Enter}
    return
}

IfWinNotExist, 计算器
    return
else
{
    WinActivate  ; 上面的 "IfWinNotExist" 也为我们设置了 "last found" window(最后找到的窗口)。
    WinMove, 40, 40  ; 将它移动到一个新位置。
    return
}

if WinExist("ahk_class Notepad") or WinExist("ahk_class" . ClassName)
    WinActivate  ; 使用 last found window

MsgBox % "激活窗口的 ID 是 " . WinExist("A")
翻译:天堂之门 menk33@163.com 2008年8月12日