| Tenouk C & C++ | MFC Home | SDI Serialization 1 | SDI Serialization 3 | Download | Site Index |


 

 

 

 

 

 

 

Module 11a:

Serialization: Reading and Writing Documents—SDI Applications 2

 

 

 

 

 

 

 

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. The MYMFC17 Example: SDI with Serialization

  2. CStudent Class

 

 

The MYMFC17 Project Example: SDI with Serialization

 

The MYMFC17 example is similar to example MYMFC16. The student dialog and the toolbar are the same except the step 4 where we set the Advanced Options (shown below) and the view class is the same. The steps have been simplified in the following Figures.

 

MYMFC17 AppWizard step 4 of 6, setting the Advanced options.

 

Figure 7: MYMFC17 AppWizard step 4 of 6, setting the Advanced options.

 

Click the Advanced button, fill the File extension as shown and for other fields will be provided automatically. Take note that name length will be truncated. You can change to other name but for this example, just accept the default.

 

The file extension used is myext.

 

Figure 8: The file extension used is myext.

 

AppWizard step 6 of 6 for MYMFC17 project.

 

Figure 9: AppWizard step 6 of 6 for MYMFC17 project, using a CFormView class.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MYMFC17 project summary.

 

Figure 10: MYMFC17 project summary.

 

Control

ID

The dialog template

IDD_MYMFC17_FORM

Name edit control

IDC_NAME

Grade edit control

IDC_GRADE

Clear pushbutton

IDC_CLEAR

 

Table 2.

 

MYMFC17 dialog and its controls, similar to MYMFC16.

 

Figure 11: MYMFC17 dialog and its controls, similar to MYMFC16.

 

Object ID

Message

Member Function

ID_EDIT_CLEAR_ALL

COMMAND

OnEditClearAll()

ID_EDIT_CLEAR_ALL

ON_UPDATE_COMMAND_UI

OnUpdateEditClearAll()

 

Table 4.

 

Message mapping for IDC_EDIT_CLEAR_ALL.

 

Figure 12: Message mapping for IDC_EDIT_CLEAR_ALL.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Object ID

Message

Member Function

IDC_CLEAR

BN_CLICKED

OnClear()

 

Table 5.

 

Message mapping for IDC_ CLEAR.

 

Figure 13: Message mapping for IDC_ CLEAR.

 

Control ID

Member Variable

Category

Variable Type

IDC_GRADE

m_nGrade

Value

int

IDC_NAME

m_strName

Value

CString

 

Table 6.

 

Adding member variables.

 

Figure 14: Adding member variables.

 

Modifying Clear All menu properties.

 

Figure 15: Adding and modifying Clear All menu properties.

 

 

Object ID

Message

Member Function

ID_STUDENT_HOME

COMMAND

OnStudentHome()

ID_STUDENT_END

COMMAND

OnStudentEnd()

ID_STUDENT_PREV

COMMAND

OnStudentPrev()

ID_STUDENT_NEXT

COMMAND

OnStudentNext()

ID_STUDENT_INS

COMMAND

OnStudentIns()

ID_STUDENT_DEL

COMMAND

OnStudentDel()

 

Table 7.

 

 

Adding and modifying toolbar buttons properties.

 

Figure 16: Adding and modifying toolbar buttons properties.

 

Object ID

Message

Member Function

ID_STUDENT_HOME

UPDATE_COMMAND_UI

OnUpdateStudentHome()

ID_STUDENT_END

UPDATE_COMMAND_UI

OnUpdateStudentEnd()

ID_STUDENT_PREV

UPDATE_COMMAND_UI

OnUpdateStudentHome()

ID_STUDENT_NEXT

UPDATE_COMMAND_UI

OnUpdateStudentEnd()

ID_STUDENT_DEL

UPDATE_COMMAND_UI

OnUpdateCommandDel()

 

Table 8.

 

Messages mapping for toolbar buttons.

 

Figure 17: Messages mapping for toolbar buttons.

 

