nested dialog boxes

I want to create a CDialog with scrollbars within another CDialog so that the scrollbars will scroll automatically.  This is to resemble the Scrolled Window in Motif.  I know that the DS_CONTROL style will allow a CDialog to be created as a child of another dialog box, but I don't know how to make it work.  Any ideas? please respond to phant@ugsolutions.com
Thanks.
phantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dkremerCommented:
Hi.
Create the second dialog (the one you want contained) using Create from the OnInitDialog (WM_INITDIALOG handler) of the first dialog, and specify 'this' as the parent, then call SetWindowPos on the second dialog and set it to the size of the first dialog... here's a sample code :

void CParentDialog::OnInitDialog()
{
  CDialog::OnInitDialog();

  CRect rcDlg;

  m_SecondDlg.Create(CSecondDialog::IDD, this); // Create the second dialog
  GetWindowRect(rcDlg);
  m_SecondDlg.SetWindowPos(NULL, 0, 0, rcDlg.Width(), rcDlg.Height(), SWP_NOZORDER); // If your calculations subtract the scrollbars width and height from the width and height paramters of SetWindowPos

  m_SecondDlg.SetExStyle(0, WS_EX_CONTROLPARENT); // Otherwise you'll have problems when the focus is lost from your application
  return TRUE;
}

Hmm... that's it I think.


0
phantAuthor Commented:
Thanks to dkremer for giving the quick response, but I want to build the dialog dynamically without using the resource template.  The Create member function, as suggested by dkremer, requires a resource template, which does not work in my case.
Any other suggestions?
0
piano_boxerCommented:
Hi.

I'v think i have the right solution for you (or part of it).

You previously mentioned that you dont want to use any dialog resource
for your child dialog, but create all the controls in code. This gave me
some problems but i managed to solve it.


1a. Create a new class derived from CWnd (Generic CWnd) using Class Wizard.
    I'll call it CChildDialog. This class will be the special child dialog
    with a vertical scrollbar.

1b. Include the header file for CChildDialog in the top of your CParentDialog's
    header file and add the CChildDialog class as a member variable in CParentDialog.
    I'll call this m_wndChildDialog

   
    CChildDialog    m_wndChildDialog;

2a. To create the child dialog modify add a handler for WM_CREATE in CParentDialog and
    modify it as follows:

    int CParentDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        if (CParentDialog::OnCreate(lpCreateStruct) == -1)
            return -1;
       
        if(!m_wndChildDialog.CreateEx(WS_EX_CONTROLPARENT, "#32770", "",
            WS_VISIBLE|WS_CHILD|WS_VSCROLL,
            0, 0, 0, 0, m_hWnd, NULL))
            return -1;
       
        return 0;
    }

2b: The string "#32779" is Windows (secret) class for dialogs. So lets hope they dont change it :-)

2c: now we need to adjust the size and position of this newly created child dialog. Add a handler
    for WM_SIZE to CParentDialog and modify as follows.


    void CParentDialog::OnSize(UINT nType, int cx, int cy)
    {
        CParentDialog::OnSize(nType, cx, cy);
       
        if(m_wndChildDialog.m_hWnd)
        {
            m_wndChildDialog.SetWindowPos(NULL, 100, 0, cx-100, rcClient.Height(),
                SWP_NOZORDER);
        }
    }

2d: I just positions the child dialog 100 pixels from the left side. You should modify this to
    meet your needs.

2e: Now the child dialog is created a ready for use. PLEASE NOTE: WM_INITDIALOG WILL NOT BE SEND
    TO THE CHILD DIALOG.

3a: Now we can modify the CChildDialog class to support:
   
    * Vertical scrolling of dialog (and child controls).
    * Auto-scroll to control receiving input focus.
    * Switch focus to next control when the enter-key is pressed in a edit control.

3b: Add a the following member-variables to CChildDialog and initialize m_nScrollPos to zero
    in the constructor:

         
    // In H-file
    class CChildDialog  : public CWnd
    {
        [...cut...]
       
    protected:
        int     m_nScrollPos;
        CEdit   m_rgWndEdit[100];

        [...cut...]
    }


    // In CPP-file
    CChildDialog::CChildDialog()
    {
        m_nScrollPos = 0;
    }

    The 100 edit control is just there to show an example of controls in the child dialog. You
    need to modify it to your needs.

3c: Add handler to the following messages in CChildDialog:
   
    WM_CREATE, WM_SIZE, WM_VSCROLL.

3d: Modify the funtions to look something like this:
    (NOTE: I use hardcoded values for child-dialog size).


    int CChildDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        if (CWnd::OnCreate(lpCreateStruct) == -1)
            return -1;

        CFont* pFont = CFont::FromHandle( (HFONT)::GetStockObject(ANSI_VAR_FONT));
        SetFont( pFont );
       
        //
        // Create 100 edit-controls (just for testing).
        //
        for(int i=0;i<100;i++)
        {
            m_rgWndEdit[i].CreateEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD|WS_VISIBLE|WS_TABSTOP|WS_BORDER,
                10, i*22+5, 250, 22, m_hWnd, (HMENU)(1000+i));

            m_rgWndEdit[i].SetFont( pFont );
        }
           
        return 0;
    }

    void CChildDialog::OnSize(UINT nType, int cx, int cy)
    {
        CWnd::OnSize(nType, cx, cy);

        SCROLLINFO si;
        si.cbSize = sizeof(si);
        si.fMask = SIF_RANGE | SIF_PAGE;
        si.nMin = 0;
        si.nMax = 100*22 +10;       // <<<< Change this to the bottom pos of your last control
                                    //      on the child-dialog.
        si.nPage = (UINT)cy;

        SetScrollInfo(SB_VERT, &si);

    }
 
    void CChildDialog::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
    {
        CRect rcClient;
        GetClientRect(&rcClient);

       
        int nNewPos = m_nScrollPos;

        switch(nSBCode)
        {
        case SB_PAGEDOWN:
            nNewPos = min((100*22+10)-rcClient.Height(), m_nScrollPos+rcClient.Height());
            break;
        case SB_PAGEUP:
            nNewPos = max(0, m_nScrollPos-rcClient.Height());
            break;
        case SB_LINEDOWN:
            nNewPos = min((100*22+10)-rcClient.Height(), m_nScrollPos+22);
            break;
        case SB_LINEUP:
            nNewPos = max(0, m_nScrollPos-22);
            break;
        case SB_THUMBPOSITION:
        case SB_THUMBTRACK:
            nNewPos = nPos;
            break;
        }
       
        if(nNewPos != m_nScrollPos)
        {
            SCROLLINFO si;
            si.cbSize = sizeof(si);
            si.fMask = SIF_POS;
            si.nPos = nNewPos;
            SetScrollInfo(SB_VERT, &si);
            ScrollWindowEx(0, m_nScrollPos-nNewPos, NULL, NULL, NULL, NULL,
                SW_SCROLLCHILDREN|SW_INVALIDATE|SW_ERASE);
            m_nScrollPos = nNewPos;
        }

        CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
    }

3e: To support moving focus to the next control when user hits enter in a edit-control
    override the PreTranslateMessage:


    BOOL CChildDialog::PreTranslateMessage(MSG* pMsg)
    {
        //
        // This code will set focus to the next control if enter is pressed in a edit control
        //
        if(pMsg->message == WM_KEYDOWN)
        {  
            CWnd* pFocus = GetFocus();
            if(pFocus && pFocus->IsKindOf(RUNTIME_CLASS(CEdit)))
            {
                switch(pMsg->wParam)
                {
                case VK_RETURN:
                    GetParent()->GetNextDlgTabItem(pFocus)->SetFocus();
                    return TRUE;
                }
            }
        }

           
        return CWnd::PreTranslateMessage(pMsg);
    }

3f: The following code will scroll the child dialog to ensure that the control that receives
    the input focus is actually visible:

    BOOL CChildDialog::OnCommand(WPARAM wParam, LPARAM lParam)
    {
        if( lParam && HIWORD(wParam)==EN_SETFOCUS )
        {
            MessageBeep(-1);

            CWnd* pControl = CWnd::FromHandle((HWND)lParam);
            ASSERT_VALID(pControl);

            CRect rcControl;
            pControl->GetWindowRect(&rcControl);
            ScreenToClient(&rcControl);

            int nScroll = 0;
            if(rcControl.top < 0)
                nScroll = -rcControl.top;
            else
            {
                CRect rc;
                GetClientRect(&rc);
                if(rcControl.bottom > rc.bottom)
                    nScroll = rc.bottom-rcControl.bottom;
            }

            if(nScroll)
            {
                m_nScrollPos = m_nScrollPos-nScroll;
                SCROLLINFO si;
                si.cbSize = sizeof(si);
                si.fMask = SIF_POS;
                si.nPos = m_nScrollPos;
                SetScrollInfo(SB_VERT, &si);
                ScrollWindowEx(0, nScroll, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN|SW_INVALIDATE|SW_ERASE);
            }
        }

        return CWnd::OnCommand(wParam, lParam);
    }


************

This will help you get going. Please fell free to post additional questions.
One aditional nice function would be one that could find to bottom-most control
and adjust the scrolling parameters.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.