Below is a quick tutorial on how to nest a child dialog inside another using MFC (useful when, say, implementing your own tab-strip-like control). Disclaimer: caveat emptor — this method may be flawed, but it works for me.
-
Using the resource editor, create the parent dialog. As the child dialog will be a child window, be sure to set Control Parent to True.
-
Optional: Create and insert a placeholder control of the desired dimensions for your nested child dialog. I typically use a
CStatic
with a border and caption for reference. Assign it a resource id that isn’tIDC_STATIC
and generate a control member variable because you will need it later.Here is a picture of a sample parent dialog with a placeholder control:
If you do not use a placeholder control, you will have to set the child window position manually.
-
Using the resource editor, create a dialog to act as the child. Make sure that it is no larger than your placeholder control or it will be truncated (unless you handle resizing). Set Style to Child, Control to True, and Border to None. TheControl setting is particularly important for a smooth user experience —Raymond Chen describes what it does in What is the
DS_CONTROL
style for?Here is a picture of a sample child dialog:
-
Assuming that you named the placeholder control member variable
m_staticPlaceholder
, insert the following code snippet into the parent dialog’sOnInitDialog()
function:// Get the static placeholder client rectangle, which we will use // to size the child dialog. CRect rct; m_staticPlaceholder.GetWindowRect(&rct); // In screen coordinates ScreenToClient(&rct); m_staticPlaceholder.ShowWindow(SW_HIDE); CDialog* pchild = new CChildDialog; // Call Create() explicitly to ensure the HWND is created. pchild->Create(CChildDialog::IDD, this); // Set the window position to be where the placeholder was. pchild->SetWindowPos ( NULL, rct.left, rct.top, rct.Width(), rct.Height(), SWP_SHOWWINDOW );
You may want to examine
SetWindowPos()
’s flags to see if there is a more appropriate combination of flags for your usage scenario.
Here is a picture of the result where the child dialog is properly nested inside the parent:
Using this method, even keyboard navigation works correctly!