7.3.2 Specifying a Menu for a Specific Window

A window need not use the class menu, since the class menu is simply a default, not a requirement. To use a menu other than the class menu, load the menu you want from your application resources by using the LoadMenu function. This function returns a menu handle. Then, when you call the CreateWindow function to create the window, pass the menu handle as the function's hMenu parameter.

The following example loads and specifies a menu by using the LoadMenu and CreateWindow functions:

HWND hWnd;              /* handle of current window */
HMENU hSampleMenu;      /* menu handle              */
    .
    .
    .

hSampleMenu = LoadMenu(hinst, "SampleMenu");
hWnd = CreateWindow("SampleWindow",
    "SampleWindow",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    (HWND) NULL,
    hSampleMenu,
    hinst,
    (LPSTR) NULL);

In this example, the LoadMenu function loads the menu named SampleMenu. The hinst variable specifies that the resource is to be loaded from the application's resources. LoadMenu then returns a menu handle, which is stored in the hSampleMenu variable.

The application calls the CreateWindow function to create a new window named SampleWindow. Finally, the application passes hSampleMenu, the menu handle that LoadMenu returned, to CreateWindow. This tells Windows to use SampleMenu for the window, instead of using the class menu (if any).