Creating a Dialog Function

A dialog function has the following form:

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

HWND hDlg;

unsigned message;

WORD wParam;

DWORD lParam;

{

switch (message) {

/* Place message cases here */

default:

return FALSE;

}

}

This is basically a window function, except that the DefWindowProc function is not called. Default processing of dialog-box messages is handled internally, so the dialog function must not call the DefWindowProc function.

The dialog function must be defined as a FAR PASCAL procedure, and must have the parameters given here. BOOL is the required return type.

Summary: Dialog functions must respond to messages by returning Boolean values.

Just as it does with window functions, Windows sends messages to a dialog function when it has information to give the function or wants the function to carry out some action. Unlike a window function, a dialog function responds to a message by returning a Boolean value. If the function processes the message, it returns TRUE. Otherwise, it returns FALSE.

In this function, the hDlg variable receives the handle of the dialog box. The other parameters serve the same purpose as in a window function. The switch statement is used as a filter for different messages. Most dialog functions process the WM_INITDIALOG and WM_COMMAND messages, but very little else.

The WM_INITDIALOG message, sent to the dialog box just before it is displayed, gives the dialog function the opportunity to give the input focus to any control in the dialog box. If the function returns TRUE, Windows will set the input focus to the control of its choosing.

The WM_COMMAND message is sent to the dialog function by the controls in the dialog box. If there are controls in the dialog box, they send notification messages when the user carries out some action within them. For example, a dialog function with a push button can check WM_COMMAND messages for the control ID of the push button. The control ID is in the message's wParam parameter. When it finds the ID, the dialog function can carry out the corresponding task.

If you create the dialog box with the WS_SYSMENU style, you should include a WM_COMMAND switch statement for the IDCANCEL control ID which is sent when the user chooses the close option in the dialog-box system menu. The statement should include a call to the EndDialog function.