MFC 文档串行化


参考:孙鑫C++视频第十三讲

一、建立一个串行化类的五个步骤(参考MSDN:CObject::Serilize)

       1、建立一个可串行化类,可串行化类都是从CObject继承而来

       2、重载Serialize成员函数

       3、在类声明中使用DECLARE_SERIAL宏

       4、定义一个没有参数的构造函数

       5、在实现文件中使用IMPLEMENT_SERIAL宏


二. 实例


Graph.h文件:

[cpp]  view plain  copy
  1. #pragma once  
  2.   
  3. // Graph 命令目标  
  4.   
  5. class Graph : public CObject  //(1)定义一个基类为COject的类  
  6. {  
  7.     DECLARE_SERIAL(Graph)  //(3)在类声明文件中使用DECLARE_SERIAL宏  
  8. public:  
  9.     Graph(); //(4)定义一个无参数的构造函数  
  10.     Graph(int drawType, CPoint ptOld);  
  11.     virtual ~Graph();  
  12.   
  13.     void Serialize(CArchive &ar); //(2)重写串行化函数  
  14.   
  15. private:  
  16.     int m_drawType;  
  17.     CPoint m_ptOld;  
  18. };  

Graph.cpp文件:

[cpp]  view plain  copy
  1. // Graph.cpp : 实现文件  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "Archive.h"  
  6. #include "Graph.h"  
  7.   
  8.   
  9. // Graph  
  10.   
  11. IMPLEMENT_SERIAL(Graph, CObject, 1) //(5)在实现文件中使用IMPLEMENT_SERIAL宏  
  12.   
  13. Graph::Graph() //(4)定义一个无参数的构造函数  
  14. {  
  15. }  
  16.   
  17. Graph::Graph(int drawType, CPoint ptOld)  
  18. {  
  19.     this->m_drawType = drawType;  
  20.     this->m_ptOld = ptOld;  
  21. }  
  22.   
  23. Graph::~Graph()  
  24. {  
  25. }  
  26.   
  27. // Graph 成员函数  
  28. void Graph::Serialize(CArchive &ar) //(2)重写串行化函数  
  29. {  
  30.     if (ar.IsStoring())  
  31.     {  
  32.         ar<<m_drawType<<m_ptOld;  
  33.     }  
  34.     else  
  35.     {  
  36.         ar>>m_drawType>>m_ptOld;  
  37.     }  
  38. }  



三、CArchive类

       CArchive类用来建立一个持久的disk storage.

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void CGraphicDoc::Serialize(CArchive& ar)  
  2. {  
  3.     POSITION pos = GetFirstViewPosition() ;  
  4.     CGraphicView *pView = (CGraphicView *)GetNextView(pos) ;  
  5.     if (ar.IsStoring())//往文件中写  
  6.     {  
  7.         // TODO: 在此添加存储代码  
  8.         /*UINT i = 10 ; 
  9.         TCHAR ch = 'a' ; 
  10.         CString str(TEXT("my string")) ; 
  11.         ar<<i<<ch<<str ;*/  
  12.         /*UINT count = pView->m_obArray.GetCount() ; 
  13.         ar<<count ; 
  14.         for(int i = 0 ; i < count ; ++ i) 
  15.         { 
  16.             ar<<pView->m_obArray.GetAt(i) ; 
  17.         }*/  
  18.     }  
  19.     else//从文件中读  
  20.     {  
  21.         // TODO: 在此添加加载代码  
  22.         /*UINT i ; 
  23.         TCHAR ch ; 
  24.         CString str ; 
  25.         CString strFormat ; 
  26.  
  27.         ar>>i>>ch>>str ; 
  28.          
  29.         strFormat.Format(TEXT("%d %c %s") , i , ch , str) ; 
  30.         AfxMessageBox(strFormat) ;*/  
  31.   
  32.         /*UINT count ; 
  33.         ar>>count ; 
  34.         CGraph *pGraph ; 
  35.         for(int i = 0 ; i < count ; ++ i) 
  36.         { 
  37.             ar>>pGraph ; 
  38.             pView->m_obArray.Add(pGraph) ; 
  39.         }*/  
  40.   
  41.     }  
  42.     m_obArray.Serialize(ar) ;//M_obArray的Serialize成员是基类的  
  43. }  
四、如何在一个类中访问另一个类的成员

      1、在Doc类中访问View类中成员获取View类指针

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. POSITION pos = GetFirstViewPosition() ;  
  2.     CGraphicView *pView = (CGraphicView *)GetNextView(pos) ;  

     2、在View类中获取Doc类指针

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. CGraphicDoc* pDoc = GetDocument();  


五、删除分配的堆内存

释放分配的堆内存放在DeleteContents 虚函数中

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void CGraphicDoc::DeleteContents()  
  2. {  
  3.     // TODO: 在此添加专用代码和/或调用基类  
  4.     UINT count = m_obArray.GetSize() ;  
  5.     for(int i = 0 ; i < count ; ++ i)  
  6.     {  
  7.         delete m_obArray.GetAt(i) ;  
  8.     }  
  9.   
  10.     m_obArray.RemoveAll() ;  
  11.   
  12.     CDocument::DeleteContents();  
  13. }  



猜你喜欢

转载自blog.csdn.net/u010901792/article/details/52919450
今日推荐