Menu/MenuBar Object

Used to modify and display menus or menu bars.

Menu objects are used to define, modify and display popup menus. MenuCreate, MenuFromHandle and A_TrayMenu return an object of this type.

MenuBar objects are used to define and modify menu bars for use with Gui.MenuBar. MenuFromHandle returns an object of this type if given a menu bar handle.





Adds or modifies a menu item.

Menu.Add(MenuItemName, Callback-or-Submenu, Options)

The text to display on the menu item, or the position& of an existing item to modify. See MenuItemName.


A function name or a reference to a function object to call as a new thread when the menu item is selected, or a reference to a Menu object to use as a submenu.

If Callback-or-Submenu is omitted, MenuItemName will be used as both the function name and the menu item name.

The function can optionally define parameters as shown below:

FunctionName(ItemName, ItemPos, Menu)

If not omitted, Options must be a space- or tab-delimited list of one or more of the following options:

Pn Replace n with the menu item's thread priority, e.g. P1. If this option is omitted when adding a menu item, the priority will be 0, which is the standard default. If omitted when updating a menu item, the item's priority will not be changed. Use a decimal (not hexadecimal) number as the priority.
+Radio If the item is checked, a bullet point is used instead of a check mark.
+Right The item is right-justified within the menu bar. This only applies to menu bars, not popup menus or submenus.
+Break The item begins a new column in a popup menu.
+BarBreak As above, but with a dividing line between columns.

The plus sign (+) is optional and can be replaced with minus (-) to remove the option, as in -Radio. Options are not case sensitive.

To change an existing item's options without affecting its callback or submenu, simply omit the Callback-or-Submenu parameter.


This is a multipurpose method that adds a menu item, updates one with a new submenu or callback, or converts one from a normal item into a submenu (or vice versa). If MenuItemName does not yet exist, it will be added to the menu. Otherwise, MenuItemName is updated with the newly specified Callback-or-Submenu and/or Options.

To add a menu separator line, omit all three parameters.

Add always adds new menu items at the bottom of the menu, but Insert can be used to insert an item before an existing custom menu item.


Adds a visible checkmark in the menu next to MenuItemName (if there isn't one already).


The name or position of a menu item. See MenuItemName.


Retrieves or sets the number of clicks required to activate the tray menu's default item.

Menu.ClickCount := Count

Specify 1 to allow a single-click to activate the tray menu's default menu item. Specify 2 to return to the default behavior (double-click).

For example: A_TrayMenu.ClickCount := 1


Deletes a menu item or all custom menu items.


The name or position of a menu item. See MenuItemName.

If MenuItemName is omitted, all items are deleted from the menu, leaving the menu empty. An empty menu still exists and thus any other menus that use it as a submenu will retain those submenus.

To delete a separator line, identify it by its position in the menu. For example, use MyMenu.Delete("3&") if there are two items preceding the separator.

If the default menu item is deleted, the effect will be similar to having set Menu.Default := "".


Retrieves or sets the default menu item.

CurrentDefault := Menu.Default

The name of the default menu item, or an empty string if there is no default.

Menu.Default := MenuItemName

The name or position of a menu item. See MenuItemName.

If MenuItemName is an empty string, there will be no default.

Setting the default item makes that item's font bold (setting a default item in menus other than the tray menu is currently purely cosmetic). When the user double-clicks the tray icon, its default menu item is launched (even if the item is disabled). If there is no default, double-clicking has no effect.

The default item for the tray menu is initially &Open, if present. Adding &Open to the tray menu by calling AddStandard or changing A_AllowMainWindow also causes it to become the default item if there wasn't one already.

If the default item is deleted, the menu is left without one.


Changes MenuItemName to a gray color to indicate that the user cannot select it.


The name or position of a menu item. See MenuItemName.


Allows the user to once again select MenuItemName if it was previously disabled (grayed).


The name or position of a menu item. See MenuItemName.


Inserts a new item before the specified item.

Menu.Insert(ItemToInsertBefore, NewItemName, Callback-or-Submenu, Options)

The name of an existing item or a position& between 1 and the current number of custom items plus 1 (following the same rules as MenuItemName). Items can also be appended by omitting ItemToInsertBefore.


The text to display on the menu item. Unlike Add, this cannot be a position.

The remaining parameters behave as per the Add method, except that Insert creates a new item even if NewItemName matches the name of an existing item.

As Standard menu items are not counted ("1&" always refers to the first custom item), an item cannot be inserted immediately before the standard items.


Renames MenuItemName to NewName.

Menu.Rename(MenuItemName , NewName)

The name or position of a menu item. See MenuItemName.


The new name. If empty or omitted, MenuItemName will be converted into a separator line.

The menu item's current callback or submenu is unchanged.

A separator line can be converted to a normal item by specifying the position& of the separator and a non-blank NewName, and then using the Add method to give the item a callback or submenu.


Changes the background color of the menu to ColorValue.

Menu.SetColor(ColorValue, Submenus := true)

One of the 16 primary HTML color names, a hexadecimal RGB color string (the 0x prefix is optional), or a pure numeric RGB color value. Omit ColorValue (or specify an empty string or the word "Default") to restore the menu to its default color. Example values: "Silver", "FFFFAA", 0xFFFFAA, "Default".


True if the color should be applied to all of this menu's submenus, otherwise false. Defaults to true.


Sets the icon to be displayed next to MenuItemName.

Menu.SetIcon(MenuItemName, FileName , IconNumber, IconWidth)

The name or position of a menu item. See MenuItemName.


The path of an icon or image file. For a list of supported formats, see the Picture control.

A bitmap or icon handle can be used instead of a filename. For example, "HICON:" handle.

Omit FileName or specify an empty string or "*" to remove the item's current icon.


To use an icon group other than the first one in the file, specify its number for IconNumber (if omitted, it defaults to 1). If IconNumber is negative, its absolute value is assumed to be the resource ID of an icon within an executable file.


The desired width of the icon. If the icon group indicated by IconNumber contains multiple icon sizes, the closest match is used and the icon is scaled to the specified size. See the Examples section for usage examples.

Currently it is necessary to specify "actual size" when setting the icon to preserve transparency on Windows Vista and later. For example:

MyMenu.SetIcon "My menu item", "Filename.png",, 0

Known limitation: Icons on Gui menu bars are positioned incorrectly on Windows XP and older.

A bitmap or icon handle can be used instead of a filename. For example, "HBITMAP:" handle.


Displays the menu, allowing the user to select an item with arrow keys, menu shortcuts (underlined letters), or the mouse.

Menu.Show(X, Y)
X, Y

The coordinates at which to display the menu. If both X and Y are omitted, the menu is displayed at the current position of the mouse cursor. If only one of them is omitted, the mouse cursor's position will be used for it. X and Y are relative to the active window's client area by default. To override this default, use CoordMode, "Menu", Mode or A_CoordModeMenu := Mode.

Any popup menu can be shown, including submenus and the tray menu. However, an exception is thrown if Menu is a MenuBar object.


Adds a checkmark if there wasn't one; otherwise, removes it.


The name or position of a menu item. See MenuItemName.


Disables MenuItemName if it was previously enabled; otherwise, enables it.


The name or position of a menu item. See MenuItemName.


Removes the checkmark (if there is one) from a menu item.


The name or position of a menu item. See MenuItemName.


Adds the standard tray menu items.


This method can be used with the tray menu or any other menu.

The standard items are inserted after any existing items. Any standard items already in the menu are not duplicated, but any missing items are added. The table below shows the names and positions of the standard items after calling AddStandard on an empty menu:

&Window Spy4
&Reload This Script5
&Edit This Script6
&Suspend Hotkeys81
&Pause Script92

Compiled scripts include only the last three by default. &Open is included only if A_AllowMainWindow is 1 when AddStandard is called (in that case, add 1 to the positions shown in the third column). If the tray menu contains standard items, &Open is inserted or removed whenever A_AllowMainWindow is changed. For other menus, &Open has no effect if A_AllowMainWindow is 0.

Each standard item has an internal menu item ID corresponding to the function it performs, but can otherwise be modified or deleted like any other menu item. AddStandard detects existing items by ID, not by name. If the Add method is used to change the callback function associated with a standard menu item, it is assigned a new unique ID and is no longer considered to be a standard item.

Adding the &Open item to the tray menu causes it to become the default item if there wasn't one already.


Returns a handle to a Win32 menu (a handle of type HMENU), constructing it if necessary.


The returned handle is valid only until the Win32 menu is destroyed, which typically occurs when the Menu object is freed. Once the menu is destroyed, the operating system may reassign the handle value to any menus subsequently created by the script or any other program.

The name or position of a menu item. Some common rules apply to this parameter across all sub-commands which use it:

To underline one of the letters in a menu item's name, precede that letter with an ampersand (&). When the menu is displayed, such an item can be selected by pressing the corresponding key on the keyboard. To display a literal ampersand, specify two consecutive ampersands as in this example: "Save && Exit"

When referring to an existing menu item, the name is not case sensitive but any ampersands must be included. For example: "&Open"

The names of menu items can be up to 260 characters long.

To identify an existing item by its position in the menu, write the item's position followed by an ampersand. For example, "1&" indicates the first item.

Win32 Menus

Windows provides a set of functions and notifications for creating, modifying and displaying menus with standard appearance and behavior. We refer to a menu created by one of these functions as a Win32 menu.

As items are added to a menu or modified, the name and other properties of each item are stored in the Menu object. A Win32 menu is constructed the first time the menu or its parent menu is attached to a GUI or shown. It is destroyed automatically when the menu object is deleted (which occurs when its reference count reaches zero).

Menu.Handle returns a handle to a Win32 menu (a handle of type HMENU), constructing it if necessary.

Any modifications which are made to the menu directly by Win32 functions are not reflected by the script's Menu object, so may be lost if an item is modified by one of the built-in methods.

Each menu item is assigned an ID when it is first added to the menu. Scripts cannot rely on an item receiving a particular ID, but can retrieve the ID of an item by using GetMenuItemID as shown in example #5. This ID cannot be used with the Menu object, but can be used with various Win32 functions.


If a menu ever becomes completely empty -- such as by using MyMenu.Delete() -- it cannot be shown. If the tray menu becomes empty, right-clicking and double-clicking the tray icon will have no effect (in such cases it is usually better to use #NoTrayIcon).

If a menu item's callback is already running and the user selects the same menu item again, a new thread will be created to run that same callback, interrupting the previous thread. To instead buffer such events until later, use Critical as the callback's first line (however, this will also buffer/defer other threads such as the press of a hotkey).

Whenever a function is called via a menu item, it starts off fresh with the default values for settings such as SendMode. These defaults can be changed in the auto-execute section.

When building a menu whose contents are not always the same, one approach is to point all such menu items to the same function and have that function refer to its parameters to determine what action to take. Alternatively, a function object, closure or fat arrow function can be used to bind one or more values or variables to the menu item's callback function.

GUI, Threads, Thread, Critical, #NoTrayIcon, Gosub, Functions, Return, SetTimer


; EXAMPLE #1: This is a working script that adds a new menu item to the bottom of the tray icon menu.

A_TrayMenu.Add()  ; Creates a separator line.
A_TrayMenu.Add("Item1", "MenuHandler")  ; Creates a new menu item.

MenuHandler(ItemName, ItemPos, Menu) {
    MsgBox "You selected " ItemName " (position " ItemPos ")"
; EXAMPLE #2: This is a working script that creates a popup menu that is displayed when the user presses the Win-Z hotkey.

; Create the popup menu by adding some items to it.
MyMenu := MenuCreate()
MyMenu.Add "Item 1", "MenuHandler"
MyMenu.Add "Item 2", "MenuHandler"
MyMenu.Add  ; Add a separator line.

; Create another menu destined to become a submenu of the above menu.
Submenu1 := MenuCreate()
Submenu1.Add "Item A", "MenuHandler"
Submenu1.Add "Item B", "MenuHandler"

; Create a submenu in the first menu (a right-arrow indicator). When the user selects it, the second menu is displayed.
MyMenu.Add "My Submenu", Submenu1

MyMenu.Add  ; Add a separator line below the submenu.
MyMenu.Add "Item 3", "MenuHandler"  ; Add another menu item beneath the submenu.
return  ; End of script's auto-execute section.

MenuHandler(Item) {
    MsgBox "You selected " Item

#z::MyMenu.Show  ; i.e. press the Win-Z hotkey to show the menu.
; EXAMPLE #3: This is a working script that demonstrates some of the various menu object members.

global tray := A_TrayMenu ; For convenience.
tray.delete ; Delete the standard items.
tray.add ; separator
tray.add "TestToggleCheck"
tray.add "TestToggleEnable"
tray.add "TestDefault"
tray.add "TestAddStandard"
tray.add "TestDelete"
tray.add "TestDeleteAll"
tray.add "TestRename"
tray.add "Test"


    tray.ToggleCheck "TestToggleCheck"
    tray.Enable "TestToggleEnable" ; Also enables the next test since it can't undo the disabling of itself.
    tray.add "TestDelete" ; Similar to above.

    tray.ToggleEnable "TestToggleEnable"

    if tray.default = "TestDefault"
        tray.default := ""
        tray.default := "TestDefault"


    tray.delete "TestDelete"

    MsgBox "The script may exit now, as the tray menu no longer contains custom items."

    if NewName <> "renamed"
        OldName := "TestRename"
        NewName := "renamed"
        OldName := "renamed"
        NewName := "TestRename"
    tray.rename OldName, NewName

    MsgBox 'You selected "' Item '"'
; EXAMPLE #4: This is a working script that adds icons to its menu items.

FileMenu := MenuCreate()
FileMenu.Add("Script Icon", "MenuHandler")
FileMenu.Add("Suspend Icon", "MenuHandler")
FileMenu.Add("Pause Icon", "MenuHandler")
FileMenu.SetIcon("Script Icon", A_AhkPath, 2) ; 2nd icon group from the file
FileMenu.SetIcon("Suspend Icon", A_AhkPath, -206) ; icon with resource ID 206
FileMenu.SetIcon("Pause Icon", A_AhkPath, -207) ; icon with resource ID 207
MyMenuBar := MenuBarCreate()
MyMenuBar.Add("&File", FileMenu)
Gui := GuiCreate()
Gui.MenuBar := MyMenuBar
Gui.Add("Button",, "Exit This Example").OnEvent("Click", () => WinClose())

MenuHandler() {
    ; For this example, the menu items don't do anything.
; EXAMPLE #5: Retrieving item count and ID.

MyMenu := MenuCreate()
MyMenu.Add "Item 1", "no"
MyMenu.Add "Item 2", "no"
MyMenu.Add "Item B", "no"

; Retrieve the number of items in a menu.
item_count := DllCall("GetMenuItemCount", "ptr", MyMenu.Handle)

; Retrieve the ID of the last item.
last_id := DllCall("GetMenuItemID", "ptr", MyMenu.Handle, "int", item_count-1)

MsgBox "MyMenu has " item_count " items, and its last item has ID " last_id

no() {
    ; Do nothing.