GUI Object

Provides an interface for creating and managing windows, and creating controls. Such windows can be used as data entry forms or custom user interfaces. GuiCreate and GuiFromHwnd returns an object of this type.

Properties:

Methods:

General:

Add

Adds a control to the GUI window, and returns a GuiControl object.

Gui.Add(ControlType , Options, Text)
Gui.AddControlType(Options, Text)
ControlType

Type: String

This is one of the following: Text, Edit, UpDown, Picture, Button, Checkbox, Radio, DropDownList, ComboBox, ListBox, ListView, TreeView, Link, Hotkey, DateTime, MonthCal, Slider, Progress, GroupBox, Tab, StatusBar, ActiveX, Custom

For example:

Gui := GuiCreate()
Gui.Add("Text",, "Please enter your name:")
Gui.AddEdit("vName")
Gui.Show
Options

Type: String

Positioning and Sizing of Controls

If some dimensions and/or coordinates are omitted from Options, the control will be positioned relative to the previous control and/or sized automatically according to its nature and contents.

The following options are supported:

R: Rows of text (can contain a floating point number such as R2.5). R is often preferable to specifying H (Height). If both the R and H options are present, R will take precedence. For a GroupBox, this setting is the number of controls for which to reserve space inside the box. For DropDownLists, ComboBoxes, and ListBoxes, it is the number of items visible at one time inside the list portion of the control (but on Windows XP or later, it is often desirable to omit both the R and H options for DropDownList and ComboBox, which makes the popup list automatically take advantage of the available height of the user's desktop). For other control types, R is the number of rows of text that can visibly fit inside the control.

W: Width, in pixels. If omitted, the width is calculated automatically for some control types based on their contents. The other controls types have the following default widths:
Tab controls: 30 times the current font size, plus 3 times the X-margin.
Vertical Progress Bars: Two times the current font size.
Horizontal Progress Bars, horizontal Sliders, DropDownLists, ComboBoxes, ListBoxes, GroupBoxes, Edits, and Hotkeys: 15 times the current font size (except GroupBoxes, which multiply by 18 to provide room inside for margins).

H: Height, in pixels. If both the H and R options are absent, DropDownLists, ComboBoxes, ListBoxes, and empty multi-line Edit controls default to 3 rows; GroupBoxes default to 2 rows; vertical Sliders and Progress Bars default to 5 rows; horizontal Sliders default to 30 pixels (except if a thickness has been specified); horizontal Progress Bars default to 2 times the current font size; Hotkey controls default to 1 row; and Tab controls default to 10 rows. For the other control types, the height is calculated automatically based on their contents. Note that for DropDownLists and ComboBoxes, H is the combined height of the control's always-visible portion and its list portion (but even if the height is set too low, at least one item will always be visible in the list). Also, for all types of controls, specifying the number of rows via the R option is usually preferable to using H because it prevents a control from showing partial/incomplete rows of text.

wp+n, hp+n, wp-n, hp-n (where n is any number) can be used to set the width and/or height of a control equal to the previously added control's width or height, with an optional plus or minus adjustment. For example, wp would set a control's width to that of the previous control, and wp-50 would set it equal to 50 less than that of the previous control.

X: X-position. For example, specifying x0 y0 would position the control in the upper left corner of the window's client area, which is the area beneath the title bar and menu bar (if any). If X is omitted but not Y, the control will be positioned to the right of all previously added controls, which can be thought of as starting a new "column".

Y: Y-position. If Y is omitted but not X, the control will be positioned beneath all previously added controls, which can be thought of as starting a new "row".

Omitting either X, Y or both is useful to make a GUI layout automatically adjust to any future changes you might make to the size of controls or font. By contrast, specifying an absolute position for every control might require you to manually shift the position of all controls that lie beneath and/or to the right of a control that is being enlarged or reduced.

If both X and Y are omitted, the control will be positioned beneath the previous control using a standard padding distance.

For X and Y, an optional plus sign can be included to position a control relative to the right or bottom edge (respectively) of the control that was previously added. For example, specifying Y+10 would position the control 10 pixels beneath the bottom of the previous control rather than using the standard padding distance. Similarly, specifying X+10 would position the control 10 pixels to the right of the previous control's right edge. Since negative numbers such as X-10 are reserved for absolute positioning, to use a negative offset, include a plus sign in front of it. For example: X+-10.

For X+ and Y+, the letter M can be used as a substitute for the window's current margin. For example, x+m uses the right edge of the previous control plus the standard padding distance. xp y+m positions a control below the previous control, whereas specifying an X coordinate on its own would normally imply yp by default.

xp+n, yp+n, xp-n, yp-n (where n is any number) can be used to position controls relative to the previous control's upper left corner, which is often useful for enclosing controls in a GroupBox.

xm and ym can be used to position a control at the leftmost and topmost margins of the window, respectively (these two may also be followed by a plus/minus sign and a number). By specifying ym without any x-position at all, the control will be positioned at the top margin but to the right of all previously added controls, which can be thought of as starting a new "column". The converse is also true.

xs and ys: these are similar to xm and ym except that they refer to coordinates that were saved by having previously added a control with the word Section in its options (the first control of the window always starts a new section, even if that word isn't specified in its options). By specifying ys without any x-position at all, the control will be positioned at the previously saved y-position, but to the right of all controls that have been added since the most recent use of the word Section; this can be thought of as starting a new column within the section. For example:

Gui := GuiCreate()
Gui.Add("Edit", "w600")  ; Add a fairly wide edit control at the top of the window.
Gui.Add("Text", "section", "First Name:")  ; Save this control's position and start a new section.
Gui.Add("Text",, "Last Name:")
Gui.Add("Edit", "ys")  ; Start a new column within this section.
Gui.Add("Edit")
Gui.Show

The converse of the above (specifying xs but omitting the y-position) is also true.

xs and ys may optionally be followed by a plus/minus sign and a number. Also, it is possible to specify both the word Section and xs/ys in a control's options; this uses the previous section for itself but establishes a new section for subsequent controls.

Storing and Responding to User Input

V: Sets the control's Name. Specify the name immediately after the letter V, which is not included in the name. For example, specifying vMyEdit would name the control "MyEdit".

Events: Event handlers (such as a function which is called automatically when the user clicks or changes a control) cannot be set within the control's Options. Instead, OnEvent can be used to register a callback function or method for each event of interest.

Controls: Common Styles and Other Options

Note: In the absence of a preceding sign, a plus sign is assumed; for example, Wrap is the same as +Wrap. By contrast, -Wrap would remove the word-wrapping property.

AltSubmit: Uses alternate submit method. For DropDownList, ComboBox, ListBox and Tab, this causes Gui.Submit to store the position of the selected item rather than its text. If no item is selected, a ComboBox will still store the text of its edit field.

C: Color of text (has no effect on buttons). Specify the letter C followed immediately by a color name (see color chart) or RGB value (the 0x prefix is optional). Examples: cRed, cFF2211, c0xFF2211, cDefault.

Disabled: Makes an input-capable control appear in a disabled state, which prevents the user from focusing or modifying its contents. Use GuiCtrl.Enabled to enable it later. Note: To make an Edit control read-only, specify the string ReadOnly instead. Also, the word Disabled may optionally be followed immediately by a 0 or 1 to indicate the starting state (0 for enabled and 1 for disabled). In other words, Disabled and "Disabled" VarContainingOne are the same.

Hidden: The control is initially invisible. Use GuiCtrl.Visible to show it later. The word Hidden may optionally be followed immediately by a 0 or 1 to indicate the starting state (0 for visible and 1 for hidden). In other words, Hidden and "Hidden" VarContainingOne are the same.

Left: Left-justifies the control's text within its available width. This option affects the following controls: Text, Edit, Button, Checkbox, Radio, UpDown, Slider, Tab, Tab2, GroupBox, DateTime.

Right: Right-justifies the control's text within its available width. For checkboxes and radio buttons, this also puts the box itself on the right side of the control rather than the left. This option affects the following controls: Text, Edit, Button, Checkbox, Radio, UpDown, Slider, Tab, Tab2, GroupBox, DateTime, Link.

Center: Centers the control's text within its available width. This option affects the following controls: Text, Edit, Button, Checkbox, Radio, Slider, GroupBox.

Section: Starts a new section and saves this control's position for later use with the xs and ys positioning options described above.

Tabstop: Use -Tabstop (i.e. minus Tabstop) to have an input-capable control skipped over when the user presses the Tab key to navigate.

Wrap: Enables word-wrapping of the control's contents within its available width. Since nearly all control types start off with word-wrapping enabled, use -Wrap to disable word-wrapping.

VScroll: Provides a vertical scroll bar if appropriate for this type of control.

HScroll: Provides a horizontal scroll bar if appropriate for this type of control. The rest of this paragraph applies to ListBox only. The horizontal scrolling width defaults to 3 times the width of the ListBox. To specify a different scrolling width, include a number immediately after the word HScroll. For example, HScroll500 would allow 500 pixels of scrolling inside the ListBox. However, if the specified scrolling width is smaller than the width of the ListBox, no scroll bar will be shown (though the mere presence of HScroll makes it possible for the horizontal scroll bar to be added later via MyScrollBar.Opt("+HScroll500"), which is otherwise impossible).

Controls: Uncommon Styles and Options

BackgroundTrans: Uses a transparent background, which allows any control that lies behind a Text, Picture, or GroupBox control to show through. For example, a transparent Text control displayed on top of a Picture control would make the text appear to be part of the picture. Use GuiCtrl.Opt("+Background") to remove this option later. See Picture control's AltSubmit section for more information about transparent images. Known limitation: BackgroundTrans might not work properly for controls inside a Tab control that contains a ListView. If a control type does not support this option, an error is thrown.

BackgroundColor: Changes the background color of the control. Replace Color with a color name (see color chart) or RGB value (the 0x prefix is optional). Examples: BackgroundSilver, BackgroundFFDD99. If this option is not present, a Text, Picture, GroupBox, CheckBox, Radio, Slider, Tab or Link control initially defaults to the background color set by Gui.BackColor (or if none or other control type, the system's default background color). Specifying BackgroundDefault or -Background applies the system's default background color. For example, a control can be restored to the default color via LV.Opt("+BackgroundDefault"). Using +Background without specifying a color reverts -Background. If a control type does not support this option, an error is thrown.

Border: Provides a thin-line border around the control. Most controls do not need this because they already have a type-specific border. When adding a border to an existing control, it might be necessary to increase the control's width and height by 1 pixel.

Theme: This option can be used to override the window's current theme setting for the newly created control. It has no effect when used on an existing control; however, this may change in a future version. See GUI's +/-Theme option for details.

(Unnamed Style): Specify a plus or minus sign followed immediately by a decimal or hexadecimal style number. If the sign is omitted, a plus sign is assumed.

(Unnamed ExStyle): Specify a plus or minus sign followed immediately by the letter E and a decimal or hexadecimal extended style number. If the sign is omitted, a plus sign is assumed. For example, E0x200 would add the WS_EX_CLIENTEDGE style, which provides a border with a sunken edge that might be appropriate for pictures and other controls. Although the other extended styles are not documented here (since they are rarely used), they can be discovered by searching for WS_EX_CLIENTEDGE at www.microsoft.com.

Text
Depending on the specified control type, a string, number or an array.

Show

By default, this makes the window visible, unminimizes it (if necessary) and activates it.

Gui.Show(Options)
Options

Type: String

Omit the X, Y, W, and H options below to have the window retain its previous size and position. If there is no previous position, the window will be auto-centered in one or both dimensions if the X and/or Y options mentioned below are absent. If there is no previous size, the window will be auto-sized according to the size and positions of the controls it contains.

Zero or more of the following strings may be present in Options (specify each number as decimal, not hexadecimal):

Wn: Specify for n the width (in pixels) of the window's client area (the client area excludes the window's borders, title bar, and menu bar).

Hn: Specify for n the height of the window's client area, in pixels.

Xn: Specify for n the window's X-position on the screen, in pixels. Position 0 is the leftmost column of pixels visible on the screen.

Yn: Specify for n the window's Y-position on the screen, in pixels. Position 0 is the topmost row of pixels visible on the screen.

Center: Centers the window horizontally and vertically on the screen.

xCenter: Centers the window horizontally on the screen. For example: Gui.Show("xCenter y0").

yCenter: Centers the window vertically on the screen.

AutoSize: Resizes the window to accommodate only its currently visible controls. This is useful to resize the window after new controls are added, or existing controls are resized, hidden, or unhidden. For example:
Gui.Show("AutoSize Center")

One of the following may also be present:

Minimize: Minimizes the window and activates the one beneath it.

Maximize: Maximizes and activates the window.

Restore: Unminimizes or unmaximizes the window, if necessary. The window is also shown and activated, if necessary.

NoActivate: Unminimizes or unmaximizes the window, if necessary. The window is also shown without activating it.

NA: Shows the window without activating it. If the window is minimized, it will stay that way but will probably rise higher in the z-order (which is the order seen in the alt-tab selector). If the window was previously hidden, this will probably cause it to appear on top of the active window even though the active window is not deactivated.

Hide: Hides the window and activates the one beneath it. This is identical in function to Gui.Hide except that it allows a hidden window to be moved or resized without showing it. For example: Gui.Show("Hide x55 y66 w300 h200").

Submit

Saves the contents of controls into an associative array (object) and returns it.

NamedCtrlContents := Gui.Submit(Hide := true)
Hide

Type: Integer (boolean)

If omitted or 1 (true), the window will be hidden. If 0 (false), the window will not be hidden.

The returned array contains one element per control, usually like array[GuiCtrl.Name] := GuiCtrl.Value, with the exceptions noted below. Only input-capable controls which support GuiCtrl.Value and have been given a name are included.

For DropDownList, ComboBox, ListBox and Tab, the text of the selected item/tab is stored instead of its position number if the control lacks the AltSubmit option, or if the ComboBox's text does not match a list item. Otherwise, Value (the item's position number) is stored.

If only one Radio button in a radio group has a name, Submit stores the number of the currently selected button instead of the control's Value. 1 is the first radio button (according to original creation order), 2 is the second, and so on. If there is no button selected, 0 is stored.

Excluded because they are not input-capable: Text, Pic, GroupBox, Button, Progress, Link, StatusBar.

Also excluded: ListView, TreeView, ActiveX, Custom.

Hide / Cancel

Hides the window.

Gui.Hide()
Gui.Cancel()

Destroy

Removes the window and all its controls, freeing the corresponding memory and system resources.

Gui.Destroy()

If Gui.Destroy() is not used, the window is automatically destroyed when the Gui object is deleted (see General Remarks for details). All GUI windows are automatically destroyed when the script exits.

SetFont

Sets the font typeface, size, style, and/or color for controls added to the window from this point onward.

Note: Omit both parameters to restore the font to the system's default GUI typeface, size, and color. Otherwise, any font attributes which are not specified will be copied from the previous font.

Gui.SetFont(Options, FontName)
Options

Type: String

Zero or more options. Each option is either a single letter immediately followed by a value, or a single word. To specify more than one option, include a space between each. For example: cBlue s12 bold.

The following words are supported: bold, italic, strike, underline, and norm. Norm returns the font to normal weight/boldness and turns off italic, strike, and underline (but it retains the existing color and size). It is possible to use norm to turn off all attributes and then selectively turn on others. For example, specifying norm italic would set the font to normal then to italic.

C: Color name (see color chart) or RGB value -- or specify the word Default to return to the system's default color (black on most systems). Example values: cRed, cFFFFAA, cDefault. Note: Buttons do not obey custom colors. Also, an individual control can be created with a font color other than the current one by including the C option. For example: Gui.Add("Text", "cRed", "My Text").

S: Size (in points). For example: s12 (specify decimal, not hexadecimal)

W: Weight (boldness), which is a number between 1 and 1000 (400 is normal and 700 is bold). For example: w600 (specify decimal, not hexadecimal)

Q: Text rendering quality. For example: q3. Q should be followed by a number from the following table:

0 = DEFAULT_QUALITY Appearance of the font does not matter.
1 = DRAFT_QUALITY Appearance of the font is less important than when the PROOF_QUALITY value is used.
2 = PROOF_QUALITY Character quality of the font is more important than exact matching of the logical-font attributes.
3 = NONANTIALIASED_QUALITY Font is never antialiased, that is, font smoothing is not done.
4 = ANTIALIASED_QUALITY Font is antialiased, or smoothed, if the font supports it and the size of the font is not too small or too large.
5 = CLEARTYPE_QUALITY Windows XP and later: If set, text is rendered (when possible) using ClearType antialiasing method.

For more details of what these values mean, see MSDN: CreateFont.

Since the highest quality setting is usually the default, this feature is more typically used to disable anti-aliasing in specific cases where doing so makes the text clearer.

FontName

Type: String

FontName may be the name of any font, such as one from the font table. If FontName is omitted or does not exist on the system, the previous font's typeface will be used (or if none, the system's default GUI typeface). This behavior is useful to make a GUI window have a similar font on multiple systems, even if some of those systems lack the preferred font. For example, by using the following methods in order, Verdana will be given preference over Arial, which in turn is given preference over MS sans serif:

