| Tenouk | Home | SDI Serialization 3 | SDI Serialization 5 | Download | Site Index |


 

 

 

 

 

 

Module 11c:

Serialization: Reading and Writing Documents—SDI Applications 4

 

 

 

 

 

 

 

 

This is a continuation from the 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. CMymfc17Doc Class

  2. Serialize()

  3. DeleteContents()

  4. OnOpenDocument()

  5. OnUpdateFileSave()

 

 

CMymfc17Doc Class

 

AppWizard originally generated the CMymfc17Doc class. Figure 6 shows the code used in the MYMFC17 example. The CMymfc17Doc class is the same as the CMymfc16Doc class from the previous module except for four functions: Serialize(), DeleteContents(), OnOpenDocument(), and OnUpdateFileSave().

 

MYMFC17DOC.H

//MYMFC17DOC.H

// Mymfc17Doc.h : interface of the CMymfc17Doc class

//

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

 

#if !defined(AFX_MYMFC17DOC_H__4D011047_7E1C_11D0_8FE0_00C04FC2A0C2__INCLUDED_)

#define AFX_MYMFC17DOC_H__4D011047_7E1C_11D0_8FE0_00C04FC2A0C2__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

#include "student.h"

 

class CMymfc17Doc : public CDocument

{

protected: // create from serialization only

    CMymfc17Doc();

    DECLARE_DYNCREATE(CMymfc17Doc)

 

// Attributes

public:

    CStudentList* GetList() {

        return &m_studentList;

    }

 

// Operations

public:

 

// Overrides

    // ClassWizard generated virtual function overrides

    //{{AFX_VIRTUAL(CMymfc17Doc)

       public:

    virtual BOOL OnNewDocument();

    virtual void Serialize(CArchive& ar);

    virtual void DeleteContents();

       virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);

       //}}AFX_VIRTUAL

 

// Implementation

public:

    virtual ~CMymfc17Doc();

#ifdef _DEBUG

    virtual void AssertValid() const;

    virtual void Dump(CDumpContext& dc) const;

#endif

 

protected:

 

// Generated message map functions

protected:

    //{{AFX_MSG(CMymfc17Doc)

    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_MYMFC16DOC_H__4D011047_7E1C_11D0_8FE0_00C04FC2A0C2__INCLUDED_)

 

 

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MYMFC17DOC.CPP

// Mymfc17Doc.cpp : implementation of the CMymfc17Doc class

//

 

#include "stdafx.h"

#include "mymfc17.h"

 

#include "Mymfc17Doc.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[ ] = __FILE__;

#endif

 

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

// CMymfc17Doc

 

IMPLEMENT_DYNCREATE(CMymfc17Doc, CDocument)

 

BEGIN_MESSAGE_MAP(CMymfc17Doc, CDocument)

    //{{AFX_MSG_MAP(CMymfc17Doc)

    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()

 

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

// CMymfc17Doc construction/destruction

 

CMymfc17Doc::CMymfc17Doc()

{

    TRACE("Entering CMymfc17Doc constructor\n");

#ifdef _DEBUG

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

#endif // _DEBUG

}

 

CMymfc17Doc::~CMymfc17Doc()

{

}

 

BOOL CMymfc17Doc::OnNewDocument()

{

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

    if (!CDocument::OnNewDocument())

        return FALSE;

 

    // TODO: add re-initialization code here

    // (SDI documents will reuse this document)

 

    return TRUE;

}

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

// CMymfc17Doc serialization

 

void CMymfc17Doc::Serialize(CArchive& ar)

{

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

    if (ar.IsStoring())

    {

        // TODO: add storing code here

    }

    else

    {

        // TODO: add loading code here

    }

    m_studentList.Serialize(ar);

}

 

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

// CMymfc17Doc diagnostics

 

#ifdef _DEBUG

void CMymfc17Doc::AssertValid() const

{

    CDocument::AssertValid();

}

 

void CMymfc17Doc::Dump(CDumpContext& dc) const

{

    CDocument::Dump(dc);

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

}

#endif //_DEBUG

 

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

// CMymfc17Doc commands

 

void CMymfc17Doc::DeleteContents()

{

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

    while (m_studentList.GetHeadPosition())

       {

        delete m_studentList.RemoveHead();

    }

}

 

void CMymfc17Doc::OnEditClearAll()

{

    DeleteContents();

    UpdateAllViews(NULL);

}

 

void CMymfc17Doc::OnUpdateEditClearAll(CCmdUI* pCmdUI)

{

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

}

 

BOOL CMymfc17Doc::OnOpenDocument(LPCTSTR lpszPathName)

{

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

     if (!CDocument::OnOpenDocument(lpszPathName))

        return FALSE;

 

    // TODO: Add your specialized creation code here

 

    return TRUE;

}

 

void CMymfc17Doc::OnUpdateFileSave(CCmdUI* pCmdUI)

{

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

       pCmdUI->Enable(IsModified());

      

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Listing 6: The CMymfc17Doc class listing.

 

 

Serialize()

 

One line has been added to the AppWizard-generated function to serialize the document's student list, as shown here:

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

// CStudentDoc serialization

 

void CStudentDoc::Serialize(CArchive& ar)

{

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

    if (ar.IsStoring())

    {

        // TODO: add storing code here

    }

    else

    {

        // TODO: add loading code here

    }

    m_studentList.Serialize(ar);

}

DeleteContents()

 

The Dump statement is replaced by a simple TRACE statement. Here is the modified code:

 

void CStudentDoc::DeleteContents()

{

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

    while (m_studentList.GetHeadPosition())

    {

        delete m_studentList.RemoveHead();

    }

}

 

OnOpenDocument()

 

This virtual function is overridden only for the purpose of displaying a TRACE message, as shown below.

 

OnOpenDocument() message mapping for CMymfc17Doc class.

 

Figure 22: OnOpenDocument() message mapping for CMymfc17Doc class.

 

BOOL CStudentDoc::OnOpenDocument(LPCTSTR lpszPathName)

{

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

     if (!CDocument::OnOpenDocument(lpszPathName))

        return FALSE;

    // TODO: Add your specialized creation code here

    return TRUE;

}

 

OnUpdateFileSave()

 

This message map function grays the File Save toolbar button when the document is in the unmodified state. The view controls this state by calling the document's SetModifiedFlag() function, as shown here:

 

Adding message handler for ID_FILE_SAVE.

 

Figure 23: Adding message handler for ID_FILE_SAVE.

 

void  CStudentDoc::OnUpdateFileSave(CCmdUI* pCmdUI)

{

    // Disable disk toolbar button if file is not modified

    pCmdUI->Enable(IsModified());

}

 

Continue on next module....part 5.

 

 

 

 

 

 

 

 

 

Further reading and digging:

  1. MSDN MFC 7.0 class library online documentation.

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

  3. Porting & Migrating your older programs.

  4. MSDN Library

  5. DCOM at MSDN.

  6. COM+ at MSDN.

  7. COM at MSDN.

  8. Windows data type.

  9. Win32 programming Tutorial.

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

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

 

 


 

| Tenouk | Home | SDI Serialization 3 | SDI Serialization 5 | Download | Site Index |