写了一个绘制简单图形的SDI程序,基本图元类通过基类派生,绘制的图元存放在文档类中的CObArray中。定义了图元基类如下:
class CShape:public CObject
{
public:
COLORREF m_pColor;
int m_pWidth,m_pStyle;
DECLARE_SERIAL(CShape)
bool IsSelected;
CPoint m_pStart,m_pEnd;
CShape *in,*out;
void SetColor(COLORREF color);
virtual void Draw(CDC *pDC){}
virtual void ShowProperty(){}
virtual void SetText(int t){}
CShape();
virtual ~CShape();
void Serialize(CArchive &ar);
};
序列化函数如下:
void CShape::Serialize(CArchive &ar)
{
CObject::Serialize(ar);
if(ar.IsStoring()){
}
else{
}
}图元派生类(矩形,其余类似)如下:
class CRectangle:public CShape
{
protected:
CString Text;
LOGBRUSH MyBrush;
CRectangle();
DECLARE_SERIAL(CRectangle)
public:
CShape *in2;
CRectangle(CPoint pFP,CPoint pSP,int Width,int Style,COLORREF Color);
void Draw(CDC *pDC);
void SetText(int t);
void ShowProperty();
void Serialize(CArchive &ar);
};
序列化函数如下:
void CRectangle::Serialize(CArchive &ar)
{ CObject::Serialize(ar);
if(ar.IsLoading())
ar>>m_pWidth>>m_pStyle>>m_pColor>>m_pStart.x>>m_pStart.y>>m_pEnd.x>>m_pEnd.y>>Text>>MyBrush.lbColor>>MyBrush.lbHatch>>MyBrush.lbStyle;
else
ar<<m_pWidth<<m_pStyle<<m_pColor<<m_pStart.x<<m_pStart.y<<m_pEnd.x<<m_pEnd.y<<Text<<MyBrush.lbColor<<MyBrush.lbHatch<<MyBrush.lbStyle;}
将在屏幕上绘制的图形存入文档类中的CObArray中,序列化函数如下:void CFlowDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
m_ObArray.Serialize(ar);
}
文档类中其他函数如下:
BOOL CFlowDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE; // TODO: add reinitialization code here
// (SDI documents will reuse this document)
DeleteContents();
SetModifiedFlag(false);
return TRUE;
}void CFlowDoc::DeleteContents()
{
// TODO: Add your specialized code here and/or call the base class
int index;
index=m_ObArray.GetSize();
while(index--)
delete m_ObArray.GetAt(index);
m_ObArray.RemoveAll();
CDocument::DeleteContents();
UpdateAllViews(NULL);}程序其他都正常,但是在绘制图形保存后再重新打开,绘制的图形没有任何显示。DEBUG后发现绘制的图形在序列化时没有被保存下来,但不知道是序列化时哪部分出问题。各位高手帮忙指点一下,谢谢了!
class CShape:public CObject
{
public:
COLORREF m_pColor;
int m_pWidth,m_pStyle;
DECLARE_SERIAL(CShape)
bool IsSelected;
CPoint m_pStart,m_pEnd;
CShape *in,*out;
void SetColor(COLORREF color);
virtual void Draw(CDC *pDC){}
virtual void ShowProperty(){}
virtual void SetText(int t){}
CShape();
virtual ~CShape();
void Serialize(CArchive &ar);
};
序列化函数如下:
void CShape::Serialize(CArchive &ar)
{
CObject::Serialize(ar);
if(ar.IsStoring()){
}
else{
}
}图元派生类(矩形,其余类似)如下:
class CRectangle:public CShape
{
protected:
CString Text;
LOGBRUSH MyBrush;
CRectangle();
DECLARE_SERIAL(CRectangle)
public:
CShape *in2;
CRectangle(CPoint pFP,CPoint pSP,int Width,int Style,COLORREF Color);
void Draw(CDC *pDC);
void SetText(int t);
void ShowProperty();
void Serialize(CArchive &ar);
};
序列化函数如下:
void CRectangle::Serialize(CArchive &ar)
{ CObject::Serialize(ar);
if(ar.IsLoading())
ar>>m_pWidth>>m_pStyle>>m_pColor>>m_pStart.x>>m_pStart.y>>m_pEnd.x>>m_pEnd.y>>Text>>MyBrush.lbColor>>MyBrush.lbHatch>>MyBrush.lbStyle;
else
ar<<m_pWidth<<m_pStyle<<m_pColor<<m_pStart.x<<m_pStart.y<<m_pEnd.x<<m_pEnd.y<<Text<<MyBrush.lbColor<<MyBrush.lbHatch<<MyBrush.lbStyle;}
将在屏幕上绘制的图形存入文档类中的CObArray中,序列化函数如下:void CFlowDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
m_ObArray.Serialize(ar);
}
文档类中其他函数如下:
BOOL CFlowDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE; // TODO: add reinitialization code here
// (SDI documents will reuse this document)
DeleteContents();
SetModifiedFlag(false);
return TRUE;
}void CFlowDoc::DeleteContents()
{
// TODO: Add your specialized code here and/or call the base class
int index;
index=m_ObArray.GetSize();
while(index--)
delete m_ObArray.GetAt(index);
m_ObArray.RemoveAll();
CDocument::DeleteContents();
UpdateAllViews(NULL);}程序其他都正常,但是在绘制图形保存后再重新打开,绘制的图形没有任何显示。DEBUG后发现绘制的图形在序列化时没有被保存下来,但不知道是序列化时哪部分出问题。各位高手帮忙指点一下,谢谢了!
谢谢!