Gui.SetFont(, "MS sans serif")
Gui.SetFont(, "Arial")
Gui.SetFont(, "Verdana")  ; Preferred font.

On a related note, the operating system offers standard dialog boxes that prompt the user to pick a font, color, or icon. These dialogs can be displayed via DllCall as demonstrated at GitHub.

BackColor

Retrieves or sets the background color of the window.

RetrievedColor := Gui.BackColor
Gui.BackColor := NewColor

RetrievedColor is a 6-digit RGB value of the current color previously set by this property, or an empty string if the default color is being used.

NewColor is one of the 16 primary HTML color names, a hexadecimal RGB color value (the 0x prefix is optional), a pure numeric RGB color value, or the word Default (or an empty string) for its default color. Example values: "Silver", "FFFFAA", 0xFFFFAA, "Default", "".

By default, the window's background color is the system's color for the face of buttons.

The color of the menu bar and its submenus can be changed as in this example: MyMenuBar.SetColor "White".

To make the background transparent, use WinSetTransColor. However, if you do this without first having assigned a custom window via Gui.BackColor, buttons will also become transparent. To prevent this, first assign a custom color and then make that color transparent. For example:

Gui.BackColor := "EEAA99"
WinSetTransColor("EEAA99")

To additionally remove the border and title bar from a window with a transparent background, use the following: Gui.Opt("-Caption")

To illustrate the above, there is an example of an on-screen display (OSD) near the bottom of this page.

MarginX

Retrieves or sets the size of horizontal margins between sides and subsequently created controls.

RetrievedValue := Gui.MarginX
Gui.MarginX := NewValue

RetrievedValue is the number of pixels of the current horizontal margin.

NewValue is the number of pixels of space to leave at the left and right side of the window when auto-positioning any control that lacks an explicit X coordinate. Also, the margin is used to determine the horizontal distance that separates auto-positioned controls from each other. Finally, the margin is taken into account by the first use of Gui.Show to calculate the window's size (when no explicit size is specified).

By default, this margin is proportional to the size of the currently selected font (1.25 times font-height for left & right).

MarginY

Retrieves or sets the size of vertical margins between sides and subsequently created controls.

RetrievedValue := Gui.MarginY
Gui.MarginY := NewValue

RetrievedValue is the number of pixels of the current vertical margin.

NewValue is the number of pixels of space to leave at the top and bottom side of the window when auto-positioning any control that lacks an explicit Y coordinate. Also, the margin is used to determine the vertical distance that separates auto-positioned controls from each other. Finally, the margin is taken into account by the first use of Gui.Show to calculate the window's size (when no explicit size is specified).

By default, this margin is proportional to the size of the currently selected font (0.75 times font-height for top & bottom).

