Question

I want to create 2 dialog windows, one is a child of the other one. Below is part of the code. When I click the menu item "Parent", it created the 2 dialog and set one as a child of the other one, as the picture showed:

enter image description here

The problem is that they are both invisible. They have been sized as indicated by Spy++. Even if I use ShowWindow and UpdateWindow, it doesn't help.

Here is my first queston:

How to fix this?

Now the reason I want do create child dialog windows is: Look at the following picture,

enter image description here

it has a Preference dialog which contains child dialogs (the bounded rectangle by black line). These child dialog windows are used to group related options. So if uses choose an item in the left side TreeView, it hides a child dialog window and show another child dialog window to provide different options.

Now the second question:

Is my code is the correct way to achieve such work?


Update 2:

Here is the solution!

http://msdn.microsoft.com/en-us/library/windows/desktop/ms644996%28v=vs.85%29.aspx


Update:

Thank you guys.

After using CreateDialog (for child dialog) instead DialogBox(...), using DS_CONTROL instead WS_CHILD, and remove OK button (and its corresponding wndproc) from child dialog, I got

enter image description here

I can even move the child dialog inside parent dialog. Remove CAPTION "Child Dialog" in the resource script file doesn't help.

  1. How (what flags should be used) to make Child Dialog fits in Parent Dialog seamlessly,? This means that : No title bar, no border...etc. (Use SetWindowLong and WS_BORDER after child dialog is created? Or something particulr for dialog?)

  2. The OK button of parent dialog doesn't work anymore. How to fix this? I don't understand how to manage the dialog procedures in this parent-child dialog case. Any reference for it?


Here is my code:

1. Message handler for dialogs:

INT_PTR CALLBACK Child(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        {
            hChild = hDlg;
            return (INT_PTR)TRUE;
        }
    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

INT_PTR CALLBACK Parent(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        {
            hParent = hDlg;
            DialogBox(hInst, MAKEINTRESOURCE(IDD_CHILD), hParent, Child);
            SetParent(hChild, hParent);
            return (INT_PTR)TRUE;
        }
    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
    break;
    }
    return (INT_PTR)FALSE;
}

2. Dialog Templates in resource file

IDD_CHILD DIALOGEX 0, 0, 70, 50


STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD | WS_SYSMENU
CAPTION "Child Dialog"
FONT 8, "MS Shell Dlg"
BEGIN
    LTEXT           "Child Dialog",IDC_STATIC,10,5,35,8,SS_NOPREFIX
    DEFPUSHBUTTON   "OK",IDOK,10,25,50,14,WS_GROUP
END

IDD_DIALOG_PARENT DIALOGEX 0, 0, 250, 150
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Parent Dialog"
FONT 8, "MS Shell Dlg"
BEGIN
    LTEXT           "Parent",IDC_STATIC,42 - 20,14,35,8,SS_NOPREFIX
    DEFPUSHBUTTON   "OK",IDOK,188, 126,50,14,WS_GROUP
END
Was it helpful?

Solution

This isn't quite the right approach. You need to set the DS_CONTROL style on the child dialog (instead of WS_CHILD). This tells the window manager that you're creating the dialog as a child window and not a true pop-up dialog. You must also use CreateDialog or one of its variants instead of DialogBox to create the child (as per Raymond's comment).

More info here: https://devblogs.microsoft.com/oldnewthing/20040730-00/?p=38293

I can't really help you with the first question. It's not apparent what you're doing wrong from the snippets you provided.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top