| Tenouk C & C++ | MFC Home | MDI Serialization 2 | MDI Serialization 4 | Download | Site Index |


 

 

 

 

 

 

Module 12b:

Serialization: Reading and Writing Documents—MDI Applications 3

 

 

 

 

 

 

 

This is a continuation from previous module... Program examples compiled using Visual C++ 6.0 (MFC 6.0) compiler on Windows XP Pro machine with Service Pack 2. Topics and sub topics for this Tutorial are listed below. You can compare the standard C file I/O, standard C++ file I/O and Win32 directory, file and access controls with the MFC serialization. So many things lor! Similar but not same. Those links also given at the end of this tutorial.

  1. CMainFrame Class

  2. CChildFrame Class

  3. CMymfc18Doc Class

 

 

CMainFrame Class

 

This main frame class, listed in Listing 6, is almost identical to the SDI version, except that it's derived from CMDIFrameWnd instead of CFrameWnd.

 

 

MAINFRM.H

// MainFrm.h : interface of the CMainFrame class

//

//////////////////////////////////////////////////////////////////////

 

#if !defined(AFX_MAINFRM_H__7B5FE26B_85E9_11D0_8FE3_00C04FC2A0C2__INCLUDED_)

#define AFX_MAINFRM_H__7B5FE26B_85E9_11D0_8FE3_00C04FC2A0C2__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

class CMainFrame : public CMDIFrameWnd

{

    DECLARE_DYNAMIC(CMainFrame)

public:

    CMainFrame();

 

// Attributes

public:

// Operations

public:

 

// Overrides

    // ClassWizard generated virtual function overrides

    //{{AFX_VIRTUAL(CMainFrame)

    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

    //}}AFX_VIRTUAL

 

// Implementation

public:

    virtual ~CMainFrame();

#ifdef _DEBUG

    virtual void AssertValid() const;

    virtual void Dump(CDumpContext& dc) const;

#endif

 

protected// control bar embedded members

    CStatusBar  m_wndStatusBar;

    CToolBar    m_wndToolBar;

 

// Generated message map functions

protected:

    //{{AFX_MSG(CMainFrame)

    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

        // NOTE - the ClassWizard will add and remove member functions here.

        //    DO NOT EDIT what you see in these blocks of generated code!

    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

};

 

//////////////////////////////////////////////////////////////////////

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations

// immediately before the previous line.

 

#endif // !defined(AFX_MAINFRM_H__7B5FE26B_85E9_11D0_8FE3_00C04FC2A0C2__INCLUDED_)

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MAINFRM.CPP

// MainFrm.cpp : implementation of the CMainFrame class

//

 

#include "stdafx.h"

#include "mymfc18.h"

 

#include "MainFrm.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[ ] = __FILE__;

#endif

 

//////////////////////////////////////////////////////////////////////

// CMainFrame

 

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)

    //{{AFX_MSG_MAP(CMainFrame)

        // NOTE - the ClassWizard will add and remove mapping macros here.

        //    DO NOT EDIT what you see in these blocks of generated code !

    ON_WM_CREATE()

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

static UINT indicators[] =

{

    ID_SEPARATOR,           // status line indicator

    ID_INDICATOR_CAPS,

    ID_INDICATOR_NUM,

    ID_INDICATOR_SCRL,

};

 

//////////////////////////////////////////////////////////////////////

// CMainFrame construction/destruction

 

CMainFrame::CMainFrame()

{

    // TODO: add member initialization code here

}

 

CMainFrame::~CMainFrame()

{     }

 

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

    if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)

        return -1;

 

    if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD

        | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS

        | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||

        !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))

    {

        TRACE0("Failed to create toolbar\n");

        return -1;      // fail to create

    }

    if (!m_wndStatusBar.Create(this) ||

        !m_wndStatusBar.SetIndicators(indicators,

          sizeof(indicators)/sizeof(UINT)))

    {

        TRACE0("Failed to create status bar\n");

        return -1;      // fail to create

    }

 

    // TODO: Delete these three lines if you don't want the toolbar to

    //  be dockable

    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);

    EnableDocking(CBRS_ALIGN_ANY);

    DockControlBar(&m_wndToolBar);

 

    return 0;

}

 

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)

{

    if ( !CMDIFrameWnd::PreCreateWindow(cs) )

        return FALSE;

    // TODO: Modify the Window class or styles here by modifying

    //  the CREATESTRUCT cs

    return TRUE;

}

//////////////////////////////////////////////////////////////////////

// CMainFrame diagnostics

 

#ifdef _DEBUG

void CMainFrame::AssertValid() const

{

    CMDIFrameWnd::AssertValid();

}

 

void CMainFrame::Dump(CDumpContext& dc) const

{

    CMDIFrameWnd::Dump(dc);

}

 

#endif //_DEBUG

 

//////////////////////////////////////////////////////////////////////

// CMainFrame message handlers

 

 

Listing 6: The CMainFrame class listing.

 