Opt

Sets one or more options for the GUI window.

Gui.Opt(Options)
Gui.Options(Options)
Options

Type: String

For performance reasons, it is better to set all options in a single line, and to do so before creating the window (that is, before any use of other methods such as Gui.Add).

The effect of this parameter is cumulative; that is, it alters only those settings that are explicitly specified, leaving all the others unchanged.

Specify a plus sign to add the option and a minus sign to remove it. For example: Gui.Opt("+Resize -MaximizeBox").

AlwaysOnTop: Makes the window stay on top of all other windows, which is the same effect as WinSetAlwaysOnTop.

Border: Provides a thin-line border around the window. This is not common.

Caption (present by default): Provides a title bar and a thick window border/edge. When removing the caption from a window that will use WinSetTransColor, remove it only after setting the TransColor.

Delimiter: Specifies that the window should use a field separator other than pipe (|) whenever controls' contents are added via Gui.Add or modified via GuiControl object. Specify a single character immediately after the word Delimiter. For example, Gui.Opt("+Delimiter`n") would use a linefeed character, which might be especially appropriate with continuation sections. Similarly, Gui.Opt("+Delimiter|") would revert to the default delimiter. To use space or tab, specify Gui.Opt("+DelimiterSpace") or Gui.Opt("+DelimiterTab"). Once the delimiter is changed, it affects all existing and subsequent threads that operate on this particular window.

Disabled: Disables the window, which prevents the user from interacting with its controls. This is often used on a window that owns other windows (see Owner).

DPIScale: Use Gui.Opt("-DPIScale") to disable DPI scaling, which is enabled by default. If DPI scaling is enabled on a system with a non-standard DPI setting, the Gui object and GuiControl object automatically scale coordinates and sizes to give controls roughly the same apparent size (but higher resolution). For example, with a DPI of 144 (150%), E := Gui.Add("Edit", "w100") would make the GUI control 150 pixels wide, but E.Pos.W would still return 100. A_ScreenDPI contains the system's current DPI.

DPI scaling only applies to the Gui object and GuiControl object, so coordinates coming directly from other sources such as ControlGetPos or WinGetPos will not work. There are a number of ways to deal with this:

  • Avoid using hard-coded coordinates wherever possible. For example, use the xp, xs, xm and x+m options for positioning controls and specify height in rows of text instead of pixels.
  • Enable (Gui.Opt("+DPIScale")) and disable (Gui.Opt("-DPIScale")) scaling on the fly, as needed. Changing the setting does not affect positions or sizes which have already been set.
  • Manually scale the coordinates. For example, x*(A_ScreenDPI/96) converts x from logical/GUI coordinates to physical/non-GUI coordinates.

