3
votes

I came across a tutorial showing how to embed a child dialog within a parent dialog using MFC. I am using Visual Studio 2015. My setup is as follows. 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. 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. I add a picture control with a blank area in the center and name it IDC_STATIC. This will simply be used as a placeholder for my child dialog that I wish to embed. It looks like:

MFC Basic Dialog with Placeholder

Still in the resource view, I create a new Dialog. I call it IDD_CHILD. I add some components. It looks like this:

MFC Dialog no border

Now back in the Solution Explorer, I add a class using the Add Class wizard, selecting to add an MFC Class. The class name is CChildDialog, with a base class of CDialog, and I use the already generated IDD_CHILD as the Dialog ID. It generates the .cpp and associated .h file. In the constructor of CChildDialog, I add a call to the Create function so the constructor becomes:

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. In CMFCApplication3Dlg.cpp, I add this to the OnInitDialog function prior to the return statement:

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?

1

1 Answers

5
votes

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:

  • Style: Child
  • Visible: TRUE

(I am not sure why Visible defaults to FALSE in this case). Making those two changes, voila! I get my embedded dialog:

MFC Dialog with Child Dialog