简体   繁体   English

MFC:Emdedded子对话框未显示在父对话框中

[英]MFC: Emdedded child dialog is not showing up within parent dialog

I came across a tutorial showing how to embed a child dialog within a parent dialog using MFC. 我遇到了一个教程,展示了如何使用MFC在父对话框中嵌入子对话框。 I am using Visual Studio 2015. My setup is as follows. 我正在使用Visual Studio 2015.我的设置如下。 Using the Visual Studio MFC Application Wizard to create a new MFC Visual C++ Project called MFCApplication3 , I select a Dialog based application where MFC is used in a Shared DLL . 使用Visual Studio MFC应用程序向导创建一个名为MFCApplication3的新MFC Visual C ++项目,我选择一个基于对话框的应用程序,其中MFC用于共享DLL Using boilerplate code, I have a simple Thick Frame Dialog, no maximize or minimize box. 使用样板代码,我有一个简单的厚框对话框,没有最大化或最小化框。

In my resource view, I go to my Dialog editor to edit the main dialog. 在我的资源视图中,我转到Dialog编辑器编辑主对话框。 I add a picture control with a blank area in the center and name it IDC_STATIC . 我在中间添加了一个带有空白区域的图片控件,并将其命名为IDC_STATIC This will simply be used as a placeholder for my child dialog that I wish to embed. 这将仅用作我希望嵌入的子对话框的占位符。 It looks like: 看起来像:

带占位符的MFC基本对话框

Still in the resource view, I create a new Dialog. 仍然在资源视图中,我创建了一个新的Dialog。 I call it IDD_CHILD. 我称之为IDD_CHILD。 I add some components. 我添加了一些组件。 It looks like this: 它看起来像这样:

MFC对话框没有边框

Now back in the Solution Explorer, I add a class using the Add Class wizard, selecting to add an MFC Class. 现在回到解决方案资源管理器中,我使用Add Class向导添加一个类,选择添加MFC类。 The class name is CChildDialog , with a base class of CDialog , and I use the already generated IDD_CHILD as the Dialog ID. 类名是CChildDialog ,基类为CDialog ,我使用已生成的IDD_CHILD作为Dialog ID。 It generates the .cpp and associated .h file. 它生成.cpp和相关的.h文件。 In the constructor of CChildDialog , I add a call to the Create function so the constructor becomes: CChildDialog的构造函数中,我添加了对Create函数的调用,因此构造函数变为:

CChildDialog::CChildDialog(CWnd* pParent /*=NULL*/)
    : CDialog(IDD_CHILD, pParent)
{
    Create(IDD_CHILD, pParent);
}

Now I modify the dialog code generated automatically when I created the project. 现在我修改了创建项目时自动生成的对话框代码。 In CMFCApplication3Dlg.h , I add a private member of type CChildDialog* called m_childDlg , and #include the associated header file. CMFCApplication3Dlg.h ,我添加一个名为m_childDlg CChildDialog*类型的private成员,并#include相关的头文件。 In CMFCApplication3Dlg.cpp , I add this to the OnInitDialog function prior to the return statement: CMFCApplication3Dlg.cpp ,我将它添加到return语句之前的OnInitDialog函数中:

CRect rc;
GetDlgItem(IDC_STATIC)->GetWindowRect(rc);
ScreenToClient(&rc);

m_childDlg = new CChildDialog(this);
m_childDlg->MoveWindow(rc);

Now I build the solution, run it, but it looks like it does in the first picture. 现在我构建解决方案,运行它,但它看起来像在第一张图片中。 A blank placeholder spot for a child dialog, but no child dialog. 子对话框的空白占位符点,但没有子对话框。 What could I be doing wrong? 我能做错什么?

It turns out (while composing this question) that the answer to my problem was two properties I need to set while in the resource view. 事实证明(在编写这个问题的时候)我的问题的答案是我需要在资源视图中设置的两个属性。 When I have the child dialog open ( IDD_CHILD ), within the properties pane, I need to set the following properties: 当我打开子对话框( IDD_CHILD )时,在属性窗格中,我需要设置以下属性:

  • Style : Child 风格 :孩子
  • Visible : TRUE 可见 :是的

(I am not sure why Visible defaults to FALSE in this case). (我不确定为什么Visible在这种情况下默认为FALSE)。 Making those two changes, voila! 做出这两个改变,瞧! I get my embedded dialog: 我得到了嵌入式对话框:

带子对话框的MFC对话框

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

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