LastFound: Sets the window to be the last found window (though this is unnecessary in a GUI thread because it is done automatically), which allows functions such as WinGetStyle and WinSetTransparent to operate on it even if it is hidden (that is, DetectHiddenWindows is not necessary). This is especially useful for changing the properties of the window before showing it. For example:

Gui.Opt("+LastFound")
WinSetTransColor(CustomColor " 150")
Gui.Show()

MaximizeBox: Enables the maximize button in the title bar. This is also included as part of Resize below.

MinimizeBox (present by default): Enables the minimize button in the title bar.

MinSize and MaxSize: Determines the minimum and/or maximum size of the window, such as when the user drags its edges to resize it. Specify the word MinSize and/or MaxSize with no suffix to use the window's current size as the limit (if the window has no current size, it will use the size from the first use of Gui.Show). Alternatively, append the width, followed by an X, followed by the height; for example: Gui.Opt("+Resize +MinSize640x480"). The dimensions are in pixels, and they specify the size of the window's client area (which excludes borders, title bar, and menu bar). Specify each number as decimal, not hexadecimal.

Either the width or the height may be omitted to leave it unchanged (e.g. +MinSize640x or +MinSizex480). Furthermore, Min/MaxSize can be specified more than once to use the window's current size for one dimension and an explicit size for the other. For example, +MinSize +MinSize640x would use the window's current size for the height and 640 for the width.

If MinSize and MaxSize are never used, the operating system's defaults are used (similarly, Gui.Opt("-MinSize -MaxSize") can be used to return to the defaults). Note: the window must have +Resize to allow resizing by the user.

OwnDialogs: Gui.Opt("+OwnDialogs") should be specified in each thread (such as a event handling function of a Button control) for which subsequently displayed MsgBox, InputBox, FileSelect, and DirSelect dialogs should be owned by the window. Such dialogs are modal, meaning that the user cannot interact with the GUI window until dismissing the dialog. By contrast, ToolTip do not become modal even though they become owned; they will merely stay always on top of their owner. In either case, any owned dialog or window is automatically destroyed when its GUI window is destroyed.

There is typically no need to turn this setting back off because it does not affect other threads. However, if a thread needs to display both owned and unowned dialogs, it may turn off this setting via Gui.Opt("-OwnDialogs").

Owner: Use +Owner to make the window owned by another. An owned window has no taskbar button by default, and when visible it is always on top of its owner. It is also automatically destroyed when its owner is destroyed. +Owner can be used before or after the owned window is created. There are two ways to use +Owner, as shown below:

Gui.Opt("+OwnerMyOtherGui")  ; Make the GUI owned by MyOtherGui.
Gui.Opt("+Owner")  ; Make the GUI owned by script's main window to prevent display of a taskbar button.

+Owner can be immediately followed by the name or number of an existing GUI or the HWND of any top-level window.

To prevent the user from interacting with the owner while one of its owned window is visible, disable the owner via Gui.Opt("+Disabled"). Later (when the time comes to cancel or destroy the owned window), re-enable the owner via Gui.Opt("-Disabled"). Do this prior to cancel/destroy so that the owner will be reactivated automatically.

Parent: Use +Parent immediately followed by the name or number of an existing GUI or the HWND of any window or control to use it as the parent of this window. To convert the GUI back into a top-level window, use -Parent. This option works even after the window is created.

Resize: Makes the window resizable and enables its maximize button in the title bar. To avoid enabling the maximize button, specify +Resize -MaximizeBox.

SysMenu (present by default): Specify -SysMenu (minus SysMenu) to omit the system menu and icon in the window's upper left corner. This will also omit the minimize, maximize, and close buttons in the title bar.

Theme: By specifying -Theme, all subsequently created controls in the window will have Classic Theme appearance on Windows XP and beyond. To later create additional controls that obey the current theme, turn it back on via +Theme. Note: This option has no effect on operating systems older than Windows XP, nor does it have any effect on XP itself if the Classic Theme is in effect. Finally, this setting may be changed for an individual control by specifying +Theme or -Theme in its options when it is created.

ToolWindow: Provides a narrower title bar but the window will have no taskbar button. This always hides the maximize and minimize buttons, regardless of whether the WS_MAXIMIZEBOX and WS_MINIMIZEBOX styles are present.

(Unnamed Style): Specify a plus or minus sign followed immediately by a decimal or hexadecimal style number.

(Unnamed ExStyle): Specify a plus or minus sign followed immediately by the letter E and a decimal or hexadecimal extended style number. For example, +E0x40000 would add the WS_EX_APPWINDOW style, which provides a taskbar button for a window that would otherwise lack one. Although the other extended styles are not documented here (since they are rarely used), they can be discovered by searching for WS_EX_APPWINDOW at www.microsoft.com.

FocusedCtrl

Retrieves the GuiControl object of the GUI's focused control.

GuiCtrlObj := Gui.FocusedCtrl

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

Minimize

Unhides the window (if necessary) and minimizes it.

Gui.Minimize()

Maximize

Unhides the window (if necessary) and maximizes it.

Gui.Maximize()

Restore

Unhides the window (if necessary) and restores it, if it was minimized or maximized beforehand.

Gui.Restore()

Pos

Retrieves the position and size of the window.

PosSizeObj := Gui.Pos

PosSizeObj is an object with the keys X (x coordinate), Y (y coordinate), W (width) and H (height). The coordinates are the upper left corner of the window. Width is the horizontal distance between the left and right side of the window, and height the vertical distance between the top and bottom side (in pixels).

ClientPos

Retrieves the position and size of the window's client area.

PosSizeObj := Gui.ClientPos

PosSizeObj is an object with the keys X (x coordinate), Y (y coordinate), W (width) and H (height). The coordinates are the upper left corner of the window's client area, which is the area not including title bar, menu bar, and borders. Width is the horizontal distance between the left and right side of the client area, and height the vertical distance between the top and bottom side (in pixels).

