Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Child Dialog (Sub Forms)

, 17 Nov 1999
A simple class for handling Child Dialogs within a dialog or property sheet.
//  Project     : any
//  Compiler    : Visual C++ 6.0
//  Plattform   : Windows 95/NT
//  File        : SubFormCollection.cpp
//  Programmer  : D. Zuppinger, SoftToys
//  Copyright   : public domain
//  Contact     : daniel@softtoys.com
//  Description : class for simple support of sub forms within dialogs
//  History     : October 1999, initial version

//#### This is the class, supporting an easy handling of sub forms

#include "stdafx.h"
#include "SubFormCollection.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSubFormCollection::CSubFormCollection()
{
    // initialize some members
    m_nActiveForm = -1;
    m_Rect.right  = 0;
    m_Rect.top    = 0;
    m_Rect.bottom = 0;
    m_Rect.left   = 0;
    m_bCentered = false;
}

CSubFormCollection::~CSubFormCollection()
{
    // Free all sub forms on destruction
    for (int i = 0; i <=  m_Forms.GetUpperBound(); i++) {
        CDialog* pDlg = m_Forms.GetAt(i);
        pDlg->DestroyWindow();
        delete pDlg;
    }
}

//////////////////////////////////////////////////////////////////////
// Methods
//////////////////////////////////////////////////////////////////////

bool CSubFormCollection::SetActive(int ix)
{
    if (ix > m_Forms.GetUpperBound()) {
        // does not exist
        return(false);
    }
    m_nActiveForm = ix; // make it the active one
    return(true);
}


bool CSubFormCollection::CreateSubForm(UINT id,CWnd* pParentWnd)
{
    CDialog* pDlg = new CDialog();
    ASSERT(pDlg != NULL);
    if (pDlg == NULL) return(false); // exit and tell the world about it
    AddForm(pDlg); // Add it to the internal table
    pDlg->Create(id,pParentWnd);
    CRect r;
    int x,y;
    pDlg->GetWindowRect(&r); // Get the size and position of the dialog in screen coordinates
    if (m_bCentered) {
        x = m_Rect.left - r.left + ((m_Rect.Width() - r.Width()) / 2);
        y = m_Rect.top - r.top + ((m_Rect.Height() - r.Height()) / 2);
    }
    else { // just use the top left corner
        x = m_Rect.left - r.left;
        y = m_Rect.top - r.top;
    }
    pDlg->SetWindowPos(pParentWnd,x,y, r.Width(), r.Height(), SWP_NOZORDER);
    return(true); // everything ok
}

bool CSubFormCollection::ShowSubForm(int ix)
{
    ASSERT(ix <= m_Forms.GetUpperBound());
    if (ix > m_Forms.GetUpperBound()) return(false); // does not exist

    CDialog* pDlg;
    if (ix != -1) {
        if (m_nActiveForm != -1) {
            pDlg = m_Forms.GetAt(m_nActiveForm);
            ASSERT(pDlg != NULL);
            ASSERT(pDlg->m_hWnd != NULL);
            if ((pDlg == NULL) 
            ||  (pDlg->m_hWnd == NULL)) return(false);
            pDlg->ShowWindow(SW_HIDE);
        }
        m_nActiveForm = ix; // this is the active one now;
    }
    else {
        if (m_nActiveForm == -1) {
            m_nActiveForm = 0;
        }
    }
    pDlg = m_Forms.GetAt(m_nActiveForm);
    ASSERT(pDlg != NULL);
    ASSERT(pDlg->m_hWnd != NULL);
    if ((pDlg == NULL) 
    ||  (pDlg->m_hWnd == NULL)) return(false);
    pDlg->ShowWindow(SW_SHOW);
    pDlg->Invalidate();
    pDlg->RedrawWindow();
    return(true);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Daniel Zuppinger
Chief Technology Officer
Switzerland Switzerland
Professional IT developer since 1983. First projects with Cobol, then Pascal, Modula2, C and since Visual C++ 1.0 also with C++. Works since 1986 as Consultant, between 1990 and 2008 for Infobrain in Switzerland, currently employed by enValue (also Switzerland).
Current field is the project management and development of software for Bank applications.
Married, two daughters, Hobbies : Paragliding, Orienteering, Mountainbiking, Photography

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150224.1 | Last Updated 18 Nov 1999
Article Copyright 1999 by Daniel Zuppinger
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid