I use custom base dialog classes in many of my applications. It's not really difficult to do (at least in Visual Studio 6) but does require some attention to detail.
First create your base class from a a blank dialog template. MFC always adds an enum member to the dialog header like this:
enum { IDD = IDD_BASE_DIALOG };
Just delete this line as your base class will not have a fixed IDD (and you can delete the dialog template too). When you delete it you will find that your constructor will fail since IDD is undefined.
Code:
CBaseDlg::CBaseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBaseDlg::IDD, pParent)
{
}
That's OK. Just change it to this:
Code:
CBaseDlg::CBaseDlg(UINT idd, CWnd* pParent /*=NULL*/)
: CDialog(idd, pParent)
{
}
Of course, you will have to change the declaration in the header file too. But you should also add a default constructor that does nothing in the private section so that it will force you to pass the real IDD from the derived class.
In other words, in your header file:
Code:
private:
CBaseDlg::CBaseDlg() : CDialog((UINT)0, (CWnd*)0)
{
ASSERT(FALSE); // No default constructor for base dialog
}
public:
// Derived dialogs are required to call this constructor:
CBaseDlg::CBaseDlg(UINT idd, CWnd* pParent /*=NULL*/);
Now create a derived dialog like you usually do by designing a template and creating the files by deriving from CDialog. Go into the header file and add an include at the top for your base dialog.
Change all instances of CDialog in the header file to CBaseDlg. Most likely will be only one - at the top of the class definition.
Do the same thing in the C++ file EXCEPT for any instance enclosed in an AFX MACRO - leave those as CDialog.
That's about all there is to it. There are some advanced issues to be aware of when using message maps, etc. but what I have outlined above will get you started.
And if anyone knows of an easier way to do this I'd like to hear it.
Bookmarks