The Dialog Box Procedure

The dialog box procedure within your program handles messages to the dialog box. Although it looks very much like a window procedure, it is not a true window procedure. The window procedure for the dialog box is within Windows. That window procedure calls your dialog box procedure with many of the messages that it receives. Here's the dialog box procedure for ABOUT1:

BOOL FAR PASCAL AboutDlgProc (HWND hDlg, WORD message,

WORD wParam, LONG lParam)

{switch (message)

{case WM_INITDIALOG :

return TRUE ;

case WM_COMMAND :

switch (wParam)

{case IDOK :

case IDCANCEL :

EndDialog (hDlg, 0) ;

return TRUE ;

}break ;

}return FALSE ;

}The parameters to this function are the same as those for a normal window procedure. (Although I've used hDlg for the handle to the dialog box window, you can use hwnd instead if you like.) Let's note first the differences between this function and a window procedure:

A window procedure returns a long; a dialog box procedure returns a BOOL (which is defined in WINDOWS.H as an int).

A window procedure calls DefWindowProc if it does not process a particular message; a dialog box procedure returns TRUE (nonzero) if it processes a message and FALSE (0) if it does not.

A dialog box procedure does not need to process WM_PAINT or WM_DESTROY messages. A dialog box procedure will not receive a WM_CREATE message; instead, the dialog box procedure performs initialization during the special WM_INITDIALOG message.

The WM_INITDIALOG message is the first message the dialog box procedure re- ceives. This message is sent only to dialog box procedures. If the dialog box procedure returns TRUE, then Windows sets the input focus to the first child window control in the dialog box that has a WS_TABSTOP style (which I'll explain in the discussion of ABOUT2). In this dialog box, the first child window control that has a WS_TABSTOP style is the push button. Alternatively, during processing of WM_INITDIALOG the dialog box procedure can use SetFocus to set the focus to one of the child window controls in the dialog box and then return FALSE.

The only other message this dialog box processes is WM_COMMAND. This is the message the push-button control sends to its parent window either when the button is clicked with the mouse or when the Spacebar is pressed while the button has the input focus. The ID of the control (which we set to IDOK in the dialog box template) is in wParam. For this message, the dialog box procedure calls EndDialog, which tells Windows to destroy the dialog box. For all other messages, the dialog box procedure returns FALSE to tell the dialog box window procedure within Windows that our dialog box procedure did not process the message.

The messages for a modal dialog box don't go through your program's message queue, so you needn't worry about the effect of keyboard accelerators within the dialog box.