Flash

Blinks the window's button in the taskbar.

Gui.Flash(Blink := true)
Blink

Type: Integer (boolean)

If this parameter is omitted or 1 (true), the window's button in the taskbar will blink. This is done by inverting the color of the window's title bar and/or taskbar button (if it has one). Specify 0 (false) to restore the original colors of the title bar and taskbar button (but the actual behavior might vary depending on OS version).

In the below example, the window will blink three times because each pair of flashes inverts then restores its appearance:

Loop 6
{
    Gui.Flash
    Sleep 500  ; It's quite sensitive to this value; altering it may change the behavior in unexpected ways.
}

Hwnd

Retrieves the window handle (HWND) of the GUI window.

CurrentHwnd := Gui.Hwnd

A GUI's HWND is often used with PostMessage, SendMessage, and DllCall. It can also be used directly as an ahk_id WinTitle.

Title

Retrieves or sets the GUI's title.

RetrievedTitle := Gui.Title
Gui.Title := NewTitle

Name

Retrieves or sets a custom name for the GUI window.

RetrievedName := Gui.Name
Gui.Name := NewName

Control

Retrieves the GuiControl object associated with the specified name, text, ClassNN or HWND.

GuiCtrlObj := Gui.ControlName

_NewEnum

Allows to iterate through the GUI's controls.

Enum := Gui._NewEnum()

Returns a new enumerator to enumerate the HWND and GuiControl object of each control inside a GUI.

This method is usually not called directly, but by the for-loop. For example:

For Hwnd, GuiCtrlObj in GuiObj
    MsgBox "Control #" A_Index " is " GuiCtrlObj.ClassNN

A GUI window may be navigated via the Tab key, which moves keyboard focus to the next input-capable control (controls from which the Tabstop style has been removed are skipped). The order of navigation is determined by the order in which the controls were originally added. When the window is shown for the first time, the first input-capable control that has the Tabstop style (which most control types have by default) will have keyboard focus.

Certain controls may contain an ampersand (&) to create a keyboard shortcut, which might be displayed in the control's text as an underlined character (depending on system settings). A user activates the shortcut by holding down the Alt key then typing the corresponding character. For buttons, checkboxes, and radio buttons, pressing the shortcut is the same as clicking the control. For GroupBoxes and Text controls, pressing the shortcut causes keyboard focus to jump to the first input-capable tabstop control that was created after it. However, if more than one control has the same shortcut key, pressing the shortcut will alternate keyboard focus among all controls with the same shortcut.

To display a literal ampersand inside the control types mentioned above, specify two consecutive ampersands as in this example: Gui.Add("Button",, "Save && Exit").

Window Appearance

For its icon, a GUI window uses the tray icon that was in effect at the time the window was created. Thus, to have a different icon, change the tray icon before creating the window. For example: TraySetIcon("MyIcon.ico"). It is also possible to have a different large icon for a window than its small icon (the large icon is displayed in the alt-tab task switcher). This can be done via LoadPicture and SendMessage; for example:

iconsize := 32  ; Ideal size for alt-tab varies between systems and OS versions.
hIcon := LoadPicture("My Icon.ico", "Icon1 w" iconsize " h" iconsize, imgtype)
Gui := GuiCreate("+LastFound")
SendMessage(0x80, 1, hIcon)  ; 0x80 is WM_SETICON; and 1 means ICON_BIG (vs. 0 for ICON_SMALL).
Gui.Show()

Due to OS limitations, Checkboxes, Radio buttons, and GroupBoxes for which a non-default text color was specified will take on Classic Theme appearance on Windows XP and beyond.

Related topic: window's margin.

General Remarks

Use the GuiControl object to operate upon individual controls in a GUI window.

Each GUI window may have up to 11,000 controls. However, use caution when creating more than 5000 controls because system instability may occur for certain control types.

The GUI window is automatically destroyed when the Gui object is deleted, which occurs when its reference count reaches zero. However, this does not typically occur while the window is visible, as Show automatically increments the reference count. While the window is visible, the user can interact with it and raise events which are handled by the script. When the user closes the window or it is hidden by Hide/Cancel, Show or Submit, this extra reference is released.

To keep a GUI window "alive" without calling Show or retaining a reference to its Gui object, the script can increment the object's reference count with ObjAddRef (in which case ObjRelease must be called when the window is no longer needed). For example, this might be done when using a hidden GUI window to receive messages, or if the window is shown by "external" means such as WinShow (by this script or any other).

If the script is not persistent for any other reason, it will exit after the last visible GUI is closed; either when the last thread completes or immediately if no threads are running.

GuiCreate, GuiControl object, GuiFromHwnd, GuiCtrlFromHwnd, Control Types, ListView, TreeView, Menu object, Control functions, MsgBox, FileSelect, DirSelect

Examples

#1: Display a text pop-up:

Gui := GuiCreate(, "Title of Window")
Gui.Opt("+AlwaysOnTop +Disabled -SysMenu +Owner")  ; +Owner avoids a taskbar button.
Gui.Add("Text",, "Some text to display.")
Gui.Show("NoActivate")  ; NoActivate avoids deactivating the currently active window.

#2: A simple input-box that asks for the first and last name:

Gui := GuiCreate(, "Simple Input Example")
Gui.Add("Text",, "First name:")
Gui.Add("Text",, "Last name:")
Gui.Add("Edit", "vFirstName ym")  ; The ym option starts a new column of controls.
Gui.Add("Edit", "vLastName")
Gui.Add("Button", "default", "OK").OnEvent("Click", (*) => ProcessUserInput(Gui))
Gui.OnEvent("Close", "ProcessUserInput")
Gui.Show()

