47.1.6 Menu Creation

You can use one of the following methods to create a menu for use in your application:

Load a menu from a menu-template resource.

Build a menu from a menu template created at run time.

Use the menu-creation functions to build a menu at run time.

The following three sections describe how to use these methods to create menus.

47.1.6.1 Menu-Template Resources

Most Windows developers implement menus by creating a menu-template resource and loading the resource at run time. A menu template defines a menu, including the items in the menu bar and all pop-up menus.

To use a menu-template resource, you must define a menu template in your application's resource-definition file, then use a resource compiler to create the resource and add it to your application's executable file. At run time, you call the LoadMenu function to load the menu-template resource into memory and retrieve the handle of the menu. Next, you call the SetMenu function to assign the menu to a window.

Implementing menus as resources makes your application easier to localize for use in other countries. With menu resources, localizing the menus only requires that the resource-definition file be edited, recompiled, and the new resources added to your application's executable file. For more information on creating a menu resource, see Section 0.2.1, “Using a Menu-Template Resource.”

47.1.6.2 Menu Template in Memory

You can create a menu from a menu template that you build in memory at run time. To do so, you can fill a MENUITEMTEMPLATEHEADER structure and an array of MENUITEMTEMPLATE structures. The last member of the MENUITEMTEMPLATEHEADER structure specifies the offset of the array of MENUITEMTEMPLATE structures. Each member of the array describes a menu item.

You create a menu from the menu template by passing the address of the MENUITEMTEMPLATEHEADER structure to the LoadMenuIndirect function. LoadMenuIndirect returns the handle of the menu.

47.1.6.3 Menu Creation Functions

Another way to create a menu at run time is to use the CreateMenu and CreatePopupMenu functions to create empty menus, then add menu items to the menus by using the AppendMenu or InsertMenu functions. CreateMenu creates an empty menu bar; CreatePopupMenu creates an empty pop-up menu.