Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

nested dialog boxes

Posted on 1998-04-02
3
Medium Priority
?
654 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:phant
3 Comments
 
LVL 1

Expert Comment

by:dkremer
ID: 1317430
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
 

Author Comment

by:phant
ID: 1317431
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
 
LVL 4

Accepted Solution

by:
piano_boxer earned 100 total points
ID: 1317432
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question