The OK and Cancel Buttons

ABOUT2 has two push buttons, labeled OK and Cancel. In the dialog box template in ABOUT2.RC, the OK button has an ID of IDOK (defined in WINDOWS.H as 1) and the Cancel button an ID of IDCANCEL (defined in WINDOWS.H as 2). The OK button is the default:

DEFPUSHBUTTON "OK" IDOK, 20, 168, 40, 14, WS_GROUP

PUSHBUTTON "Cancel" IDCANCEL, 80, 168, 40, 14, WS_GROUP

This arrangement is normal for OK and Cancel buttons in dialog boxes; having the OK button as the default helps out with the keyboard interface. Here's how: Normally, you would end the dialog box by clicking one of these buttons with the mouse or pressing the Spacebar when the desired button has the input focus. However, the dialog box window procedure also generates a WM_COMMAND message when the user presses Enter, regardless of which control has the input focus. The value of wParam is set to the ID value of the default push button in the dialog box unless another push button has the input focus. In that case, wParam is set to the ID of the push button with the input focus. If no push button in the dialog box is a default push button, then Windows sends the dialog box procedure a WM_COMMAND message with wParam equal to IDOK. If the user presses the Esc key or Ctrl-Break, Windows sends the dialog box procedure a WM_COMMAND message with wParam equal to IDCANCEL. So you don't have to add separate keyboard logic to the dialog box procedure, because the keystrokes that normally terminate a dialog box are translated by Windows into WM_COMMAND messages for these two push buttons.

The AboutDlgProc function handles these two WM_COMMAND messages by calling EndDialog:

switch (wParam)

{

case IDOK :

nCurrentColor = nColor ;

nCurrentFigure = nFigure ;

EndDialog (hDlg, TRUE) ;

return TRUE ;

case IDCANCEL :

EndDialog (hDlg, FALSE) ;

return TRUE ;

ABOUT2's window procedure uses the global variables nCurrentColor and nCurrentFigure when drawing the rectangle or ellipse in the program's client area. AboutDlgProc uses the static local variables nColor and nFigure when drawing the figure within the dialog box.

Notice the different values in the second parameter of EndDialog. This is the value that is passed back as the return value from the original DialogBox function in WndProc:

case IDM_ABOUT :

if (DialogBox (hInstance, "AboutBox", hwnd, lpfnAboutDlgProc))

InvalidateRect (hwnd, NULL, TRUE) ;

return 0 ;

If DialogBox returns TRUE (nonzero), meaning that the OK button was pressed, then the WndProc client area needs to be updated with the new figure and color. These were saved in the global variables nCurrentColor and nCurrentFigure by AboutDlgProc when it received a WM_COMMAND message with wParam equal to IDOK. If DialogBox returns FALSE, the main window continues to use the original settings of nCurrentColor and nCurrentFigure.

TRUE and FALSE are commonly used in EndDialog calls to signal to the main window procedure whether the user ended the dialog box with OK or Cancel. However, the parameter to EndDialog is actually an int, and DialogBox returns an int, so it's possible to return more information in this way than simply TRUE or FALSE.