line.h
#pragma once
#include "pch.h"
//
//CArchive串行化,序列化
//
//1.编写可序列化的类,继承自cobject
//2.DECLARE_SERIAL 声明
//3.serialize 函数
//4.IMPLEMENT_SERIAL
//可序列化类的版本号
class CLine :public CObject
{
DECLARE_SERIAL(CLine)
public:
CPoint m_ptFrom;
CPoint m_ptTo;
COLORREF m_clrLine;
public:
CLine(){}
CLine(CPoint from, CPoint to)
{
m_ptFrom = from;
m_ptTo = to;
}
CLine(CPoint from, CPoint to,COLORREF color)
{
m_ptFrom = from;
m_ptTo = to;
m_clrLine = color;
}
void Serialize(CArchive &ar);
};
line.cpp
#include "pch.h"
#include "line.h"
IMPLEMENT_SERIAL(CLine, CObject, 2|VERSIONABLE_SCHEMA); //版本化的,di2个版本
void CLine::Serialize(CArchive &ar)
{
CObject::Serialize(ar);
if (ar.IsStoring())
{
ar << m_ptFrom << m_ptTo<<m_clrLine;
}
else
{
UINT nSchema = ar.GetObjectSchema();
switch (nSchema)
{
case 1:
ar >> m_ptFrom >> m_ptTo;
m_clrLine = RGB(0, 0, 0);
break;
case 2:
ar >> m_ptFrom >> m_ptTo>> m_clrLine;