程序是MFC的Single Document应用,功能是用mouse画线条图形,能够保存和读取,现在的问题是用MFC系列化保存时自定义类CLine中的属性没有写进文件中,下面是类的定义// Line.h: interface for the CLine class.
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_LINE_H__47AA0B0A_2FEF_47CC_83CD_41B1147A8AB2__INCLUDED_)
#define AFX_LINE_H__47AA0B0A_2FEF_47CC_83CD_41B1147A8AB2__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000class CLine : public CObject
{
DECLARE_SERIAL (CLine)
public:
void Serical(CArchive &ar);
void Draw(CDC *pDC);
CLine(CPoint ptForm, CPoint ptTo, COLORREF crColor);
CLine();
virtual ~CLine();private:
COLORREF m_crColor;
CPoint m_ptTo;
CPoint m_ptForm;
};#endif // !defined(AFX_LINE_H__47AA0B0A_2FEF_47CC_83CD_41B1147A8AB2__INCLUDED_)
// Line.cpp: implementation of the CLine class.
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "SingleDocument.h"
#include "Line.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
IMPLEMENT_SERIAL (CLine, CObject, 1)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CLine::CLine()
{}CLine::~CLine()
{}CLine::CLine(CPoint ptForm, CPoint ptTo, COLORREF crColor)
{
m_ptForm = ptForm;
m_ptTo = ptTo;
m_crColor = crColor;
}void CLine::Draw(CDC *pDC)
{
CPen lPen(PS_SOLID, 1, m_crColor); CPen* pOldPen = pDC->SelectObject(&lPen);
pDC->MoveTo(m_ptForm);
pDC->LineTo(m_ptTo);
pDC->SelectObject(pOldPen);
}void CLine::Serical(CArchive &ar)
{
CObject::Serialize(ar); if (ar.IsStoring())
{
ar << m_ptForm << m_ptTo << (DWORD)m_crColor;
}
else
{
ar >> m_ptForm >> m_ptTo >> (DWORD)m_crColor;
}
}类CSingleDocumentDoc中有以下定义,用来存储线的集合
public:
CObArray m_oaLines;
下面是Serialize方法
void CSingleDocumentDoc::Serialize(CArchive& ar)
{
m_oaLines.Serialize(ar);
}
程序的其它部分是已经完成的,只有保存m_oaLines不成功
下面是保存文件部分内容,其它内容和第二行完全一样
00000000h: EF 00 FF FF 01 00 05 00 43 4C 69 6E 65 01 80 01 ;
00000010h: 80 01 80 01 80 01 80 01 80 01 80 01 80 01 80 01 ;
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_LINE_H__47AA0B0A_2FEF_47CC_83CD_41B1147A8AB2__INCLUDED_)
#define AFX_LINE_H__47AA0B0A_2FEF_47CC_83CD_41B1147A8AB2__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000class CLine : public CObject
{
DECLARE_SERIAL (CLine)
public:
void Serical(CArchive &ar);
void Draw(CDC *pDC);
CLine(CPoint ptForm, CPoint ptTo, COLORREF crColor);
CLine();
virtual ~CLine();private:
COLORREF m_crColor;
CPoint m_ptTo;
CPoint m_ptForm;
};#endif // !defined(AFX_LINE_H__47AA0B0A_2FEF_47CC_83CD_41B1147A8AB2__INCLUDED_)
// Line.cpp: implementation of the CLine class.
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "SingleDocument.h"
#include "Line.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
IMPLEMENT_SERIAL (CLine, CObject, 1)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CLine::CLine()
{}CLine::~CLine()
{}CLine::CLine(CPoint ptForm, CPoint ptTo, COLORREF crColor)
{
m_ptForm = ptForm;
m_ptTo = ptTo;
m_crColor = crColor;
}void CLine::Draw(CDC *pDC)
{
CPen lPen(PS_SOLID, 1, m_crColor); CPen* pOldPen = pDC->SelectObject(&lPen);
pDC->MoveTo(m_ptForm);
pDC->LineTo(m_ptTo);
pDC->SelectObject(pOldPen);
}void CLine::Serical(CArchive &ar)
{
CObject::Serialize(ar); if (ar.IsStoring())
{
ar << m_ptForm << m_ptTo << (DWORD)m_crColor;
}
else
{
ar >> m_ptForm >> m_ptTo >> (DWORD)m_crColor;
}
}类CSingleDocumentDoc中有以下定义,用来存储线的集合
public:
CObArray m_oaLines;
下面是Serialize方法
void CSingleDocumentDoc::Serialize(CArchive& ar)
{
m_oaLines.Serialize(ar);
}
程序的其它部分是已经完成的,只有保存m_oaLines不成功
下面是保存文件部分内容,其它内容和第二行完全一样
00000000h: EF 00 FF FF 01 00 05 00 43 4C 69 6E 65 01 80 01 ;
00000010h: 80 01 80 01 80 01 80 01 80 01 80 01 80 01 80 01 ;
CObArray 有没有呢?是否真正调用了CLine::Serialize呢?
{
ASSERT_VALID(this); CObject::Serialize(ar); if (ar.IsStoring())
{
ar.WriteCount(m_nSize);
for (int i = 0; i < m_nSize; i++)
ar << m_pData[i];
}
else
{
DWORD nOldSize = ar.ReadCount();
SetSize(nOldSize);
for (int i = 0; i < m_nSize; i++)
ar >> m_pData[i];
}
}
{ if (ar.IsStoring())
{
int size=m_oaLines.GetCount();
ar << size;
for (int i = 0; i < size; i++)
(CLine*)m_oaLines.GetAt(i)->Serialize(ar);
}
else
{
DWORD size=0 ;
ar >>size;
m_oaLines.RemoveAll();
for (int i = 0; i < size; i++)
{
CLine *pLine=new CLine();
pLine->Serialize(ar);
m_oaLines.Insert(i,pLine,1);
}
}
不过我已经找出原因了,
这是CLine的系列化方法名定义的不正确引起的!
Serical > Serialize