Vous êtes sur la page 1sur 4

Program to implement Serialization

Edit view.cpp file,


void CCustView::OnDraw(CDC* pDC)
{
CCustDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
int nLineHeight = tm.tmHeight + tm.tmExternalLeading;
CPoint ptText(0, 0);
for(int nIndex = 0; nIndex < pDoc->GetCount(); nIndex++)
{
CString strOut;
CUser* pUser = pDoc->GetUser(nIndex);
strOut.Format("User = %s, email = %s",
pUser->GetName(),
pUser->GetAddr());
pDC->TextOut(ptText.x, ptText.y, strOut);
ptText.y += nLineHeight;
}
}

void CCustView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)


{
InvalidateRect( NULL );
}

Include the following in Doc.h file.,


int GetCount() const;
CUser* GetUser(int nUser) const;
CArray<CUser*, CUser*&> m_setOfUsers;

Edit Doc.cpp, and insert the Menu Item Click Event Handler for EditUser,
void CCustDoc::OnEdituser()
{
CUsersDlg dlg;

if(dlg.DoModal() == IDOK)
{
CUser* pUser = new CUser(dlg.m_strName, dlg.m_strAddr);
m_setOfUsers.Add(pUser);
UpdateAllViews(NULL);
SetModifiedFlag();
}
}

1
And also add the definition for user defined functions,
int CCustDoc::GetCount() const
{
return m_setOfUsers.GetSize();
}

CUser* CCustDoc::GetUser(int nUser) const


{
CUser* pUser = 0;
if(nUser < m_setOfUsers.GetSize())
pUser = m_setOfUsers.GetAt(nUser);
return pUser;
}

void AFXAPI SerializeElements( CArchive& ar, CUser** pUser, int nCount )


{
for( int i = 0; i < nCount; i++, pUser++ )
{
if( ar.IsStoring() )
{
(*pUser)->Serialize(ar);
}
else
{
CUser* pNewUser = new CUser;
pNewUser->Serialize(ar);
*pUser = pNewUser;
}
}
}

Open the Notepad and type the following and save as Users.h

#pragma once
class CUser : public CObject
{
DECLARE_SERIAL(CUser);
public:
// Constructors
CUser();
CUser(const CString& strName, const CString& strAddr);
// Attributes
void Set(const CString& strName, const CString& strAddr);
CString GetName() const;
CString GetAddr() const;
// Operations

2
virtual void Serialize(CArchive& ar);
// Implementation
private:
// The user's name
CString m_strName;
// The user's e-mail address
CString m_strAddr;
};

Open notepad and type the following and save as Users.cpp file..

#include "stdafx.h"
#include "Users.h"
IMPLEMENT_SERIAL(CUser, CObject, 1);

CUser::CUser() { }

CUser::CUser(const CString& strName, const CString& strAddr)


{
Set(strName, strAddr);
}

void CUser::Set(const CString& strName, const CString& strAddr)


{
m_strName = strName;
m_strAddr = strAddr;
}

CString CUser::GetName() const


{
return m_strName;
}

CString CUser::GetAddr() const


{
return m_strAddr;

void CUser::Serialize(CArchive& ar)


{
if(ar.IsLoading())
{
ar >> m_strName >> m_strAddr;
}
else

3
{
ar << m_strName << m_strAddr;
}
}
1. Go to Resources pane, Insert a new Dialog with two edit boxes for Name and
Mail Id.
2. Create member Variables of type CString for both the edit controls, the variable
names should be similar to the one used in Users.cpp “ as m_ “, which u’ve
assigned in the Get, Set, etc. functions.
3. Then insert a New class for this dialog by clicking the class wizard when u are in
the dialog box, it automatically will promp as “Do u wish to create a new Class,
Say Yes, and give the class name as UsersDlg, respective header and cpp files
will be created.

Now include the following in the Header files specified…

1. Open StdAfx.h and include the following, after #endif.


#include "afxtempl.h" // For Referring CArray template.
2. Open Doc.h and include #include “Users.h“, and #include "UsersDlg.h"
3. Open View.h and include #include “Users.h“.

Vous aimerez peut-être aussi