简体   繁体   English

如何继承一个MFC对话框?

[英]How can I inherit an MFC dialog box?

I have created a dialog box (cMyDialog).我创建了一个对话框 (cMyDialog)。 I am planning to duplicate cMyDialog and call it cMyDialog2.我打算复制 cMyDialog 并将其命名为 cMyDialog2。 How can I do inheritance in MFC? MFC中的inheritance怎么办? I want cMyDialog2 to inherit all the IDDs from cMyDialog1 so that I do not have to copy and paste the code from cMyDialog1 to cMyDialog2.我希望 cMyDialog2 继承 cMyDialog1 的所有 IDD,这样我就不必将代码从 cMyDialog1 复制并粘贴到 cMyDialog2。 The purpose of cMyDialog2 is to inherit all the functions from cMyDialog1 and to add some extra functions in it. cMyDialog2 的目的是继承cMyDialog1 的所有功能,并在其中添加一些额外的功能。


Thank you very much for your reply.非常感谢您的回复。 I am not quite sure about the IMPLEMENT_DYNAMIC.我不太确定 IMPLEMENT_DYNAMIC。 Below is a short snippet of my code.下面是我的代码的一小段。 Can you please review it and help me if I misunderstood the macro?如果我误解了宏,你能复查一下并帮助我吗?

// 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)
}

I am able to inherit from CMyDialog via the DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC method.我可以通过DECLARE_DYNAMICIMPLEMENT_DYNAMIC方法从 CMyDialog 继承。 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 functions in the child dialog box, CMyDialog1, such as adding a "Save As" button, but I was unable to do it.我想在子对话框 CMyDialog1 中添加一些额外的功能,例如添加一个“另存为”按钮,但我无法做到。 Is it because CMyDialog1 is an inherited dialog from CMyDialog and hence, I can't add in new functions?是否因为 CMyDialog1 是 CMyDialog 的继承对话框,因此我无法添加新功能? How can I add new functions in the inherited dialog box?如何在继承对话框中添加新功能?

Yes you can inherit from a CDialog-derived class. 是的,您可以从CDialog派生类继承。 You just need to add a few macros like DECLARE_DYNAMIC and a few others to satisfy MFC. 你只需要添加一些像DECLARE_DYNAMIC和其他一些宏来满足MFC。 Here is an example. 这是一个例子。 You can use this as a starting point: 您可以将此作为起点:

In the .h file: 在.h文件中:

class cMyDialog2
  : public cMyDialog
{
  DECLARE_DYNAMIC(cMyDialog2)

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

protected:
  DECLARE_MESSAGE_MAP()
};

In the .cpp file: 在.cpp文件中:

#include "cMyDialog2.h"

IMPLEMENT_DYNAMIC(cMyDialog2, cMyDialog)

BEGIN_MESSAGE_MAP(cMyDialog2, cMyDialog)
END_MESSAGE_MAP()

cMyDialog2::cMyDialog2()
{
}

...etc.

This maybe considered as addendum to Adam Piece answer. 这可能被视为Adam Piece答案的附录。 It is also important to understand the role of DoDataExchange() when deriving from another dialog. 在从另一个对话框派生时理解DoDataExchange()的作用也很重要。 Either the derived class (cMyDialog2) shall not implement this function or if it is implemented (recommended) it should call base version of it like below: 派生类(cMyDialog2)不应该实现这个函数,或者如果它被实现(推荐)它应该调用它的基本版本,如下所示:

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

If this is not done correctly, the controls on the dialog will not be created and the dialog may crash as a result when invoked/executed. 如果未正确完成,则不会创建对话框上的控件,并且在调用/执行时对话框可能会因此而崩溃。

I have tried to use this code and it's working properly for me.

`GetClientRect(&rect); `GetClientRect(&rect); int x = rect.right-rect.left; int x = rect.right-rect.left; int y = rect.bottom-rect.top; int y = rect.bottom-rect.top;

cout << x << endl;
cout << y << endl;

int newSizex = x;
int newSizey = y;

SetWindowPos(NULL, x, y, x - 30, y - 20, SWP_NOMOVE);`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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