MMenu

By majkinetor

MMenu is the replacement for the standard AutoHotKey Menu command.  Among other options, it alows menu items to have icons, and solves limitation of standard menus about item identification – besides title, which is nonreliable way to identify items, MMenu gives you an option to identify menu items by ID or position.  It gives you full control over the menu you created, even while it is displayed.

Summary
By majkinetor
Creates new menu and sets its options.
Adds a new menu item into the specified menu.
Removes the item from the given menu and if item opens the submenu, deattaches submenu.
Set item’s title and/or icon and/or options on already crated items.
Destroy the menu and its submenus.
Get the count of items in the menu
Closes the currently opened menu.
Show the menu on screen position X, Y.
Displays the message containing version and other information.
MMenu is open source project and can be used freely.
Basic usage

Create

MMenu_Create( pOptions  =  "" )

Creates new menu and sets its options.  It returns menu handle that is used in other menu functions.

Parameters

You can specify any combination of following options (space separated):

s<size> size of the icons in this menu, default = 32
o<num> number of pixels to skip between icon and the text, default = 0
c<color> background color of the menu, in hexademical format, omit 0x
t<color> default text color
h<height> maximum height of the menu (does not work with b option for menu items ,see below)

Returns

Menu handle

Examples

;
myMenu := Menu_Create() ;create with default options
myMenu := Menu_Create("S64 O20") ;set icon size to 64 and offset to 20
myMenu := Menu_Create("C0 TFFFFFF") ;set background color to black and text color to white

Add

MMenu_Add( pMenu,   
pTitle  =  "",
pIcon  =  "",
pItem  =  0,
pOptions  =  "" )

Adds a new menu item into the specified menu.

Parameters

pMenu Menu handle returned by Create function
pTitle Optional title of the menu item
pIcon Optional path to the icon for the item; if you set the number, it will be seen as the icon handle.  You can use 32x32 icons from resources if you specify :idx after icon path ( shell32.dll:4 )
pItem One of the 3 methods to identify the menu item.  See item identification bellow.  If number, position for the new item, default = 0 means that item will be appended.  If name, new item will be inserted before the item referenced by that name.
pOptions Options for the menu item, see bellow

Options

i<num> menu item ID specified by the syntax of the Autohotkey variable name.
s item is separator; omiting all optional parameters has the same effect
m<handle> item is submenu and will open the menu with given handle
g item is disabled (grayed)
c item is checked
b [|] horizontaly break menu at this item’s position; to draw a line between the break item and next menu part, set b|
d use bold fonts for the item
t text color to use for the item, otherwise, use default text color specified with Create’s T parameter

Returns

false if there was an error

Item identification

There are 3 methods to identify the item – by ID, by 1-based position and by title.  You can use anything that is more appropriate in your scripting environment.  All of MMenu functions dealing with items have item parameter that you use to point on specific menu item.  If you set item to be positive number, it will be seen as a position.  If you pass string instead of number, it will be seen as as item ID.  Item ID is any vaild Autohotkey name that you associate with item when you add it to the menu.  If you set the ID to the already existing, you will remove ID of the item previously associated with it.  Item ID is unique acros all menus that you create.  If item ends with A_SPACE, it will be seen as items title without ending space (the reason for this is that valid item ID’s can not have space).  As you can have more items with the same title, this will return the one with lowest position in the menu.

;
1 - identifies the item at position 1
"myItem" - identifies the item with ID=myItem
"myItem " - identifies the item with Title=myItem

Examples

;
Menu_Add( myMenu, "title 1") ;append item, set the title
Menu_Add( myMenu, "title 2", "icons\item.ico") ;append item, set the title and icon
Menu_Add( myMenu, "title 3", "", 3) ;add item without icon on third position
Menu_Add( myMenu, "title 4", "", , "imyItem c") ;append item with ID=myItem, set its title and check it
Menu_Add( myMenu, "title 5", "", "myItem") ;add item without icon before item with ID=myItem
Menu_Add( myMenu ) ;append separator

mySubMenu := Menu_Create()
Menu_Add( myMenu, "My Submenu", "icons\submenu.ico", 2, "M" mySubMenu) ;add submenu on second position

Remove

MMenu_Remove( pMenu,   
pItem  =  )

Removes the item from the given menu and if item opens the submenu, deattaches submenu.  If you specify item by title, the item with the lowest position with that title will be removed.

Parameters

pMenu menu that holds the item to be removed
pItem one of the 3 methods to identify the item

Returns

true on success, false if error occured

Note

If you want to remove the item using its title as identification you must A_SPACE at the end of it.

Without space it will be understood as item ID or item position, depending on title value

;
Menu_Remove(main, "1") ;remove the item with position=1
Menu_Remove(main, "1 ") ;remove the item with title=1

Set

MMenu_Set( pMenu,   
pItem,   
pTitle  =  "",
pIcon  =  "",
pOptions  =  "" )

Set item’s title and/or icon and/or options on already crated items.

Parameters

pMenu Handle of the menu containing the item
pItem Item identifier, the same rules as in Remove function (ID, position or title)
pTitle New title
pIcon New icon
pOptions the same as in Add function

Notes

If you want to remove title or icon use A_SPACE instead of empty string.

Use the empty string to leave the option untouched.

To unset the flag option, use – in front of it (-c –b –m -g). d option can not be unset.

Examples

