Processing Input from a Menu

When a user chooses a command in a menu, Windows sends a WM_COMMAND message to the corresponding window function. The message contains the menu ID of the command in its wParam parameter.

Summary: Menu commands are normally processed in the window function.

The window function is responsible for carrying out any tasks associated with the selected command. For example, if the user chooses the Open command, the window function prompts for the filename, opens the file, and displays the file in the window's client area.

The most common way to process menu input is with a switch statement in the window function. Usually, the switch statement directs processing according to the value of the wParam parameter of the WM_COMMAND message. Each case processes a different menu ID.

For example:

case WM_COMMAND:

1 switch(wParam)

{

2 case IDM_NEW:

/* perform operations for creating a new file */

break;

case IDM_OPEN:

/* perform operations for opening a file */

break;

case IDM_SAVE:

/* perform operations for saving this file */

break;

case IDM_SAVEAS:

/* perform operations for saving this file */

break;

case IDM_EXIT:

/* perform operations for exiting the application */

break;

}

break;

In this example:

1 The wParam parameter contains the menu ID of the item the user just selected.
2 For each menu ID (menu item), the application performs the appropriate operations.