PROGRAMMING FOR A DIALOG

There are two aspects to programming for a dialog: getting it displayed, and handling user interactions with its controls. Before you can display the dialog corresponding to the resource you’ve just created, you must define a dialog class for it. The Class Wizard helps with this.

Adding a Dialog Class

Right-click in the Resource Editor pane for the dialog and then select Add Class from the pop-up to display the Class Wizard dialog. You’ll define a new dialog class derived from the MFC class CDialogEx, so select that class name from the “Base class” drop-down list if it’s not already selected. You can enter the class name as CPenDialog. The Class Wizard dialog should look like Figure 16-2. Click the Finish button to create the new class.

The CDialogEx class is a window class (derived from CWnd) that’s specifically for displaying and managing dialogs. The dialog resource will be automatically associated with a CPenDialog object because the IDD class member is initialized with the dialog resource ID:

class CPenDialog : public CDialogEx
{
  DECLARE_DYNAMIC(CPenDialog)
        
public:
  CPenDialog(CWnd* pParent = NULL);   // standard constructor
  virtual ~CPenDialog();
        
// Dialog Data
  enum { IDD = IDD_PENWIDTH_DLG };
        
protected:
  virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
        
  DECLARE_MESSAGE_MAP()
};

The boldfaced statement defines IDD as a ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.