Creating a Dialog Function

A “dialog box” is a special kind of window whose window procedure is built into Windows. For every dialog box an application has, the application must have a dialog function. The built-in window procedure for Windows calls a dialog function to handle input messages that can be interpreted only by the application.

The function that processes input for Generic's About dialog box is called About. This function, like other dialog functions, uses the same parameters as a window function, but processes only messages that are not handled by Windows' default processing. (The dialog function returns TRUE if it processes a message, and FALSE if it does not.) The dialog function, like the window function, uses the PASCAL calling convention and the FAR keyword in its definition. You must name the dialog function in an EXPORTS statement in the application's module-definition file. As with a window function, you must not call a dialog function directly from your application.

Unlike a window function, a dialog function usually processes only user-input messages, such as WM_COMMAND, and must not send unprocessed messages to the DefWindowProc function. Generic's dialog function, About, looks like this:

BOOL FAR PASCAL About(hDlg, message, wParam, lParam)

HWND hDlg; /* window handle of the dialog box */

unsigned message; /* type of message */

WORD wParam; /* message-specific information */

LONG lParam;

{

switch (message) {

case WM_INITDIALOG:/* message: initialize dialog box */

return (TRUE);

case WM_COMMAND: /* message: received a command */

if (wParam == IDOK|| wParam == IDCANCEL) {

EndDialog(hDlg, TRUE); /* Exits the dialog box */

return (TRUE);

}

break;

}

return (FALSE);/* Didn't process a message */

}

The About dialog function processes two messages: WM_INITDIALOG and WM_COMMAND. Windows sends the WM_INITDIALOG message to a dialog function to let the function prepare before displaying the dialog box. In this case, WM_INITDIALOG returns TRUE so that the “focus” will be passed to the first control in the dialog box that has the WS_TABSTOP bit set (this control will be the default push button). If WM_INITDIALOG had returned FALSE, then Windows will not set the focus to any control.

In contrast to WM_INITDIALOG messages, WM_COMMAND messages are a result of user input. About responds to input to the OK button or the system-menu Close command by calling the EndDialog function, which directs Windows to remove the dialog box and continue execution of the application. The EndDialog function is used to terminate dialog boxes.