Deriving from CDialog

Unlike CModalDialog, where you have the choice of whether or not to derive your own dialog window class box, you must derive your dialog from the CDialog class.

·To derive a dialog class from CDialog:

1.Define a contructor for the derived class and optionally define message-handler functions and a message map. This is essentially the same technique as that described for the standard frame window.

A typical constructor simply calls the Create function and supplies the name of the dialog template and the parent window that owns the dialog.

2.Optionally, override the virtual function OnInitDialog to perform any necessary dialog initialization before the dialog is displayed. You do not have to define a message-map entry for the WM_INITDIALOG message for classes derived from CDialog. The CDialog class automatically calls the OnInitDialog member function when the dialog receives a WM_INITDIALOG message.

The advantage of a dialog derived from CDialog, as opposed to CModalDialog, is that it can be modeless. That is, the user can switch back and forth from the modeless dialog to other application windows.

More on Dialog Boxes

If you want an even more elaborate modal dialog box where the user can interact with several controls, you can add message-handler functions and message-map entries to handle notification events from the dialog box controls, as described in “Notification Messages from Child Windows” on page 316. If any of the controls other than the OK and Cancel buttons allow the user to close the dialog, you can call EndDialog in the message-handler functions for those control notifications. See the example program code in SHOWFONT.CPP for examples of derived modal dialog boxes that handle notification messages from child controls.

·To derive a simple class from CDialog:

Use the following class declaration as a model:

class CMyDialog : public CDialog

{

public:

// constructor calls Create

CMyDialog( CWnd* parentWnd )

{

Create( "MyTemplateName", parentWnd );

}

virtual BOOL OnInitDialog();

};

Beyond these two basic tasks, you will typically define message-handler functions and message-map entries to handle notification messages from other controls in your dialog. This process is described in “Notification Messages from Child Windows” on page 316.

Type-Safe Dialog Item Access

One optional technique that you might find useful in dialog boxes is to define type-safe member functions to access individual controls in the dialog box. For example, if your dialog contains a button control with the ID number ID_MYBUTTON, you can define the following function in your derived dialog class:

CButton* CMyDialog::GetMyButton() { return (CButton*)GetDlgItem( ID_MYBUTTON ); }

Once this function is defined, you can access the button with code similar to the following:

myDlg.GetMyButton()->SetState( TRUE );

See the sample program RESTOOL.