ControlGet

Retrieves various types of information about a control.

ControlGet, OutputVar, SubCommand , Value, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

Parameters

OutputVar
The name of the output variable in which to store the result of SubCommand.
SubCommand, Value
These are dependent upon each other and their usage is described below.
Control

If blank or omitted, the target window's topmost control will be used. Otherwise, specify either ClassNN (the classname and instance number of the control) or the control's text, both of which can be determined via Window Spy. When using text, the matching behavior is determined by SetTitleMatchMode.

To operate upon a control's HWND (window handle), leave the Control parameter blank and specify ahk_id %ControlHwnd% for the WinTitle parameter (this also works on hidden controls even when DetectHiddenWindows is Off). The HWND of a control is typically retrieved via ControlGet Hwnd, MouseGetPos, or DllCall().

WinTitle, WinText, ExcludeTitle, ExcludeText

If each of these is blank or omitted, the Last Found Window will be used. Otherwise, specify for WinTitle a window title or other criteria to identify the target window and/or for WinText a substring from a single text element of the target window (as revealed by the included Window Spy utility).

ExcludeTitle and ExcludeText can be used to exclude one or more windows by their title or text. Their specification is similar to WinTitle and WinText, except that ExcludeTitle does not recognize any criteria other than the window title.

Window titles and text are case-sensitive. By default, hidden windows are not detected and hidden text elements are detected, unless changed with DetectHiddenWindows and DetectHiddenText. By default, a window title must start with the specified WinTitle or ExcludeTitle to be a match, unless changed with SetTitleMatchMode.

Sub-commands

For SubCommand, specify one of the following:

List

Retrieves a list of items from a ListView, ListBox, ComboBox, or DropDownList. For ListView, additional options can be specified.

ControlGet, OutputVar, List , Options, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

ListView

If the Options parameter is blank or omitted, all the text in the control is retrieved. Each row except the last will end with a linefeed character (`n). Within each row, each field (column) except the last will end with a tab character (`t).

Specify for Options zero or more of the following words, each separated from the next with a space or tab:

Option Description
Selected Retrieves only the selected (highlighted) rows rather than all rows. If none, OutputVar is made blank.
Focused Retrieves only the focused row. If none, OutputVar is made blank.
ColN Retrieves only the Nth column (field) rather than all columns. Replace N with a number of your choice. For example, Col4 retrieves the fourth column.
Count Retrieves a single number that is the total number of rows in the control.
Count Selected Retrieves the number of selected (highlighted) rows.
Count Focused Retrieves the row number (position) of the focused row (0 if none).
Count Col Retrieves the number of columns in the control (or -1 if the count cannot be determined).

Note: Some applications store their ListView text privately, which prevents their text from being retrieved. In these cases, ErrorLevel will usually be set to 0 (indicating success) but all the retrieved fields will be empty. Also note that ListView text retrieval is not restricted by #MaxMem.

Upon success, ErrorLevel is set to 0. Upon failure, it is set to 1 and OutputVar is made blank. Failure occurs when: 1) the target window or control does not exist; 2) the target control is not of type SysListView32; 3) the process owning the ListView could not be opened, perhaps due to a lack of user permissions or because it is locked; 4) the ColN option specifies a nonexistent column.

To extract the individual rows and fields out of a ListView, use a parsing loop as in this example:

ControlGet, SelectedItems, List, Selected, SysListView321, WinTitle
Loop, Parse, SelectedItems, `n  ; Rows are delimited by linefeeds (`n).
{
    RowNumber := A_Index
    Loop, Parse, A_LoopField, %A_Tab%  ; Fields (columns) in each row are delimited by tabs (A_Tab).
        MsgBox Row #%RowNumber% Col #%A_Index% is %A_LoopField%.
}

On a related note, the columns in a ListView can be resized via SendMessage as shown in this example:

SendMessage, 0x101E, 0, 80, SysListView321, WinTitle  ; 0x101E is LVM_SETCOLUMNWIDTH.

In the above, 0 indicates the first column (specify 1 for the second, 2 for the third, etc.) Also, 80 is the new width. Replace 80 with -1 to autosize the column. Replace it with -2 to do the same but also take into account the header text width.

ListBox, ComboBox, DropDownList

All the text is retrieved from the control (that is, the ListView options above such as Count and Selected are not supported).

