Win32 CreateMenu API FunctionHow to Create Menus Correctly using Windows Functions from Win32 APISep 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:
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 CreateMenuThe function definition is as follows:
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:
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:
Obviously, to use the above, it is necessary to have obtained a valid menu handle. This is done with GetMenu:
If individual items need to be removed, this can be achieved with the DeleteMenu function:
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:
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 StructureTo set up one that handles simple text menus, the following mix of parameters can be used:
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 FunctionThis is used to insert a menu item in an existing menu, and is necessary to populate the menu with items. The function looks like:
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.
CommentsNov 24, 2008 4:35 PM
Guest :
1 Comment:
Related Articles
Related Topics
Reference
More in Technology
|