GuiControl Object

Auto Hotkey

GuiControl Object

Provides an interface for modifying GUI controls and retrieving information about them. Gui.Add, Gui.ControlGuiCtrlFromHwnd and Gui._NewEnum returns an object of this type.

Properties:

  • ClassNN: Retrieves the ClassNN of the control.
  • Enabled: Retrieves the current interaction state of the control, or enables or disables (grays out) it.
  • Focused: Retrieves the current focus state of the control.
  • Gui: Retrieves the control's GUI parent.
  • Hwnd: Retrieves the HWND of the control.
  • Name: Retrieves or sets the explicit name of the control.
  • Pos: Retrieves the position and size of the control.
  • Text: Retrieves or sets the text/caption of the control.
  • Type: Retrieves the type of the control.
  • Value: Retrieves or sets new contents into a value-capable control.
  • Visible: Retrieves the current visibility state of the control, or shows or hides it.

Methods:

  • Add: Appends the specified entries at the current list of a ListBox, DropDownList, ComboBox, or Tab control.
  • Choose: Sets the selection in a ListBox, DropDownList, ComboBox, or Tab control to be the specified value.
  • Delete: Deletes the specified entry or all entries of a ListBox, DropDownList, ComboBox, or Tab control.
  • Focus: Sets keyboard focus to the control.
  • Move: Moves and/or resizes the control.
  • OnEvent: Registers a function or method to be called when the given event is raised.
  • Opt: Sets various options and styles for the appearance and behavior of the control.
  • SetFont: Sets the font typeface, size, style, and/or color for the control.
  • SetFormat: Sets the display format of a DateTime control.
  • UseTab: Causes subsequently added controls to be belong to the specified tab of a Tab control.

Type

Retrieves the type of the control.

Type := GuiCtrl.Type

For a full list of control types, see GUI control types.

Hwnd

Retrieves the window handle (HWND) of the control.

Hwnd := GuiCtrl.Hwnd

A control's HWND is often used with PostMessage, SendMessage, and DllCall.

Name

Retrieves or sets the name of the control.

RetrievedName := GuiCtrl.Name
GuiCtrl.Name := NewName

The control's name can be used with Gui.Control to retrieve the GuiControl object. For most input-capable controls, the name is also used by Gui.Submit.

ClassNN

Retrieves the ClassNN of the control.

ClassNN := GuiCtrl.ClassNN

Gui

Retrieves the Gui object of the control's parent GUI window.

GuiObj := GuiCtrl.Gui

Opt

Adds or removes various options and styles of the control.

GuiCtrl.Opt(Options)
GuiCtrl.Options(Options)
Options
Either control-specific or general options and styles.

In the following example, the control is disabled and its background is restored to the system default:

MyEdit.Opt("+Disabled -Background")

In the next example, the OK button is made the new default button:

OKButton.Opt("+Default")

Although styles and extended styles are also recognized, some of them cannot be applied or removed after a control has been created. ErrorLevel is set to 0 if at least one of the specified changes was successfully applied. Otherwise, it is set to 1 to indicate that none of the changes could be applied. Even if a change is successfully applied, the control might choose to ignore it.

Move

Moves and/or resizes the control, optionally with drawing it again.

GuiCtrl.Move(Pos , Draw := false)
Pos

One or more of the following option letters (specify each number as decimal, not hexadecimal):

Xn: Specify for n the new x-coordinate relative to the GUI window's client area, which is the area not including title bar, menu bar, and borders.

Yn: Specify for n the new y-coordinate relative to the GUI window's client area (see above).

Wn: Specify for n the new width of the control.

Hn: Specify for n the new height of the control.

Draw
If this parameter is true, the region of the GUI window occupied by the control will be repaint. Although this may cause an unwanted flickering effect when called repeatedly and rapidly, it solves painting artifacts for certain control types such as GroupBoxes.

Examples

MyEdit.Move("x10 y20 w200 h100", true)
MyEdit.Move("x%VarX+10% y%VarY+5% w%VarW*2% h%VarH*1.5%")

Focus

Sets keyboard focus to the control.

GuiCtrl.Focus()

Note: To be effective, the window generally must not be minimized or hidden.

Choose

Sets the selection in a ListBox, DropDownList, ComboBox, or Tab control to be the specified value.

GuiCtrl.Choose(Value)
Value

This parameter should be 1 for the first entry, 2 for the second, etc.

If Value is a string (even a numeric string), the entry whose leading part matches Value will be selected. The search is not case sensitive. For example, if the control contains the item "UNIX Text", specifying the word unix (lowercase) would be enough to select it. For a multi-select ListBox, all matching items are selected.

If Value is zero or empty, the current selection is removed.

To select or deselect all items in a multi-select ListBox, follow this example:

Gui.Opt("+LastFound")  ; Avoids the need to specify WinTitle below.
PostMessage(0x185, 1, -1, ListBox.ClassNN)  ; Select all items. 0x185 is LB_SETSEL.
PostMessage(0x185, 0, -1, ListBox.ClassNN)  ; Deselect all items.
ListBox.Choose(0)  ; Deselect all items.

Unlike Control Choose, this method does not raise a Change or DoubleClick event.

UseTab

Causes subsequently added controls to be belong to the specified tab of a Tab control.

GuiCtrl.UseTab(Value, ExactMatch := false)
Value
This parameter should be 1 for the first tab, 2 for the second, etc. If Value is not an integer, the tab whose leading part matches Value will be used. The search is not case sensitive. For example, if a the control contains the tab "UNIX Text", specifying the word unix (lowercase) would be enough to use it. If Value is zero, a blank string or omitted, subsequently controls are added outside the Tab control.
ExactMatch
If this parameter is true, Value has to be an exact match, but not case sensitive.

Add

Appends the specified entries at the current list of a ListBox, DropDownList, ComboBox, or Tab control.

GuiCtrl.Add(ArrayOrList)
ArrayOrList
A pipe-delimited list of entries (e.g. "Red|Green|Blue") or an array of entries (e.g. ["Red", "Green", "Blue"]) to be appended at the end of the control's list. To replace (overwrite) the list instead, use GuiCtrl.Delete beforehand. When using pipes, one of the entries can be pre-selected by including two pipes after it (e.g. "Red|Green||Blue"), otherwise use GuiCtrl.Choose. The separator between text fields may be changed to something other than pipe. For example, Gui.Opt("+Delimiter`n") would change it to linefeed and Gui.Opt("+DelimiterTab") would change it to tab (`t).

Related: ListView.Add, TreeView.Add

Delete

Deletes the specified entry or all entries of a ListBox, DropDownList, ComboBox, or Tab control.

GuiCtrl.Delete(Value)
Value
This parameter should be 1 for the first entry, 2 for the second, etc. If omitted, all entries are deleted.

For tab controls, you should note that a tab's sub-controls stay associated with their original tab number; that is, they are never associated with their tab's actual display-name. For this reason, renaming or removing a tab will not change the tab number to which the sub-controls belong. For example, if there are three tabs "Red|Green|Blue" and the second tab is removed by means of MyTab.Delete(2), the sub-controls originally associated with Green will now be associated with Blue. Because of this behavior, only tabs at the end should generally be removed. Tabs that are removed in this way can be added back later, at which time they will reclaim their original set of controls.

Related: ListView.Delete, TreeView.Delete

Value

Retrieves or sets the contents of a control.

RetrievedValue := GuiCtrl.Value
GuiCtrl.Value := NewValue

Note: If the control is not value-capable, RetrievedValue will be blank and assigning NewValue will raise an error. Invalid values throw an exception.

Following control types are value-capable:

Picture

RetrievedValue is the picture's file name as it was originally specified when the control was created. This name does not change even if a new picture file name is specified.

NewValue is the filename (or handle) of the new image to load (see Picture for supported file types). Zero or more of the following options may be specified immediately in front of the filename: *wN (width N), *hN (height N), and *IconN (icon group number N in a DLL or EXE file). In the following example, the default icon from the second icon group is loaded with a width of 100 and an automatic height via "keep aspect ratio": MyPic.Value("*icon2 *w100 *h-1 C:\My Application.exe"). Specify *w0 *h0 to use the image's actual width and height. If *w and *h are omitted, the image will be scaled to fit the current size of the control. When loading from a multi-icon .ICO file, specifying a width and height also determines which icon to load. Note: Use only one space or tab between the final option and the filename itself; any other spaces and tabs are treated as part of the filename.

Text

RetrievedValue is the text/caption of the control.

NewValue is the control's new text. Since the control will not expand automatically, use GuiCtrl.Move("w300") if the control needs to be widened.

Edit

RetrievedValue is the current content of the control. For multi-line controls, any line breaks in the text will be represented as plain linefeeds (`n) rather than the traditional CR+LF (`r`n) used by non-GUI commands such as ControlGetText and ControlSetText.

NewValue is the new content. For multi-line controls, Any linefeeds (`n) in NewValue that lack a preceding carriage return (`r) are automatically translated to CR+LF (`r`n) to make them display properly. However, this is usually not a concern because when using Gui.Submit() or when retrieving this value this translation will be reversed automatically by replacing CR+LF with LF (`n).

