如何检查 Autohotkey 中是否存在控制字段?

How do I check if a control field exists in Autohotkey?

我正在编写与基于 Java 的 Web 应用程序交互的 AutoHotkey 脚本。有一次,我希望我的脚本暂停并等到它找到控制权 SunAwtCanvas1,然后再继续 运行。

我知道您可以使用 IfWinExist 来检查 window 是否存在,但我不知道按钮或控制字段是否存在类似的命令。

如何检查 AutoHotkey 中是否存在控制字段?

选项 1:WinExist 标题,control_name

使用 AU3_Spy.exe 检查时,您可能会在文本中找到控件名称。那样的话,就

IfWinExistwintitle, SunAwtCanvas1

会做。

尽管我更喜欢函数式语法,尤其是因为它可以使用埃及方括号:

If (WinExist(wintitle, "SunAwtCanvas1")) {
    // …
}

选项 2:ControlGet

如果控件名称不在文本中,或者虽然在文本中但可能不可见,而您想知道它是否可见,ControlGet 可以读取控件的一些属性。

如果控件不存在,ErrorLevel设置为1,OutputVar为空。此外,它会在 Try {} 块内抛出异常。

此外,某些控件可能 pre-created 但不可见。因此,要检查控件是否显示:

ControlGet ctrlVisible, Visible,, SunAwtCanvas1
If (ctrlVisible) { // like IfControlVisible
    // …
}

据我所知,没有 WinWait-like 控件运算符,但简单的循环就可以:

// add IfWinExist or WinWait somewhere before to fill in LastWindowFound

Loop
{
    ControlGet ctrlVisible, Visible,, SunAwtCanvas1 
    Sleep 100 ; to avoid high CPU load, and to allow app finish its operations in case control is found in middle of something (that's why it's after ControlGet, not before)
} Until ctrlVisible

在此示例中,ControlGet 将等待控件出现在 LastWindowFound 中。或者您可以在 ControlGet 中直接指定 wintitle/wintext/excludetitle/excludetext 作为参数。