简体   繁体   中英

Override default Constructor of Dialog in MFC

I want to override the default constructor of CTestDialog so that I can pass CString in it.

How do I pass

CTestDialog(CString strValue = NULL);

Is this possible in MFC or is it just my imagination?

class CTestDialog : public CDialog
{
    DECLARE_DYNAMIC(CTestDialog)

public:
    CTestDialog(CWnd* pParent = NULL);   // standard constructor
    CTestDialog(CString strValue = NULL); // Custom Constructor
    virtual ~CTestDialog();

    // Dialog Data
#ifdef AFX_DESIGN_TIME
    enum { IDD = IDD_TESTDIALOG };
#endif

protected:
    CString _filename;
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    DECLARE_MESSAGE_MAP()
public:
    virtual BOOL OnInitDialog();
};

Usage:

 CString str = _T("Some Text");

 CTestDialog dlg(str);
 dlg.doModal();

UPDATE 1

On line:

dlg.DoModal();

Debug Assertion Failed. Microsoft Visual C++ Runtime Library Dialog is displayed.

UPDATE 2

IMPLEMENT_DYNAMIC(CTestDialog, CDialog)

CTestDialog::CTestDialog(CWnd* pParent /*=NULL*/)
    : CDialog(IDD_TESTDIALOG, pParent)
{

}

CTestDialog::CTestDialog(CString str)
    : CDialog(CTestDialog::IDD, NULL)
{
    _filename = str;
}

CTestDialog::~CTestDialog()
{
}

void CTestDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CTestDialog, CDialog)
END_MESSAGE_MAP()

BOOL CTestDialog::OnInitDialog()
{
    CDialog::OnInitDialog();

    // TODO:  Add extra initialization here
    AfxMessageBox(_filename.GetBuffer());
    return TRUE;
}

Try:

class CTestDialog : public CDialog
{
    DECLARE_DYNAMIC(CTestDialog)

   public:
      CTestDialog(CWnd* pParent = NULL);   // standard constructor

      CTestDialog(CWnd* pParent, CString strValue); // Custom Constructor

      virtual ~CTestDialog();

   // Dialog Data
   #ifdef AFX_DESIGN_TIME
        enum { IDD = IDD_TESTDIALOG };
   #endif

   protected:
      CString _filename; 
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

      DECLARE_MESSAGE_MAP()
   public:
      virtual BOOL OnInitDialog();
};

And:

IMPLEMENT_DYNAMIC(CTestDialog, CDialog)

CTestDialog::CTestDialog(CWnd* pParent /*=NULL*/)
    : CDialog(IDD_TESTDIALOG, pParent)
{

}

CTestDialog::CTestDialog(CWnd* pParent, CString str)
    : CDialog(IDD_TESTDIALOG, pParent)
{
     _filename = str;
}

CTestDialog::~CTestDialog()
{
}

void CTestDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CTestDialog, CDialog)
END_MESSAGE_MAP()

BOOL CTestDialog::OnInitDialog()
{
    CDialog::OnInitDialog();

    // TODO:  Add extra initialization here
    AfxMessageBox(_filename.GetBuffer());
    return TRUE; 
}

Notice:

CTestDialog::CTestDialog(CWnd* pParent, CString str)
    : CDialog(IDD_TESTDIALOG, pParent)
{
     _filename = str;
}

So it would be:

CTestDialog dlg(NULL, "filename");
dlg.DoModal();

You see, your custom constructor is called first. That then passes the required pParent into the base class constructor.

class CTestDialog : public CDialog
{
    DECLARE_DYNAMIC(CTestDialog)

   public:
      CTestDialog(CWnd* pParent = NULL);   // standard constructor

      CTestDialog(CString strValue); // Custom Constructor

      virtual ~CTestDialog();

   // Dialog Data
   #ifdef AFX_DESIGN_TIME
        enum { IDD = IDD_TESTDIALOG };
   #endif

   protected:

      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

      DECLARE_MESSAGE_MAP()
   public:
      CString _filename; 
      virtual BOOL OnInitDialog();
};

and In usage

 CTestDialog dlg(NULL, stingtopass);
 dlg.doModal();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM