Now that you've added a command to Generic's menu, you need to be able to respond when the user selects the command. To do this, you need to process the WM_COMMAND message. Windows sends this message to the window function when the user chooses a command from the window's menu. Windows passes the menu ID identifying the command in the wParam parameter, so you can check to see 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 the wParam parameter. As your application's message-processing becomes more complex, you may want to use a switch statement instead.) You want to display the dialog box if the parameter is equal to IDM_ABOUT, the About command's menu ID. For any other value, you must pass the message on to the DefWindowProc function. If you do not, you effectively disable all other commands on the menu.
The WM_COMMAND case should look like this:
FARPROC lpProcAbout;
.
.
.
case WM_COMMAND: /* message: command from a menu */
if (wParam == IDM_ABOUT) {
1 lpProcAbout = MakeProcInstance(About, hInst);
2 DialogBox(hInst, /* current instance */
“AboutBox”, /* resource to use */
hWnd, /* parent handle */
lpProcAbout); /* About() inst. address */
3 FreeProcInstance(lpProcAbout);
break;
}
else /* Let Windows process it */
return (DefWindowProc(hWnd, message, wParam, lParam));
1 | Before displaying the dialog box, you need the procedure-instance address of the dialog function. You create the procedure-instance address by using the MakeProcInstance function. This function binds the data segment of the current application instance to a function pointer. This guarantees that when Windows calls the dialog function, the dialog function will use 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. | |
2 | The DialogBox function creates and displays the dialog box. It requires the current application's instance handle 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 function. |
DialogBox does not return control until the user has closed the dialog box. Typically, the dialog box contains at least a push-button control to permit the user to close the box. | |
3 | When the DialogBox function returns, the procedure-instance address of the dialog function is no longer needed, so the FreeProcInstance function frees the address. This invalidates the content of the pointer variable, making it an error to attempt to use the value again. |