Menu/MenuBar Object

Used to modify and display menus or menu bars.

class Menu extends Object

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

class MenuBar extends Menu

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

"MyMenu" is used below as a placeholder for any Menu object, as "Menu" is the class itself.

In addition to the methods and property inherited from Object, Menu objects have the following predefined methods and properties.

Table of Contents

Static Methods


Creates a new Menu or MenuBar object.

MyMenu := Menu()
MyMenuBar := MenuBar()



Adds or modifies a menu item.

MyMenu.Add(MenuItemName, Function-or-Submenu, Options)

Type: String

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


Type: Function Object or Menu

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.

This parameter is required when creating a new item, but optional when updating the Options of an existing item.

The function should accept the following parameters:

FunctionName(ItemName, ItemPos, MyMenu)

Type: String

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

Option Description
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 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 Script5
&Edit 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.


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


Type: String

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


Deletes a menu item or all custom menu items.


Type: String

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 MyMenu.Default := "".


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


Type: String

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


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


Type: String

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


Inserts a new item before the specified item.

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

Type: String

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.


Type: String

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.


Renames MenuItemName to NewName.

MyMenu.Rename(MenuItemName , NewName)

Type: String

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


Type: String

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.

MyMenu.SetColor(ColorValue, ApplyToSubmenus)

Type: String or Integer

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".


Type: Boolean

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


Sets the icon to be displayed next to MenuItemName.

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

Type: String

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


Type: String

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.


Type: Integer

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.


Type: Integer

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. For example:

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

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.

MyMenu.Show(X, Y)
X, Y

Type: Integer

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.


Type: String

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


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


Type: String

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


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


Type: String

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.

MyMenu.ClickCount := Count

Type: Integer

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


Retrieves or sets the default menu item.

CurrentDefault := MyMenu.Default

Type: String

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

MyMenu.Default := MenuItemName

Type: String

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.


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 methods 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.


A menu usually looks like this:


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 during script startup.

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, Functions, Return, SetTimer


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, MyMenu) {
    MsgBox "You selected " ItemName " (position " ItemPos ")"

Creates a popup menu that is displayed when the user presses a hotkey.

; Create the popup menu by adding some items to it.
MyMenu := Menu()
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 := Menu()
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.

MenuHandler(Item, *) {
    MsgBox "You selected " Item

#z::MyMenu.Show  ; i.e. press the Win-Z hotkey to show the menu.

Demonstrates some of the various menu object members.

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


    tray.ToggleCheck "TestToggleCheck"
    tray.Enable "TestToggleEnable" ; Also enables the next test since it can't undo the disabling of itself.
    tray.add "TestDelete", "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."

    static OldName := "", NewName := ""
    if NewName != "renamed"
        OldName := "TestRename"
        NewName := "renamed"
        OldName := "renamed"
        NewName := "TestRename"
    tray.rename OldName, NewName

Test(Item, *)
    MsgBox 'You selected "' Item '"'

Demonstrates how to add icons to menu items.

FileMenu := Menu()
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 := MenuBar()
MyMenuBar.Add("&File", FileMenu)
MyGui := Gui()
MyGui.MenuBar := MyMenuBar
MyGui.Add("Button",, "Exit This Example").OnEvent("Click", (*) => WinClose())

MenuHandler(*) {
    ; For this example, the menu items don't do anything.

Reports the number of items in a menu and the ID of the last item.

MyMenu := Menu()
MyMenu.Add "Item 1", NoAction
MyMenu.Add "Item 2", NoAction
MyMenu.Add "Item B", NoAction

; 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

NoAction(*) {
    ; Do nothing.