CChildFrame Class

 

This child frame class, listed in Listing 7, lets you conveniently control the child frame window's characteristics by adding code in the PreCreateWindow() function. You can also map messages and override other virtual functions.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CHILDFRM.H

// ChildFrm.h : interface of the CChildFrame class

//

//////////////////////////////////////////////////////////////////////

 

#if !defined(AFX_CHILDFRM_H__7B5FE26D_85E9_11D0_8FE3_00C04FC2A0C2__INCLUDED_)

#define AFX_CHILDFRM_H__7B5FE26D_85E9_11D0_8FE3_00C04FC2A0C2__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

class CChildFrame : public CMDIChildWnd

{

    DECLARE_DYNCREATE(CChildFrame)

public:

    CChildFrame();

 

// Attributes

public:

 

// Operations

public:

 

// Overrides

    // ClassWizard generated virtual function overrides

    //{{AFX_VIRTUAL(CChildFrame)

    public:

    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

    virtual void ActivateFrame(int nCmdShow = -1);

    //}}AFX_VIRTUAL

 

// Implementation

public:

    virtual ~CChildFrame();

#ifdef _DEBUG

    virtual void AssertValid() const;

    virtual void Dump(CDumpContext& dc) const;

#endif

 

// Generated message map functions

protected:

    //{{AFX_MSG(CChildFrame)

        // NOTE - the ClassWizard will add and remove member functions here.

        //    DO NOT EDIT what you see in these blocks of generated code!

    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

};

 

//////////////////////////////////////////////////////////////////////

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations

// immediately before the previous line.

 

#endif // !defined(AFX_CHILDFRM_H__7B5FE26D_85E9_11D0_8FE3_00C04FC2A0C2__INCLUDED_)

 

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

CHILDFRM.CPP

// ChildFrm.cpp : implementation of the CChildFrame class

//

 

#include "stdafx.h"

#include "mymfc18.h"

 

#include "ChildFrm.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[ ] = __FILE__;

#endif

 

//////////////////////////////////////////////////////////////////////

// CChildFrame

 

IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd)

 

BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd)

    //{{AFX_MSG_MAP(CChildFrame)

        // NOTE - the ClassWizard will add and remove mapping macros here.

        //    DO NOT EDIT what you see in these blocks of generated code !

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

//////////////////////////////////////////////////////////////////////

// CChildFrame construction/destruction

 

CChildFrame::CChildFrame()

{

    // TODO: add member initialization code here

}

 

CChildFrame::~CChildFrame()

{         }

 

BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs)

{

    // TODO: Modify the Window class or styles here by modifying

    //  the CREATESTRUCT cs

 

    if ( !CMDIChildWnd::PreCreateWindow(cs) )

        return FALSE;

 

    return TRUE;

}

 

//////////////////////////////////////////////////////////////////////

// CChildFrame diagnostics

 

#ifdef _DEBUG

void CChildFrame::AssertValid() const

{

    CMDIChildWnd::AssertValid();

}

 

void CChildFrame::Dump(CDumpContext& dc) const

{

    CMDIChildWnd::Dump(dc);

}

 

#endif //_DEBUG

 

//////////////////////////////////////////////////////////////////////

// CChildFrame message handlers

 

void CChildFrame::ActivateFrame(int nCmdShow)

{

    TRACE("Entering CChildFrame::ActivateFrame\n");

    CMDIChildWnd::ActivateFrame(nCmdShow);

}

 

 

Listing 7: The CChildFrame class listing.

 

CMymfc18Doc Class

 

AppWizard originally generated the CMymfc18Doc class. Listing 8 shows the code used in the MYMFC18 example and the class is the same as the CMymfc17Doc class from the previous module.

 

MYMFC18DOC.H

//MYMFC18DOC.H

// MYMFC18DOC.h : interface of the CMYMFC18DOC class

//

//////////////////////////////////////////////////////////////////////

 

#if !defined(AFX_MYMFC18DOC_H__4D011047_7E1C_11D0_8FE0_00C04FC2A0C2__INCLUDED_)

#define AFX_MYMFC18DOC_H__4D011047_7E1C_11D0_8FE0_00C04FC2A0C2__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

#include "student.h"

 

class CMymfc18Doc : public CDocument

{

protected: // create from serialization only

    CMymfc18Doc();

    DECLARE_DYNCREATE(CMymfc18Doc)

 

// Attributes

public:

    CStudentList* GetList() {

        return &m_studentList;

    }

 

// Operations

public:

 

// Overrides

    // ClassWizard generated virtual function overrides

    //{{AFX_VIRTUAL(CMYMFC18DOC)

       public:

    virtual BOOL OnNewDocument();

    virtual void Serialize(CArchive& ar);

    virtual void DeleteContents();

       virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);

       //}}AFX_VIRTUAL

 

// Implementation

public:

    virtual ~CMymfc18Doc();

