Most Windows applications have only one menu in the resource script. The program makes reference to this menu in the definition of the window class:
wndclass.lpszMenuName = "MyMenu" ;
Programmers often use the name of the program as the name of the menu so that the same text string can also be used for the window class, the name of the program's icon, and the name of the menu. However, you can also use a number (or a macro identifier) for the menu rather than a name. The resource script would look like this:
45 MENU
{
[menu definition]
}
In this case, the assignment statement for the lpszMenuName field of the window class structure can be either:
wndclass.lpszMenuName = MAKEINTRESOURCE (45) ;
or:
wndclass.lpszMenuName = "#45" ;
Although specifying the menu in the window class is the most common way to reference a menu resource, you have alternatives. A Windows application can load a menu resource into memory with the LoadMenu function, which is similar to the LoadIcon and LoadCursor functions described in Chapter 8. If you use a name for the menu in the resource script, LoadMenu returns a handle to the menu:
hMenu = LoadMenu (hInstance, "MyMenu") ;
If you use a number, the LoadMenu call takes either this form:
hMenu = LoadMenu (hInstance, MAKEINTRESOURCE (45)) ;
or this form:
hMenu = LoadMenu (hInstance, "#45") ;
You can then specify this menu handle as the ninth parameter to CreateWindow:
hwnd = CreateWindow ("MyClass", "Window Caption",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,
hMenu,
hInstance,
NULL) ;
In this case, the menu specified in the CreateWindow call overrides any menu specified in the window class. You can think of the menu in the window class as being a default menu for the windows based on the window class if the ninth parameter to CreateWindow is NULL. Therefore, you can use different menus for several windows based on the same window class.
You can also have a NULL menu in the window class and a NULL menu in the CreateWindow call and assign a menu to a window after the window has been created:
SetMenu (hwnd, hMenu) ;
This form lets you dynamically change a window's menu. We'll see an example of this in the NOPOPUPS program shown later in this chapter.