views:

627

answers:

3

Hi all,

I have created a dialog box (cMyDialog). I am planning to duplicate cMyDialog and called it cMyDialog2. Can I know in MFC, how can I do inheritance? I want the cMyDialog2 to inherit all the IDDs from cMyDialog1 so that I do not have to copy and paste the codes from cMyDialog1 and cMyDialog2. The purpose of cMyDialog2 is to inherit all the functions from cMyDialog1 and to add some extra functions in it.

thank you very much in advance for your help and advices

+2  A: 

Yes you can inherit from a CDialog-derived class. You just need to add a few macros like DECLARE_DYNAMIC and a few others to satisfy MFC. Here is an example. You can use this as a starting point:

In the .h file:

class cMyDialog2
  : public cMyDialog
{
  DECLARE_DYNAMIC(cMyDialog2)

pulic:
  cMyDialog2();
  virtual ~cMyDialog2();

protected:
  DECLARE_MESSAGE_MAP()
};

In the .cpp file:

#include "cMyDialog2.h"

IMPLEMENT_DYNAMIC(cMyDialog2, cMyDialog)

BEGIN_MESSAGE_MAP(cMyDialog2, cMyDialog)
END_MESSAGE_MAP()

cMyDialog2::cMyDialog2()
{
}

...etc.
Adam Pierce
A: 

Hi Adam,

Thank you very much for your reply. I am not quite sure about the IMPLEMENT_DYNAMIC.Below are a short snippet of my codes. Can you please help to advice if I misunderstood the macro wrongly?

// cMyDialog1.cpp : implementation file

cMyDialog1::cMyDialog1(void * pMsgData, CWnd* pParent /=NULL/): CDialog(cMyDialog1::IDD, pParent)

{ //codes.... }

BOOL cMyDialog1::OnInitDialog()

{ CDialog::OnInitDialog(); ... }

//cMyDialog2.cpp

cMyDialog2::cMyDialog2(void * pMsgData, CWnd* pParent /=NULL/) : CMyDialog1(cMyDialog2::IDD, pParent)

{ //codes.... IMPLEMENT_DYNAMIC(cMyDialog2, cMyDialog1) }

Really appreciate the head start that you had given me.

Thanks, peace

peace
You should put IMPLEMENT_DYNAMIC outside your constructor. Also cMyDialog1 and cMyDialog2 should be in separate files. Otherwise, it looks fine.
Adam Pierce
Hi Adam, I tried to compile it (after taking out the IMPLEMENT_DYNAMIC from the constructor), I received an error that said error C2309:class 'classcMyDialog1' : is not a member of 'cMyDialog1'.... see declaration of 'cMyDialog1'Can you please help to advice in this? Thanks..
peace
I forgot to mention that cMyDialog1 and cMyDialog2 are in separate filfes
peace
That error sounds like you have a curly-brace in the wrong place or your constructor definition does not match the declaration or something similarly simple.
Adam Pierce
I am sorry for bothering you so much. I tried to debug but was unable to find the cause. Besides, I dont quite understand the DECLARE_DYNAMIC macro. I searched online for more info on that but am still in the dark. Can I know if you have any examples showing inheritance from a CDialog-derived class?
peace
A: 

Hi All,

I am able to inherit from CMyDialog via the DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC method. Thanks a lot for your help, Adam.

But I was unable to get the second part of my question to work. I wanted to add some extra function in the child dialog box, CMyDialog1 such as adding a Save As button but I was unable to do it. Is it because CMyDialog1 is an inherited dialog from CMyDialog and hence, I could not add in new functions? Can you all please advice me how to add new functions in the inherited dialog box?

thanks a lot for your help.

peace