#ifdef _DEBUG

    virtual void AssertValid() const;

    virtual void Dump(CDumpContext& dc) const;

#endif

 

protected:

 

// Generated message map functions

protected:

    //{{AFX_MSG(CMYMFC18DOC)

    afx_msg void OnEditClearAll();

    afx_msg void OnUpdateEditClearAll(CCmdUI* pCmdUI);

       afx_msg void OnUpdateFileSave(CCmdUI* pCmdUI);

       //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

private:

    CStudentList m_studentList;

};

 

//////////////////////////////////////////////////////////////////////

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations

// immediately before the previous line.

 

#endif // !defined(AFX_MYMFC18DOC_H__4D011047_7E1C_11D0_8FE0_00C04FC2A0C2__INCLUDED_)

 

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MYMFC18DOC.CPP

// MYMFC18DOC.cpp : implementation of the CMYMFC18DOC class

//

 

#include "stdafx.h"

#include "mymfc18.h"

 

#include "mymfc18Doc.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[ ] = __FILE__;

#endif

 

//////////////////////////////////////////////////////////////////////

// CMYMFC18DOC

 

IMPLEMENT_DYNCREATE(CMymfc18Doc, CDocument)

 

BEGIN_MESSAGE_MAP(CMymfc18Doc, CDocument)

    //{{AFX_MSG_MAP(CMYMFC18DOC)

    ON_COMMAND(ID_EDIT_CLEAR_ALL, OnEditClearAll)

    ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR_ALL, OnUpdateEditClearAll)

       ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)

       //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

//////////////////////////////////////////////////////////////////////

// CMYMFC18DOC construction/destruction

 

CMymfc18Doc::CMymfc18Doc()

{

    TRACE("Entering CMYMFC18DOC constructor\n");

#ifdef _DEBUG

    afxDump.SetDepth(1); // Ensure dump of list elements

#endif // _DEBUG

}

 

CMymfc18Doc::~CMymfc18Doc()

{       }

 

BOOL CMymfc18Doc::OnNewDocument()

{

    TRACE("Entering CMymfc18Doc::OnNewDocument\n");

    if (!CDocument::OnNewDocument())

        return FALSE;

    // TODO: add re-initialization code here

    // (SDI documents will reuse this document)

    return TRUE;

}

//////////////////////////////////////////////////////////////////////

// CMymfc18Doc serialization

 

void CMymfc18Doc::Serialize(CArchive& ar)

{

    TRACE("Entering CMymfc18Doc::Serialize\n");

    if (ar.IsStoring())

    {

        // TODO: add storing code here

    }

    else

    {

        // TODO: add loading code here

    }

    m_studentList.Serialize(ar);

}

 

//////////////////////////////////////////////////////////////////////

// CMymfc18Doc diagnostics

 

#ifdef _DEBUG

void CMymfc18Doc::AssertValid() const

{

    CDocument::AssertValid();

}

 

void CMymfc18Doc::Dump(CDumpContext& dc) const

{

    CDocument::Dump(dc);

    dc << "\n" << m_studentList << "\n";

}

#endif //_DEBUG

 

//////////////////////////////////////////////////////////////////////

// CMymfc18Doc commands

 

void CMymfc18Doc::DeleteContents()

{

TRACE("Entering CMymfc18Doc::DeleteContents\n");

    while (m_studentList.GetHeadPosition())

       {

        delete m_studentList.RemoveHead();

    }

}

 

void CMymfc18Doc::OnEditClearAll()

{

    DeleteContents();

    UpdateAllViews(NULL);

}

 

void CMymfc18Doc::OnUpdateEditClearAll(CCmdUI* pCmdUI)

{

    pCmdUI->Enable(!m_studentList.IsEmpty());

}

 

BOOL CMymfc18Doc::OnOpenDocument(LPCTSTR lpszPathName)

{

    TRACE("Entering CMymfc18Doc::OnOpenDocument\n");

     if (!CDocument::OnOpenDocument(lpszPathName))

        return FALSE;

    // TODO: Add your specialized creation code here

    return TRUE;

}

 

void CMymfc18Doc::OnUpdateFileSave(CCmdUI* pCmdUI)

{

       // TODO: Add your command update UI handler code here

       pCmdUI->Enable(IsModified());

      

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 8: The CMymfc18Doc class listing.

 

Continue on next module...part 4.

 

 

 

 

Further reading and digging:

  1. Standard C File Input/Output.

  2. Standard C++ File Input/Output.

  3. Win32 File Input/Output: Module C, Module D and Module E.

  4. MSDN MFC 7.0 class library online documentation.

  5. MSDN MFC 9.0 class library online documentation - latest version.

  6. MSDN Library

  7. Windows data type.

  8. Win32 programming Tutorial.

  9. The best of C/C++, MFC, Windows and other related books.

  10. Unicode and Multibyte character set: Story and program examples.

 


 

| Tenouk C & C++ | MFC Home | MDI Serialization 2 | MDI Serialization 4 | Download | Site Index |