To retrieve or set the text without translating `n to or from `r`n, use GuiCtrl.Text.

Hotkey

RetrievedValue is the modifiers and key name if there is a hotkey in the control; otherwise blank. Examples: ^!C, ^Home, +^NumpadHome.

NewValue can be a set of modifiers with a key name, or blank to clear the control. Examples: ^!c, ^Numpad1, +Home. The only modifiers supported are ^ (Control), ! (Alt), and + (Shift). See the key list for available key names.

Checkbox / Radio

RetrievedValue is 1 if the control is checked, 0 if it is unchecked, or -1 if it has a gray checkmark.

NewValue can be 0 to uncheck the button, 1 to check it, or -1 to give it a gray checkmark. For Radio buttons, if one is being checked (turned on) and it is a member of a multi-radio group, the other radio buttons in its group will be automatically unchecked.

To get or set control's text/caption instead, use GuiCtrl.Text.

DateTime / MonthCal

RetrievedValue is a date-time stamp in YYYYMMDDHH24MISS format.

NewValue is a date-time stamp in YYYYMMDDHH24MISS format. Specify A_Now to use the current date and time (today). For DateTime controls, NewValue may be an empty string to cause the control to have no date/time selected (if it was created with that ability). For MonthCal controls, a range may be specified if the control is multi-select.

UpDown / Slider / Progress

RetrievedValue is the control's current position.

NewValue is the new position of the control, for example MySlider.Value := 10. To adjust the value by a relative amount, use the operators +=, -=, ++ or -- instead of :=. If the new position would be outside the range of the control, the control is generally set to the nearest valid value.

Tab / DropDownList / ComboBox / ListBox

RetrievedValue is the position of the currently selected item/tab or zero if none is selected. If text is entered into a ComboBox, the first item with matching text is used. If there is no matching item, the result is zero even if there is text in the control. For a multi-select ListBox, the result is an array of numbers (which is empty if no items are selected).

NewValue is the position of a single item/tab to select, or zero to clear the current selection (this is valid even for Tab controls). To select multiple items, call GuiCtrl.Choose repeatedly.

To get or set the selected item given its text instead of its position, use GuiCtrl.Text.

ActiveX

RetrievedValue is the ActiveX object. For example, if the control was created with the text Shell.Explorer, this is a WebBrowser object. The same wrapper object is returned each time.

Text

Retrieves or sets the text/caption of the control.

RetrievedText := GuiCtrl.Text
GuiCtrl.Text := NewText

Note: If the control has no visible caption text and no (single) text value, this property corresponds to the control's hidden caption text (like ControlGetText/ControlSetText).

Caption/display text: The Text property retrieves or sets the caption/display text of the following control types: Button, Checkbox, Edit, GroupBox, Link, Radio, Text. Since the control will not expand automatically, use GuiCtrl.Move("w300") or similar if the control needs to be widened.

DateTime: The Text property retrieves the formatted text displayed by the control. Assigning a formatted date/time string to the control is not supported. To change the date/time being displayed, assign GuiCtrl.Value a date-time stamp in YYYYMMDDHH24MISS format.

Edit: As with other controls, the text is retrieved or set as-is; no end-of-line translation is performed. To retrieve or set the text of a multi-line Edit control while also translating between `r`n and `n, use GuiCtrl.Value.

StatusBar: The Text property retrieves or sets the text of the first part only (use SB.SetText for greater flexibility).

List item text: The Text property retrieves or sets the currently selected item/tab for the following control types: Tab, DropDownList, ComboBox, ListBox.

NewText should be the full text (case insensitive) of the item/tab to select.

For a ComboBox, if there is no selected item, the text in the control's edit field is retrieved instead. For other controls, RetrievedText is empty/blank. Similarly, if NewText is empty/blank, the current item/tab will be deselected.

For a multi-select ListBox, RetrievedText is an array. NewText cannot be an array, but if multiple items match, they are all selected. To select multiple items with different text, call GuiCtrl.Choose repeatedly.

To select an item by its position number instead of its text, use GuiCtrl.Value.

SetFont

Sets the font typeface, size, style, and/or color for the control.

Note: Omit both parameters to set the font to the GUI's current font, as set by Gui.SetFont. Otherwise, any font attributes which are not specified will be copied from the control's previous font. Text color is changed only if specified in Options.

GuiCtrl.SetFont(Options, FontName)

For details about both parameters, see Gui.SetFont.

Enabled

Retrieves the current interaction state of the control, or enables or disables (grays out) it.

RetrievedState := GuiCtrl.Enabled
GuiCtrl.Enabled := NewState

For Tab controls, this will also enable or disable all of the tab's sub-controls. However, any sub-control explicitly disabled via GuiCtrl.Enabled := false will remember that setting and thus remain disabled even after its Tab control is re-enabled.

Visible

Retrieves the current visibility state of the control, or shows or hides it.

RetrievedState := GuiCtrl.Visible
GuiCtrl.Visible := NewState

For Tab controls, this will also show or hide all of the tab's sub-controls. If you additionally want to prevent a control's shortcut key (underlined letter) from working, disable the control via GuiCtrl.Enabled := false.

Focused

Retrieves the current focus state of the control.

RetrievedState := GuiCtrl.Focused

Note: To be effective, the window generally must not be minimized or hidden.

Pos

Retrieves the position and size of the control.

PosSizeObj := GuiCtrl.Pos

The position is relative to the GUI window's client area, which is the area not including title bar, menu bar, and borders. The information is stored in an object. For example:

value := MyEdit.Pos
MsgBox The X coordinate is %value.x%. The Y coordinate is %value.y%. The width is %value.w%. The height is %value.h%.