ProcessUserInput(this, *)
{
    Saved := this.Submit()  ; Save the contents of named controls into an object.
    MsgBox("You entered '" Saved.FirstName " " Saved.LastName "'.")
}

#3: Creates a tab control with multiple tabs, each containing different controls to interact with:

Gui := GuiCreate()
Tab := Gui.Add("Tab3",, "First Tab|Second Tab|Third Tab")
Gui.Add("Checkbox", "vMyCheckbox", "Sample checkbox") 
Tab.UseTab(2)
Gui.Add("Radio", "vMyRadio", "Sample radio1")
Gui.Add("Radio",, "Sample radio2")
Tab.UseTab(3)
Gui.Add("Edit", "vMyEdit r5")  ; r5 means 5 rows tall.
Tab.UseTab()  ; i.e. subsequently-added controls will not belong to the tab control.
Btn := Gui.Add("Button", "default xm", "OK")  ; xm puts it at the bottom left corner.
Btn.OnEvent("Click", (*) => ProcessUserInput(Gui))
Gui.OnEvent("Close", "ProcessUserInput")
Gui.OnEvent("Escape", "ProcessUserInput")
Gui.Show()

ProcessUserInput(this, *)
{
    Saved := this.Submit()  ; Save the contents of named controls into an object.
    MsgBox("You entered:`n" Saved.MyCheckbox "`n" Saved.MyRadio "`n" Saved.MyEdit)
}

#4: Creates a ListBox control containing files in a directory:

Gui := GuiCreate()
Gui.Add("Text",, "Pick a file to launch from the list below.")
LB := Gui.Add("ListBox", "w640 r10 vFile")
LB.OnEvent("DoubleClick", "LaunchFile")
Loop Files, "C:\*.*"  ; Change this folder and wildcard pattern to suit your preferences.
    LB.Add(A_LoopFilePath)
Gui.Add("Button", "Default", "OK").OnEvent("Click", "LaunchFile")
Gui.Show()

LaunchFile(this, *)
{
    Saved := this.Gui.Submit()
    if MsgBox("Would you like to launch the file or document below?`n`n" Saved.File,, 4) = "No"
        return
    ; Otherwise, try to launch it:
    try Run(Saved.File)
    if A_LastError
        MsgBox("Could not launch the specified file. Perhaps it is not associated with anything.")
}

#5: Displays a context-sensitive help (via ToolTip) whenever the user moves the mouse over a particular control:

Gui := GuiCreate()
Gui.Add("Edit", "vMyEdit")
MyEdit_TT := "This is a tooltip for the control whose name is MyEdit."
Gui.Add("DropDownList", "vMyDDL", "Red|Green|Blue")
MyDDL_TT := "Choose a color from the drop-down list."
Gui.Add("Checkbox", "vMyCheckBox", "This control has no tooltip.")
Gui.OnEvent("Close", (*) => ExitApp())
Gui.Show()
OnMessage(0x200, "WM_MOUSEMOVE")

WM_MOUSEMOVE(wParam, lParam, msg, Hwnd)
{
    global  
    static PrevHwnd
    if (Hwnd != PrevHwnd)
    {
        Text := "", ToolTip() ; Turn off any previous tooltip.
        CurrControl := GuiCtrlFromHwnd(Hwnd)
        if CurrControl
        {
            Text := %CurrControl.Name%_TT
            SetTimer () => ToolTip(Text), -1000
            SetTimer () => ToolTip(), -4000 ; Remove the tooltip.
        }
        PrevHwnd := Hwnd
    }
}

#6: Creates an On-screen display (OSD) via transparent window:

Gui := GuiCreate()
Gui.Opt("+LastFound +AlwaysOnTop -Caption +ToolWindow")  ; +ToolWindow avoids a taskbar button and an alt-tab menu item.
Gui.BackColor := "EEAA99"  ; Can be any RGB color (it will be made transparent below).
Gui.SetFont("s32")  ; Set a large font size (32-point).
CoordText := Gui.Add("Text", "cLime", "XXXXX YYYYY")  ; XX & YY serve to auto-size the window.
; Make all pixels of this color transparent and make the text itself translucent (150):
WinSetTransColor(Gui.BackColor " 150")
SetTimer(() => UpdateOSD(CoordText), 200)
UpdateOSD(CoordText)  ; Make the first update immediate rather than waiting for the timer.
Gui.Show("x0 y400 NoActivate")  ; NoActivate avoids deactivating the currently active window.

UpdateOSD(GuiCtrl)
{
    MouseGetPos(MouseX, MouseY)
    GuiCtrl.Value := "X" MouseX ", Y" MouseY
}

#7: A moving progress bar overlayed on a background image:

Gui := GuiCreate()
Gui.BackColor := "White"
Gui.Add("Picture", "x0 y0 h350 w450", A_WinDir "\Web\Wallpaper\Windows\img0.jpg")
MyBtn := Gui.Add("Button", "Default xp+20 yp+250", "Start the Bar Moving")
MyBtn.OnEvent("Click", "MoveBar")
Gui.Add("Progress", "w416 vMyProgress")
Gui.Add("Text", "wp vMyText")  ; wp means "use width of previous".
Gui.Show()

MoveBar(this, *)
{
    ProgressCtrl := this.Gui.Control["MyProgress"]
    TextCtrl := this.Gui.Control["MyText"]
    Loop Files, A_WinDir "\*.*"
    {
        if (A_Index > 100)
            break
        ProgressCtrl.Value := A_Index
        TextCtrl.Value := A_LoopFileName
        Sleep 50
    }
    TextCtrl.Value := "Bar finished."
}

#8: A simple image viewer:

Gui := GuiCreate("+Resize")
MyBtn := Gui.Add("Button", "default", "&Load New Image")
MyBtn.OnEvent("Click", "LoadNewImage")
Gui.Add("Radio", "ym+5 x+10 checked vMyRadio", "Load &actual size")
Gui.Add("Radio", "ym+5 x+10", "Load to &fit screen")
Gui.Add("Pic", "xm vMyPic")
Gui.Show()

LoadNewImage(this, *)
{
    Gui := this.Gui
    RadioCtrl := Gui.Control["MyRadio"]
    PicCtrl := Gui.Control["MyPic"]
    File := FileSelect(,, "Select an image:", "Images (*.gif; *.jpg; *.bmp; *.png; *.tif; *.ico; *.cur; *.ani; *.exe; *.dll)")
    if File = ""
        return
    if RadioCtrl.Value = 1  ; Display image at its actual size.
    {
        Width := 0
        Height := 0
    }
    else ; Second radio is selected: Resize the image to fit the screen.
    {
        Width := A_ScreenWidth - 28  ; Minus 28 to allow room for borders and margins inside.
        Height := -1  ; "Keep aspect ratio" seems best.
    }
    PicCtrl.Value := Format("*w{1} *h{2} {3}", Width, Height, File)  ; Load the image.
    Gui.Title := File
    Gui.Show("xCenter y0 AutoSize")  ; Resize the window to match the picture size.
}

#9: A simple text editor with menu bar:

; Make these variables accessible for all the functions below:
global Gui, FileMenu, MainEdit, CurrentFileName, About

; Create the GUI window:
Gui := GuiCreate("+Resize", "Untitled")  ; Make the window resizable.

; Create the submenus for the menu bar:
FileMenu := MenuCreate()
FileMenu.Add("&New", "MenuFileNew")
FileMenu.Add("&Open", "MenuFileOpen")
FileMenu.Add("&Save", "MenuFileSave")
FileMenu.Add("Save &As", "MenuFileSaveAs")
FileMenu.Add() ; Separator line.
FileMenu.Add("E&xit", "MenuFileExit")
HelpMenu := MenuCreate()
HelpMenu.Add("&About", "MenuHelpAbout")

; Create the menu bar by attaching the submenus to it:
MyMenuBar := MenuBarCreate()
MyMenuBar.Add("&File", FileMenu)
MyMenuBar.Add("&Help", HelpMenu)

; Attach the menu bar to the window:
Gui.MenuBar := MyMenuBar

; Create the main Edit control:
MainEdit := Gui.Add("Edit", "WantTab W600 R20")

; Apply events:
Gui.OnEvent("DropFiles", "Gui_DropFiles")
Gui.OnEvent("Size", "Gui_Size")

MenuFileNew()  ; Apply default settings.
Gui.Show()  ; Display the window.

MenuFileNew(*)
{
    MainEdit.Value := ""  ; Clear the Edit control.
    FileMenu.Disable("3&")  ; Gray out &Save.
    Gui.Title := "Untitled"
}

MenuFileOpen(*)
{
    Gui.Opt("+OwnDialogs")  ; Force the user to dismiss the FileSelect dialog before returning to the main window.
    SelectedFileName := FileSelect(3,, "Open File", "Text Documents (*.txt)")
    if SelectedFileName = "" ; No file selected.
        return
    CurrentFileName := readContent(SelectedFileName)
}

MenuFileSave(*)
{
    saveContent(CurrentFileName)
}

MenuFileSaveAs(*)
{
    Gui.Opt("+OwnDialogs")  ; Force the user to dismiss the FileSelect dialog before returning to the main window.
    SelectedFileName := FileSelect("S16",, "Save File", "Text Documents (*.txt)")
    if SelectedFileName = "" ; No file selected.
        return
    CurrentFileName := saveContent(SelectedFileName)
}

MenuFileExit(*)  ; User chose "Exit" from the File menu.
{
    WinClose()
}

MenuHelpAbout(*)
{
    About := GuiCreate("+owner" Gui.Hwnd)  ; Make the main window the owner of the "about box".
    Gui.Opt("+Disabled")  ; Disable main window.
    About.Add("Text",, "Text for about box.")
    About.Add("Button", "Default", "OK").OnEvent("Click", "About_Close")
    About.OnEvent("Close", "About_Close")
    About.OnEvent("Escape", "About_Close")
    About.Show()
}

readContent(FileName)
{
    FileContent := FileRead(FileName)  ; Read the file's contents into the variable.
    if ErrorLevel
    {
        MsgBox("Could not open '" FileName "'.")
        return
    }
    MainEdit.Value := FileContent  ; Put the text into the control.
    FileMenu.Enable("3&")  ; Re-enable &Save.
    Gui.Title := FileName  ; Show file name in title bar.
    return FileName
}

saveContent(FileName)
{
    if FileExist(FileName)
    {
        FileDelete(FileName)
        if ErrorLevel
        {
            MsgBox("The attempt to overwrite '" FileName "' failed.")
            return
        }
    }
    FileAppend(MainEdit.Value, FileName)  ; Save the contents to the file.
    ; Upon success, Show file name in title bar (in case we were called by MenuFileSaveAs):
    Gui.Title := FileName
    return FileName
}

About_Close(*)
{
    Gui.Opt("-Disabled")  ; Re-enable the main window (must be done prior to the next step).
    About.Destroy()  ; Destroy the about box.
}

Gui_DropFiles(Gui, Ctrl, FileArray, *)  ; Support drag & drop.
{
    CurrentFileName := readContent(FileArray[1])  ; Read the first file only (in case there's more than one).
}

Gui_Size(Gui, MinMax, Width, Height)
{
    if MinMax = -1  ; The window has been minimized. No action needed.
        return
    ; Otherwise, the window has been resized or maximized. Resize the Edit control to match.
    MainEdit.Move("W" Width-20 " H" Height-20)
}