1.Run AppWizard(.exe),check SDI.Choose CFormView base class instead of CView.
2.Insert->Resource->Dialog->IDD_Dialog1.
3.Project->Add to project->New->Student.h and Student.cpp.
4.Define a student class that inherits the base class Cobject to make the class serializable and override the function serialize().
5.Modify the Document and View class.
6.Build and test the application.
CODING:
Student.h
#ifndef _INSIDE_VISUAL_CPP_STUDENT
#define _INSIDE_VISUAL_CPP_STUDENT
class student:public CObject
{
DECLARE_SERIAL(student)
public:
CString m_strName;
int m_grade;
void Serialize(CArchive& ar);
student()
{
}
student(const char* szName,int nGrade):m_strName(szName)
{
m_grade=nGrade;
}
};
#endif
Student.cpp
#include "stdafx.h"
#include "student.h"
IMPLEMENT_SERIAL(student,CObject,0)
#ifdef DEBUG
void student::Serialize(CArchive& ar)
{
if(ar.IsStoring())
ar<
ar>>m_strName>>(int&)m_grade;
}
#endif
SDIDoc.h
#include "student.h"
public:
student m_student;
SDIDoc.cpp
CSDIDoc::CSDIDoc():m_student("VPLAB",0)
{
// TODO: add one-time construction code here
}
void CSDIDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
m_student.Serialize(ar);
}
BOOL CSDIDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
return TRUE;
}
BOOL CSDIDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
return CDocument::OnSaveDocument(lpszPathName);
}
SDIView.h
public:
void UpdateControlsFromdoc();
SDIView.cpp
void CSDIView::OnInitialUpdate()
{
UpdateControlsFromdoc();
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
void CSDIView::OnEnter()
{
CSDIDoc* p=GetDocument();
UpdateData(TRUE);
p->m_student.m_grade=m_grade;
p->m_student.m_strName=m_name;
}
void CSDIView::UpdateControlsFromdoc()
{
CSDIDoc* p=GetDocument();
m_grade=p->m_student.m_grade;
m_name=p->m_student.m_strName;
UpdateData(FALSE);
}
No comments:
Post a Comment