在MFC中覆盖默认的Dialog构造函数

时间:2016-12-03 15:34:14

标签: c++ mfc

我想覆盖 CTestDialog 的默认构造函数,以便我可以在其中传递 CString

如何通过

  

CTestDialog(CString strValue = NULL);

这可能在MFC中还是仅仅是我的想象力?

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();
};

用法:

 CString str = _T("Some Text");

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

更新1

在线:

dlg.DoModal();
  

Debug Assertion失败。将显示Microsoft Visual C ++运行时库对话框。

更新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;
}

2 个答案:

答案 0 :(得分:3)

尝试:

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();
};

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; 
}

注意:

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

所以它会是:

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

您会看到,首先调用您的自定义构造函数。然后将所需的pParent传递给基类构造函数。

答案 1 :(得分:0)

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();
};

和使用中

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