Menu_Set( myMenu, 1,            "new title","","i33" )      ;change the title and ID of first item
Menu_Set( myMenu, "new title ", "", A_SPACE ,"c" ) ;check the first item with title"new title", and remove icon
Menu_Set( myMenu, "i101", "","disabled.ico","g -c" ) ;gray and uncheck item with ID=i101 and change its icon

Destroy

MMenu_Destroy( pMenu )

Destroy the menu and its submenus.  Submenus are destroyed recursively.

If you don’t want to destroy submenus first deattach them with MMenu.Remove function or use MMenu.Set with an -m option.

Parameters

pMenu Menu handle of the menu you want to destroy

Count

MMenu_Count( pMenu )

Get the count of items in the menu

Parameters : pMenu - Menu handle

Returns

Item count

Hide

MMenu_Hide()

Closes the currently opened menu.

Examples

To show the context menu for the menu item when it is right clicked you can set up things like this:

;
mainMenu := MMenu_Create(), contextMenu := MMenu_Create()
...
Mmenu_Show( mainMenu, 0, 0, “OnMainMenuClick, “ROnMainRightClick”)
...

OnMainRightClick:
MMenu_GetPosition(M_MENU, cX, cY, true) ;get coordinates of clicked item
MMenu_Hide()
SetTimer, LaunchContextMenu, 50 ;allow mmenu to close (show function to return)
Return

LaunchContextMenu:
SetTimer, LaunchContextMenu, off
MMenu_Show(contextMenu, cX, cY, "OnContextMenuClick")
Return

Show

MMenu_Show( pMenu,   
pX,   
pY,   
pOnClick,   
pHandlers  =  "" )

Show the menu on screen position X, Y.

You can not call Show function while it is already running.

Parameters

pMenu Handle of the menu to be shown
pX, pY Screen coordinates where the menu will be shown.  Menu will reposition itself if it can not fit the screen.
pOnClick User soubroutine that will be called upon item selection.  M_MENU, M_TITLE and M_ID global variables will contain the information about selected item
pHandlers String containing optional user soubroutines that will be called upon certain event.  See below.

Events

S This notificiation is received when user selects the menu item.  Sets M_SMENU, M_STITLE, M_SID
I The menu is going to be shown.  (initialise).  Sets M_MENU
U The menu is going to be closed (uninitialise).  Sets M_MENU
R The menu item was right clicked.  Sets M_MENU, M_TITLE, M_ID
M The menu item was middle clicked.  Sets M_MENU, M_TITLE, M_ID
C The menu has received character input.  Sets M_CMENU, M_CHAR.  It Doesn’t report ampersend keys, like “t” in “i&tem” as that one will select item or open submenu.

Example

 
MMenu_Show( mainMenu, X, Y, "OnClick", "SOnSelect UOnUninit")
...

OnSelect:
MMenu_GetPosition(M_SMENU, X, Y) ; get the position of the currently open menu
msg := aMyTooltips%M_SID% ; aMyTooltips is array containing tooltips for eatch item
Tooltip, %msg%, % X + 5, % Y – 30 ; and display tooltip above the current menu
Return

OnUninit:
Tooltip ;close the tooltip when the (sub)menu closes
Return

About

MMenu_About()

Displays the message containing version and other information.

Notes

MMenu is open source project and can be used freely.  However, if you use MMenu, provide appropriate credits in your documentation.  One of the ways is to put the button in your about window that will launch About function.

Feel free to send me comments and bug reports.

Created by

Miodrag Milic miodra.nosp@m.g.milic@gmai.nosp@m.l.com

Examples

Basic usage

;
myMenu := MMenu_Create(), mySubMenu := MMenu_Create()

s = %A_WinDir%\System32\Shell32.dll
MMenu_Add(myMenu,"CD-ROM", s ":" 190, 0, "iv101 d")
MMenu_Add(myMenu,"Recycle Bin", s ":" 145, 0, "iv102 c")
MMenu_Add(myMenu)
MMenu_Add(myMenu,"Drives", s ":" 3, 0, "iv103 m" . mySubMenu)

MMenu_Add(mySubMenu,"c:\", s ":"8)
MMenu_Add(mySubMenu,"d:\", s ":"8)
MMenu_Add(mySubMenu,"network", s ":" 9, 0, "g")

MouseGetPos, x, y
MMenu_Show(myMenu, x, y, "OnItemClick")
Return

OnItemClick:
s :="myMenu"
If (M_MENU = mySubMenu)
s :="mySubMenu"
MsgBox, Menu: %s%`nItem: %M_TITLE% (ID=%M_ID%)
Return
MMenu_Create( pOptions  =  "" )
Creates new menu and sets its options.
MMenu_Add( pMenu,   
pTitle  =  "",
pIcon  =  "",
pItem  =  0,
pOptions  =  "" )
Adds a new menu item into the specified menu.
MMenu_Remove( pMenu,   
pItem  =  )
Removes the item from the given menu and if item opens the submenu, deattaches submenu.
MMenu_Set( pMenu,   
pItem,   
pTitle  =  "",
pIcon  =  "",
pOptions  =  "" )
Set item’s title and/or icon and/or options on already crated items.
MMenu_Destroy( pMenu )
Destroy the menu and its submenus.
MMenu_Count( pMenu )
Get the count of items in the menu
MMenu_Hide()
Closes the currently opened menu.
MMenu_Show( pMenu,   
pX,   
pY,   
pOnClick,   
pHandlers  =  "" )
Show the menu on screen position X, Y.
MMenu_About()
Displays the message containing version and other information.