2.4.5 Processing a WM_COMMAND Message

Now that you have added a menu item to Generic's menu, you will want the application to be able to respond when the user chooses it as a command. To respond, the application must process a WM_COMMAND message. Windows sends this message to the window procedure when the user chooses a command from the window's menu. Windows passes the menu identifier of the command in the wParam parameter, so you can check which command was chosen. (In this case, you can use if and else statements to direct the flow of control, depending on the value of wParam. As your application's message processing becomes more complex, you may want to use a switch statement instead.) The goal is to have the application display the dialog box if the parameter is equal to IDM_ABOUT, the About command's menu identifier. For any other value, the application must pass the message on to the DefWindowProc function. If it does not, all other commands on the menu are effectively disabled.

The WM_COMMAND case should look like this:

FARPROC lpProcAbout;       /* pointer to the "About" function */
    .
    .
    .

case WM_COMMAND:              /* message: command from a menu */
    if (wParam == IDM_ABOUT) {
        lpProcAbout = MakeProcInstance((FARPROC) About, hInst);

       



        DialogBox(hInst,            /* current instance       */
            "AboutBox",             /* resource to use        */
            hWnd,                   /* parent handle          */
            (DLGPROC) lpProcAbout); /* About instance address */

        FreeProcInstance(lpProcAbout);
        break;
    }

    else                            /* let Windows process it */
        return (DefWindowProc(hWnd, message, wParam, lParam));

Before it can display the dialog box, your application must have the procedure-instance address of the dialog box procedure. You create this address by using the MakeProcInstance function, which binds the data segment of the current application instance to a pointer. This guarantees that when Windows calls the dialog box procedure, the procedure uses the data in the current instance and not some other instance of the application. MakeProcInstance returns the address of the procedure instance. This value should be assigned to a pointer variable that has the FARPROC type.

The DialogBox function creates and displays the dialog box. It requires the instance handle of the current application and the name of the dialog box template. It uses this information to load the dialog box template from the executable file. DialogBox also requires the handle of the parent window (the window to which the dialog box belongs) and the procedure-instance address of the dialog box procedure. DialogBox does not return control until the user has closed the dialog box. Typically, the dialog box contains at least a push button to permit the user to close the box.

When the DialogBox function returns, the procedure-instance address of the dialog box procedure is no longer needed, so the FreeProcInstance function frees the address. This invalidates the content of the pointer variable; an error results if the application attempts to use the value again.