Each item except the last will be terminated by a linefeed character (`n). To access the items individually, use a parsing loop as in this example:

ControlGet, Items, List,, ComboBox1, WinTitle
Loop, Parse, Items, `n
    MsgBox Item number %A_Index% is %A_LoopField%.

Checked

Retrieves 1 if the checkbox or radio button is checked or 0 if not.

ControlGet, OutputVar, Checked ,, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

Enabled

Retrieves 1 if Control is enabled, or 0 if disabled.

ControlGet, OutputVar, Enabled ,, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

Visible

Retrieves 1 if Control is visible, or 0 if hidden.

ControlGet, OutputVar, Visible ,, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

Tab

Retrieves the tab number of a SysTabControl32 control.

ControlGet, OutputVar, Tab ,, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

The first tab is 1, the second is 2, etc. To instead discover how many tabs (pages) exist in a tab control, follow this example:

SendMessage, 0x1304,,, SysTabControl321, WinTitle  ; 0x1304 is TCM_GETITEMCOUNT.
TabCount := ErrorLevel

FindString

Retrieves the entry number of a ListBox or ComboBox that is an exact match for String.

ControlGet, OutputVar, FindString, String , Control, WinTitle, WinText, ExcludeTitle, ExcludeText

The first entry in the control is 1, the second 2, and so on. If no match is found, OutputVar is made blank and ErrorLevel is set to 1.

Choice

Retrieves the name of the currently selected entry in a ListBox or ComboBox.

ControlGet, OutputVar, Choice ,, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

To instead retrieve the position of the selected item, follow this example (use only one of the first two lines):

SendMessage, 0x0188, 0, 0, ListBox1, WinTitle  ; 0x0188 is LB_GETCURSEL (for a ListBox).
SendMessage, 0x0147, 0, 0, ComboBox1, WinTitle  ; 0x0147 is CB_GETCURSEL (for a DropDownList or ComboBox).
ChoicePos := ErrorLevel<<32>>32  ; Convert UInt to Int to have -1 if there is no item selected.
ChoicePos += 1  ; Convert from 0-based to 1-based, i.e. so that the first item is known as 1, not 0.

LineCount

Retrieves the number of lines in an Edit control.

ControlGet, OutputVar, LineCount ,, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

All Edit controls have at least 1 line, even if the control is empty.

CurrentLine

Retrieves the line number in an Edit control where the caret (insert point) resides.

ControlGet, OutputVar, CurrentLine ,, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

The first line is 1. If there is text selected in the control, OutputVar is set to the line number where the selection begins.

CurrentCol

Retrieves the column number in an Edit control where the caret (text insertion point) resides.

ControlGet, OutputVar, CurrentCol ,, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

The first column is 1. If there is text selected in the control, OutputVar is set to the column number where the selection begins.

Line

Retrieves the text of line N in an Edit control.

ControlGet, OutputVar, Line, N , Control, WinTitle, WinText, ExcludeTitle, ExcludeText

Line 1 is the first line. Depending on the nature of the control, OutputVar might end in a carriage return (`r) or a carriage return + linefeed (`r`n). If the specified line number is blank or does not exist, ErrorLevel is set to 1 and OutputVar is made blank.

Selected

Retrieves the selected text in an Edit control.

ControlGet, OutputVar, Selected ,, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

If no text is selected, OutputVar will be made blank and ErrorLevel will be set to 0 (i.e. no error). Certain types of controls, such as RichEdit20A, might not produce the correct text in some cases (e.g. Metapad).

Style

Retrieves an 8-digit hexadecimal number representing the style of the control.

ControlGet, OutputVar, Style ,, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

See the styles table for a listing of some styles.

ExStyle

Retrieves an 8-digit hexadecimal number representing the extended style of the control.

ControlGet, OutputVar, ExStyle ,, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

See the styles table for a listing of some styles.

Hwnd [v1.0.43.06+]

Retrieves the window handle (HWND) of the control.

ControlGet, OutputVar, Hwnd ,, Control, WinTitle, WinText, ExcludeTitle, ExcludeText

For example: ControlGet, OutputVar, Hwnd,, Edit1, WinTitle. A control's HWND is often used with PostMessage, SendMessage, and DllCall(). On a related note, a control's HWND can also be retrieved via MouseGetPos. Finally, a control's HWND can be used directly as an ahk_id WinTitle (this also works on hidden controls even when DetectHiddenWindows is Off).

Error Handling

[v1.1.04+]: This command is able to throw an exception on failure. For more information, see Runtime Errors.

Upon success, ErrorLevel is set to 0. If a problem occurred -- such as a nonexistent window or control -- ErrorLevel is set to 1 and OutputVar is made blank.

Remarks

Unlike commands that change a control, ControlGet does not have an automatic delay; that is, SetControlDelay does not affect it.

To discover the ClassNN or HWND of the control that the mouse is currently hovering over, use MouseGetPos. To retrieve a list of all controls in a window, use WinGet ControlList.

Control, GuiControlGet, ControlMove, ControlGetText, ControlSetText, ControlGetPos, ControlClick, ControlFocus, ControlSend, WinGet

Examples

Retrieves the first line of the first Edit control.

ControlGet, OutputVar, Line, 1, Edit1, Some Window Title

Retrieves the currently active tab of the first Tab control.

ControlGet, WhichTab, Tab,, SysTabControl321, Some Window Title
if ErrorLevel
    MsgBox There was a problem.
else
    MsgBox Tab #%WhichTab% is active.