Modal dialog boxes require that the user dismiss the dialog box before going to another application window. The simplest example of a modal dialog box is an About dialog box.
·To create an About dialog box:
1.Define a dialog resource template for your dialog box and then create a CModalDialog window to use that template. You do not need to derive your own dialog window class box.
2.Call the DoModal member function to process user input until the user dismisses the dialog.
The following dialog resource template is taken from HELLO.RC:
AboutBox DIALOG 22, 17, 144, 75
STYLE DS_MODALFRAME | WS_CAPTION | WS_SYSMENU
CAPTION "About Hello"
{
CTEXT "Microsoft Windows" -1, 0, 5, 144, 8
CTEXT "Microsoft Foundation Classes" -1, 0, 14, 144, 8
CTEXT "Hello, Windows!" -1, 0, 23, 144, 8
CTEXT "Version 1.0" -1, 0, 36, 144, 8
DEFPUSHBUTTON "OK" IDOK, 56, 56, 32, 14, WS_GROUP
}
The following code is taken from HELLO.CPP. It shows how the main window message-handler function for the About menu command creates a CModalDialog window based on the dialog template shown above and then calls DoModal for the dialog.
The arguments to the constructor for a CModalDialog window are the name of the dialog template and a pointer to a CWnd object which is the parent window of the dialog box window. In the following example, the dialog can pass this as the parent window pointer since the dialog is being created in a member function of CMainWindow and the main window is the parent window of the dialog box:
void CMainWindow::OnAbout()
{
CModalDialog about( "AboutBox", this );
about.DoModal();
}
·To initialize the modal dialog box before it is displayed:
1.Derive a dialog class from CModalDialog.
2.Override the virtual OnInitDialog function. OnInitDialog is called when the dialog receives the WM_INITDIALOG message.
An example of a situation in which you would need to do this would be calculating available memory for an About dialog box.
Note:
Because OnInitDialog is so commonly overridden by derived dialog classes, it is a virtual function that is automatically called by the base dialog class when the dialog receives a WM_INITDIALOG message. OnInitDialog is not called through the normal Microsoft Foundation message map mechanism, so you are not required to have to create a message-map entry for the WM_INITDIALOG message.
The following class declaration shows how you might derive from CModalDialog to handle the WM_INITDIALOG message:
class CMyModalDlg : public CModalDialog
{
// override OnInitDialog
virtual BOOL OnInitDialog();
};
BOOL CMyModalDialog::OnInitDialog()
{
//...
}
·To customize the response of the buttons in a CModalDialog object:
1.Override the virtual CModalDialog functions OnOK and OnCancel to handle mouse clicks in the OK and Cancel buttons of your dialog box.
2.Use the predefined control IDs IDOK and IDCANCEL for these buttons. These two control IDs are special in CModalDialog objects in that they do not require message-map entries. BN_CLICKED events in buttons with these IDs are automatically sent to the OnOK and OnCancel virtual functions.
·To customize the response to other events in your modal dialog box so that it is the same as choosing the OK or Cancel buttons:
Define a message-map entry for that notification event and hook it to the OnOK or OnCancel function, as shown here for a double-click in a list box:
class CMyModalDlg : public CModalDialog
{
// override OnInitDialog
virtual BOOL OnInitDialog();
public:
virtual void OnOK()
{
// do something with list box selection...
// and dismiss dialog
EndDialog( IDOK );
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP( CMyModalDlg,CModalDialog )
// double-click list box aliases for OK
ON_LBN_DBLCLK( ID_TYPEFACE, OnOK )
END_MESSAGE_MAP()
The above declaration includes the DECLARE_MESSAGE_MAP macro which is required to enable message map mechanism for the derived class.
The default implementations of OnOK and OnCancel call EndDialog to dismiss the modal dialog. If you override either of these functions, you should call EndDialog in your overridden versions.