'dlg'에 해당되는 글 1건

  1. 2008.12.07 Nesting Dialogs In MFC

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.

  1. 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.

  2. 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’t IDC_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:

    Nested dialogs parent dialog

    If you do not use a placeholder control, you will have to set the child window position manually.

  3. 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:

    Nested dialogs child dialog
  4. Assuming that you named the placeholder control member variablem_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:

Nested dialogs combined dialog

Using this method, even keyboard navigation works correctly!


Posted by 두장
이전버튼 1 이전버튼