.H:
class CShape : public CObject
{
public:
CShape(void);
CShape(CShape& other);
~CShape(void);
// 是否边框
bool m_bBorder;
// 是否填充
bool m_bFill;
// 边框颜色
COLORREF m_borderColor;
// 边框粗细
int m_nBorderWidth;
// 填充颜色
COLORREF m_fillColor;
// 中心点
CRect m_rect;
virtual CString getType(void);
protected:
CString m_strType;
public:
void Serialize(CArchive &ar);//序列化函数
DECLARE_SERIAL(CShape)//声明序列化类
};
/////////////////////////////////////////////////////////////////////////////
.cpp:
IMPLEMENT_SERIAL(CShape,CObject,1)//实现序列化类
CShape::CShape(void)
: m_bBorder(false)
, m_bFill(false)
, m_nBorderWidth(0)
, m_rect()
, m_strType(_T("Shape"))
{
}
CShape::CShape(CShape& other)
{
this->m_strType = other.m_strType;
this->m_bBorder = other.m_bBorder;
this->m_bFill = other.m_bFill;
this->m_borderColor = other.m_borderColor;
this->m_nBorderWidth = other.m_nBorderWidth;
this->m_fillColor = other.m_fillColor;
this->m_rect = other.m_rect;
}
CShape::~CShape(void)
{
}
CString CShape::getType(void)
{
return m_strType;
}
void CShape::Serialize(CArchive &ar)//序列化函数的实现
{
if(ar.IsStoring())
//存入
ar<<m_strType<<m_bBorder<<m_bFill<<m_nBorderWidth<<m_borderColor<<m_fillColor<<m_rect;
else
//输出
ar>>m_strType>>m_bBorder>>m_bFill>>m_nBorderWidth>>m_borderColor>>m_fillColor>>m_rect;
}
///////////////////////////////////////////////////////////////////////////////////
使用CTypedPtrArray存储,注意数据尽量保存在DOC里面:
CTypedPtrArray<CObArray,CShape *> m_ShapeArray;
在CDoc的Serialize 里面:
if (ar.IsStoring())
{
m_ShapeArray.Serialize(ar);
}
else
{
m_ShapeArray.Serialize(ar);
}
///////////////////////////////////////
数据修改后记得进行标记:
SetModifiedFlag();