//Paints.h #pragma once // CPaints 命令目标 class CPaints : public CObject { public: CPaints(); virtual ~CPaints(); CPaints(UINT type,CPoint start,CPoint end); void Serialize(CArchive& ar); void Draw(CDC* pDC); private: UINT m_nDrawType; CPoint m_ptOrigin; CPoint m_ptEnd; DECLARE_SERIAL(CPaints) };
//Paints.cpp // Paints.cpp : 实现文件 // #include "stdafx.h" #include "mfcArchive.h" #include "Paints.h" IMPLEMENT_SERIAL(CPaints,CObject,1) // CPaints CPaints::CPaints() { m_nDrawType = 0; } CPaints::~CPaints() { } // CPaints 成员函数 CPaints::CPaints(UINT type,CPoint start,CPoint end) { m_nDrawType = type; m_ptOrigin = start; m_ptEnd = end; } void CPaints::Serialize(CArchive& ar) { if(ar.IsStoring() ) { ar<<m_nDrawType<<m_ptOrigin<<m_ptEnd; } else { ar>>m_nDrawType>>m_ptOrigin>>m_ptEnd; } } void CPaints::Draw(CDC* pDC) { CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH)); CBrush *pOldBrush = pDC->SelectObject(pBrush); switch(m_nDrawType) { case 1: pDC->SetPixel(m_ptEnd,RGB(0,0,0)); break; case 2: pDC->MoveTo(m_ptOrigin); pDC->LineTo(m_ptEnd); break; case 3: pDC->Rectangle(CRect(m_ptOrigin,m_ptEnd)); break; case 4: pDC->Ellipse(CRect(m_ptOrigin,m_ptEnd)); break; } pDC->SelectObject(pOldBrush); }
void CmfcArchiveDoc::Serialize(CArchive& ar) { POSITION ps = GetFirstViewPosition(); CmfcArchiveView* pView = (CmfcArchiveView*)GetNextView(ps); if (ar.IsStoring()) { // TODO: 在此添加存储代码 int a = 5; char b = 'c'; CString str("load test"); ar<<a<<b<<str; // int nCount = pView->m_obArray.GetSize(); ar<<nCount; for(int i=0;i<nCount;i++) { ar<<pView->m_obArray.GetAt(i); } } else { // TODO: 在此添加加载代码 int a; char b; CString str,strResult; ar>>a>>b>>str; strResult.Format("%d,%c,%s",a,b,str); //AfxMessageBox(strResult); int nCount; CPaints *ps; ar>>nCount; for(int i=0;i<nCount;i++) { ar>>ps; pView->m_obArray.Add(ps); } } }............................................................................
void CmfcArchiveView::OnDraw(CDC* pDC) { CmfcArchiveDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: 在此处为本机数据添加绘制代码 int nCount; nCount = m_obArray.GetSize(); for(int i=0;i<nCount;i++) { ( (CPaints*)m_obArray.GetAt(i) )->Draw(pDC); } }