Serialization has been added, together with an update command UI function for File Save. The header and implementation files for the view and document classes will be reused in example MYMFC18 in the next module. All the new code (code that is different from MYMFC16) is listed, with additions and changes to the AppWizard-generated code and the ClassWizard code in orange if any. A list of the files and classes in the MYMFC17 example is shown in the following table.

 

Header File

Source Code File

Class

Description

mymfc17.h

mymfc17.cpp

CMymfc17App

Application class (from AppWizard)

-

-

CAboutDlg

About dialog

MainFrm.h

MainFrm.cpp

CMainFrame

SDI main frame

mymfc17Doc.h

mymfc17Doc.cpp

CMymfc17Doc

Student document

mymfc17View.h

mymfc17View.cpp

CMymfc17View

Student form view (from MYMFC17)

Student.h

Student.cpp

CStudent

Student record

StdAfx.h

StdAfx.cpp

 

Precompiled headers (with afxtempl.h included)

 

Table 9.

 

MYMFC17 files seen through FileView.

 

Figure 18: MYMFC17 files seen through FileView.

 

 

CStudent Class

 

The following steps show how to add the CStudent class (Student.h and Student.cpp).

 

Creating and adding new files for CStudent class to the project.

 

Figure 19: Creating and adding new files for CStudent class to the project.

 

Creating and adding Student.cpp file to the project.

 

Figure 20: Creating and adding Student.cpp file to the project.

 

STUDENT.H

// student.h

 

#ifndef _INSIDE_VISUAL_CPP_STUDENT

#define _INSIDE_VISUAL_CPP_STUDENT

 

class CStudent : public CObject

{

    DECLARE_SERIAL(CStudent)

public:

    CString m_strName;

    int m_nGrade;

 

    CStudent()

    {

        m_nGrade = 0;

    }

 

    CStudent(const char* szName, int nGrade) : m_strName(szName)

    {

        m_nGrade = nGrade;

    }

 

    CStudent(const CStudent& s) : m_strName(s.m_strName)

    {

        // copy constructor

        m_nGrade = s.m_nGrade;

    }

 

    const CStudent& operator =(const CStudent& s)

    {

        m_strName = s.m_strName;

        m_nGrade = s.m_nGrade;

        return *this;

    }

 

    BOOL operator ==(const CStudent& s) const

    {

        if ((m_strName == s.m_strName) && (m_nGrade == s.m_nGrade))

              {

            return TRUE;

        }

        else

              {

            return FALSE;

        }

    }

 

    BOOL operator !=(const CStudent& s) const

    {

        // Let's make use of the operator we just defined!

        return !(*this == s);

    }

#ifdef _DEBUG

    void Dump(CDumpContext& dc) const;

#endif // _DEBUG

};

 

#endif // _INSIDE_VISUAL_CPP_STUDENT

 

typedef CTypedPtrList<CObList, CStudent*> CStudentList;

 

 

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

 

 

STUDENT.CPP

#include "stdafx.h"

#include "student.h"

 

IMPLEMENT_SERIAL(CStudent, CObject, 0)

 

#ifdef _DEBUG

void CStudent::Dump(CDumpContext& dc) const

{

    CObject::Dump(dc);

    dc << "m_strName = " << m_strName << "\nm_nGrade = " <<m_nGrade;

}

#endif // _DEBUG

 

 

Listing 1: CStudent class.

 

The use of the MFC template collection classes requires the following statement in StdAfx.h:

 

#include <afxtempl.h>

 

The MYMFC17 Student.h file is almost the same as the file in the MYMFC17 project except the header contains the macro:

 

DECLARE_SERIAL(CStudent)

 

instead of:

 

DECLARE_DYNAMIC(CStudent)

 

 

Listing 2.

 

and the implementation file contains the macro:

 

IMPLEMENT_SERIAL(CStudent, CObject, 0)

 

instead of:

 

IMPLEMENT_DYNAMIC(CStudent, Cobject)

 

 

Listing 3.

 

The virtual Serialize() function has also been added.

 

Continue on next module...part 3.

 

 

 

 

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 C & C++ | MFC Home | SDI Serialization 1 | SDI Serialization 3 | Download | Site Index |