Win32 CreateMenu API Function

How to Create Menus Correctly using Windows Functions from Win32 API

Sep 17, 2008 Guy Lecky-Thompson

A Windows programming tutorial article using the Win32 API Functions CreateWindow and InsertMenuItem to create menus programatically instead of using a resource file.

There are many reasons to use the Win32 API to create menus programatically, rather than using a resource file. One of the most common uses is to provide context sensitive menus that change depending on what the user is doing with the application, and the rights that they have.

The following examples can be used with any language, but it is assumed that the reader is programming in C for the purpose of the definitions. The steps for creating menus are as follows:

  1. Create an empty menu;
  2. Insert items, if an item is a submenu, then
    1. Create empty menu;
    2. Insert items (and submenus);
    3. Assign to item being created.
  3. Attach menu to Window.

The middle step can be recursive, depending on how many levels the menu system needs to go down. Clearly, the first thing that has to be done is to create an empty menu.

Creating an Empty Menu with the Win32 API Function CreateMenu

The function definition is as follows:

  • HMENU CreateMenu (void)

This returns a valid menu handle, or NULL if the menu could not be created for some reason (very rare). The resulting handle can be assigned to a window using the SetMenu API function:

  • BOOL SetMenu(HWND, HMENU)

This function takes window handle and menu handle. If a menu is already attached to the window, it is replaced but not destroyed. To do this, use the DestroyMenu function:

  • BOOL DestroyMenu(HMENU);

Obviously, to use the above, it is necessary to have obtained a valid menu handle. This is done with GetMenu:

  • HMENU GetMenu(HWND);

If individual items need to be removed, this can be achieved with the DeleteMenu function:

  • BOOL DeleteMenu(HMENU, UINT, UINT);

The first parameter is the menu to delete the item from. The second is the menu ID of the item to be removed. The last is a flag, set to MF_BYCOMMAND, indicating that the second parameter is the actual ID Of the item to be removed.

If the item to be removed is a submenu, then the GetSubMenu function can be used:

  • HMENU GetSubMenu(HMENU, UINT);

Here, however, the last parameter is index position of submenu, rather than the ID.

To create menu items to be added, the MENUITEMINFO structure must first be populated.

The MENUITEMINFO Win32 API Structure

To set up one that handles simple text menus, the following mix of parameters can be used:

  • UINT cbSize - size, in bytes, obtained with sizeof() function;
  • UINT fMask - members to set, for text we use MIIM_TYPE|MIIM_ID|MIIM_DATA but could also add MIIM_SUBMENU to add submenus;
  • UINT fType - menu item type, there are many options, most common are MFT_STRING and MFT_SEPARATOR;
  • UINT fState - can be NULL, unless MIIM_STATE used in the fMask member;
  • UINT wID - the ID of the item;
  • HMENU hSubMenu - handle to a submenu (MIIM_SUBMENU checked) or NULL;
  • HBITMAP hbmpChecked - can be NULL;
  • HBITMAP hbmpUnchecked - can be NULL;
  • DWORD dwItemData - can be NULL;
  • LPTSTR dwTypeData - a string containing the menu text;
  • UINT cch - the length of the string in dwTypeData.

Other uses can be to set the hbmpChecked and hbmpUnchecked, for graphical menus, as well as assigning submenus using the hSubMenu option. This needs to be set to a previously created menu created using CreateMenu function.

To populate a blank menu the InsertMenuItem function is used.

The InsertMenuItem Win32 API Function

This is used to insert a menu item in an existing menu, and is necessary to populate the menu with items. The function looks like:

  • BOOL InsertMenuItem(HMENU, UINT, BOOL, LPMENUITEMINFO)

The first parameter is a handle to an existing menu. The second is either the ID or position at which the new menu item is to be inserted. In most cases, the ID will be used, so the third parameter can be set to FALSE, indicating that it is not assigned by position. The last parameter is pointer to structure defined in section above.

With all these functions, the reader can create their own dynamic menu system, or create a static menu without recourse to resource files.

The copyright of the article Win32 CreateMenu API Function in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Win32 CreateMenu API Function in print or online must be granted by the author in writing.
What do you think about this article?

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
post your comment
What is 2+4?

Comments

Nov 24, 2008 4:35 PM
Guest :
My dynamic context menu text is not visible on Vista. Any ideas?
1 Comment: