Re: MFC: A subclass of a subclass of CDialog
On Sun, 30 Dec 2007 20:02:25 -0800, Zapanaz
<http://joecosby.com/code/mail.pl> wrote:
I am building an app with two MFC classes descended from CDialog.
Probably by the end of it, I am going to have 5 or 6 CDialogs (in a
tabbed window). I have spent quite some time adding custom painting
to the first of them. All of them, in the end, are going to have this
same custom painting going on.
The thing to do then would be to make an intermediate class, MyDialog,
descended from CDialog, with the custom painting in the OnPaint()
method, then derive the 5 or 6 dialogs from that, so I am not
repeating and maintaining the custom painting code in all of my
dialogs.
Theoretically this is very simple ... I create a class MyDialog, then
change the parent class of my dialogs from CDialog to MyDialog, then
transfer the custom OnPaint() method from the dialogs to MyDialog, so
the children all inherit it.
But this doesn't seem to work in a project built on MFC. I tried
creating MyDialog by hand. I've been programming in C++ for years,
this part is simple enough. The CDialog constructor stymies me a
little ... the auto-generated version calls the parent with
CDialog(AppDialog1::IDD, pParent), where IDD is an enum generated with
the resource ID of the dialog. If I'm doing it by hand, I don't have
this, so I try the alternate constructor, CDialog(). This compiles,
but I don't know if this is going to be a problem.
You should simply chain to the CDialog(ID, parent) ctor. The intermediate
base class won't have an ID of its own; instead, the derived class will
supply it.
Then I try to change AppDialog1 to subclass from MyDialog. So the
only change is I change the superclass from CDialog to MyDialog.
Now it doesn't compile, I get "error C2143: syntax error : missing ';'
before 'constant'", the error is in the header file at this line:
"class AppDialog1.1 : public MyDialog". I renamed AppDialog1 to
AppDialog1.1 so I could work on it without messing up AppDialog1,
which works ... is an x.x naming convention a problem?
Yes, it's a problem. The dot is not a legal character for identifiers. The
only non-alphanumeric character you can use is the underscore. Just don't
begin identifiers with the underscore or use two of them in a row, because
such names are reserved.
So I have no idea what that means.
So I try creating MyDialog in the visual builder. It gives me a
warning because I am not building it from a visually-created class, so
it won't have an ID. Well I can't see a way around that, so OK.
When I try to compile it, I get an error (more or less predictably)
because in the enum, IDD is defined as "UNKNOWN ID" or something, so
again I change the constructor from CDialog(MyDialog::IDD, pParent) to
CDialog().
Don't do that.
Now it compiles OK. So I try to create a subclass, AppDialog1.1,
which descends from MyDialog
Again I get the same error:
"error C2143: syntax error : missing ';' before 'constant'"
And again I have no idea what exactly that is supposed to mean.
Is this just impossible? Can I not mix my own classes with MFC
classes this way?
Of course you can!
I am wondering if anybody has done something similar. The errors are,
as far as I can guess, happening in macros or something that are
generated by MFC. It is basically impossible to debug these.
Maybe if I build AppDialog1.1 in the visual builder ... but then it
won't allow me to choose a base class of my own, only from the MFC
classes. Another dead end.
I believe you've described the hierarchy:
CDialog <- SubDlgBase <- SubDlg1, SubDlg2, SubDlg3, ...
Like CDialog, the SubDlgBase class will not have an IDD enum member, but
the derived classes, SubDlg1, SubDlg2, etc, will, because they are
associated with dialog resources. Your SubDlgBase class should define a
ctor SubDlgBase(UINT id, CWnd* parent) that chains to the corresponding
CDialog ctor. You should be able to set this up in ClassWizard such that
SubDlgBase and the derived subdialogs are all available there. It's been a
long time since I did this, but IIRC, after creating SubDlgBase, you will
delete its IDD member and the dialog template ClassWizard created for it.
Then you will modify its ctor as I described earlier. What you are trying
to do is definitely possible, and it works with full ClassWizard support
for the created classes. At least that was the case in VC6, which is where
I did it.
P.S. The best group for this question would have been
microsoft.public.vc.mfc.
--
Doug Harrison
Visual C++ MVP