Specifying a Menu for a Specific Window

A window need not use the class menu; the class menu is simply a default, not a requirement. To use a menu other than the class menu, specify the menu you want when you create the window.

To specify a menu when creating a window:

1.Load the menu from your application resources using the LoadMenu function. This function returns a menu handle.

2.When you call CreateWindow to create the window, pass the menu handle as the function's hMenu parameter.

The following example shows how to load and specify a menu by using CreateWindow:

HWND hWnd; /* Initialize a variable to hold the

handle to the current window*/

HMENU hSampleMenu;/* Initialize a variable to hold the

handle to the menu */

.

.

.

1 hSampleMenu = LoadMenu (hInstance, “SampleMenu”);

2 hWnd = CreateWindow ("SampleWindow",

“SampleWindow”,

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

(HWND) NULL,

3 hSampleMenu,

hInstance,

(LPSTR) NULL );

In this example:

1 The LoadMenu function loads the menu named SampleMenu. The hInstance variable specifies that the resource is to be loaded from the application's resources. LoadMenu returns a menu handle, which is stored in the hSampleMenu variable.
2 The application then calls CreateWindow to create a new window named SampleWindow.
3 The application passes hSampleMenu, the menu handle that LoadMenu returned, to the CreateWindow function. This tells Windows to use SampleMenu for this window, instead of the class menu (if any).