3
votes

In my case, there are times that a certain button will exist and not exist.

Is there a way to check whether a certain button exists on a window using AutoHotkey?

2
Can you provide a specific example? What type of buttons would you be looking for? In what type of application?Stevoisiak

2 Answers

2
votes

If you use ControlGet with a command and the control does not exist then ErrorLevel will be set to 1.

You could use ControlGet to get the window handle (HWND) of a control. If the control exists the window handle will be put in your output var and ErrorLevel will be set to 0, otherwise the output var will be blank and ErrorLevel will be 1.

In the example below the first two lines get the window handle for the 'Ok' button on Notepad's About screen (the about screen must of course be shown for it to work) and show the results in a MsgBox. The ClassNN of the Ok button is Button1.

The second two lines to the same but for a control with a ClassNN of Button2 that doesn't exist.

ControlGet, Handle, Hwnd,, Button1, About Notepad ahk_class #32770
MsgBox Handle: %Handle%`n`nError: %ErrorLevel%

ControlGet, Handle, Hwnd,, Button2, About Notepad ahk_class #32770
MsgBox Handle: %Handle%`n`nError: %ErrorLevel%
0
votes

Here is the code example from notepadplusplus_toogle_find_window.ahk using ControlGet:

; Button1 is the class name for the title bar and close button of the results pane when docked
ControlGet, OutputVar, Visible,, Button1, Notepad++
if ErrorLevel = 0
{
    If OutputVar > 0
    {
        ; Found it docked
        Open := 1
        ; Get the size and coordinates of the title bar and button
        ControlGetPos, X, Y, Width, Height, Button1
        ; Set the coordinates of the close button
        X := Width - 9
        Y := 5
        ; Send a click
        ControlClick, Button1,,,,, NA x%X% y%Y%
    }
}