ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

文档与串行化

2021-07-05 13:08:35  阅读:201  来源: 互联网

标签:code void Serialize here ar 文档 串行化 TODO


1.CArchive写入和读取文件

void CGraphicView::OnFileWrite()

{

// TODO: Add your command handler code here

CFile file("1.txt",CFile::modeCreate | CFile::modeWrite);

CArchive ar(&file,CArchive::store);

int i=4;

char ch='a';

float f=1.3f;

CString str("http://www.sunxin.org");

ar<<i<<ch<<f<<str;

}

void CGraphicView::OnFileRead()

{

// TODO: Add your command handler code here

CFile file("1.txt",CFile::modeRead);

CArchive ar(&file,CArchive::load);

int i;

char ch;

float f;

CString str;

CString strResult;

ar>>i>>ch>>f>>str;

strResult.Format("%d,%c,%f,%s",i,ch,f,str);

MessageBox(strResult);

}

2.OnNewDocument函数

BOOL CGraphicDoc::OnNewDocument()

{

if (!CDocument::OnNewDocument())

return FALSE;

// TODO: add reinitialization code here

// (SDI documents will reuse this document)

// SetTitle("http://www.sunxin.org");

return TRUE;

}

//IDR_MAINFRAME字符串资源。CGraphicApp->InitInstance->

CSingleDocTemplate* pDocTemplate;

pDocTemplate = new CSingleDocTemplate(

IDR_MAINFRAME,

RUNTIME_CLASS(CGraphicDoc),

RUNTIME_CLASS(CMainFrame), // main SDI frame window

RUNTIME_CLASS(CGraphicView));

AddDocTemplate(pDocTemplate);

使用GetDocString函数获取IDR_MAINFRAME字符串资源的各个字串。

文档与串行化 - 铜豌豆 - 铜豌豆的博客

String Table中IDR_MAINFRAME字符串资源中各子串的含义:

(1)CDocTemplate::windowTitle,主窗口标题栏上的字符串,MDI程序不需要指定,将以IDR_MAINFRAME字符串为默认值。

(2)CDocTemplate::docName,缺省文档的名称。如果没有指定,缺省文档的名称是无标题。

(3)CDocTemplate::fileNewName,文档类型的名称。如果应用程序支持多种类型的文档,此字符串将显示在"File/New"对话框中。如果没有指定,就不能够在"File/New"对话框处理这种文件。

(4)CDocTemplate::filterName,文档类型的描述和一个适用于此类型的通配符过滤器。这个字符串将出现在“File/Open”对话框中的文件类型列表框中。要和CDocTemplate::filterExt一起使用。

(5)CDocTemplate::filterExt,文档的扩展名。如果没有指定,就不能够在“File/Open”对话框中处理这种文档。要和CDocTemplate::filterName一起使用。

(6)CDocTemplate::regFileTypeId,如果你以::RegisterShellFileTypes向系统的注册表注册文件类型,此值会出现在HEY_CLASSES_ROOT之下成为其子项,并仅供Windows内部使用。如果没有指定,这种文件类型就无法注册。

(7)CDocTemplate::regFileTypeName,这也是存储在注册表中的文件类型名称。它会显示于程序中用以访问注册表的对话框内。

CGraphicDoc::OnNewDocument函数被调用的内部运行过程。该函数在点击“新建”命令时被调用。

3.CGraphicDoc::Serialize函数

Serialize函数是用来保存和加载数据的函数。

void CGraphicDoc::Serialize(CArchive& ar)

{

if (ar.IsStoring())

{

// TODO: add storing code here

int i=5;

char ch='b';

float f=1.2f;

CString str("http://www.sunxin.org");

ar<<i<<ch<<f<<str;

}

else

{

// TODO: add loading code here

int i;

char ch;

float f;

CString str;

CString strResult;

ar>>i>>ch>>f>>str;

strResult.Format("%d,%c,%f,%s",i,ch,f,str);

AfxMessageBox(strResult);

}

}

*****让CGraph类支持串行化的操作*****

注:参考MSDN,CObject::Serialize->Serialization(Object persistence)->Serialization:Making a Serializable Class->…

① Deriving your class from CObject (or from some class derived from CObject).

将前面章节中的Graph类的头文件和源文件导入工程,让它继承CObject类:class CGraph:public CObject。

② Overriding the Serialize member function.

在Graph头文件中声明Serialize函数:

void Serialize(CArchive& ar);

在Graph源文件中实现Serialize函数:

void CGraph::Serialize(CArchive& ar)

{

if(ar.IsStoring())

{

ar<<m_nDrawType<<m_ptOrigin<<m_ptEnd;

}

else

{

ar>>m_nDrawType>>m_ptOrigin>>m_ptEnd;

}

}

③ Using the DECLARE_SERIAL macro in the class declaration.

在Graph头文件中声明SERIAL宏:DECLARE_SERIAL(CGraph)。

④ Defining a constructor that takes no arguments.

⑤ Using the IMPLEMENT_SERIAL macro in the implementation file for your class.

在构造函数前面加上IMPLEMENT_SERIAL(CGraph, CObject, 1 )。

*****串行化完毕*****

在Graph类中增加图形绘制的函数:

void CGraph::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 CGraphicView::OnDot()

{

// TODO: Add your command handler code here

m_nDrawType=1;

}

void CGraphicView::OnLine()

{

// TODO: Add your command handler code here

m_nDrawType=2;

}

void CGraphicView::OnRectangle()

{

// TODO: Add your command handler code here

m_nDrawType=3;

}

void CGraphicView::OnEllipse()

{

// TODO: Add your command handler code here

m_nDrawType=4;

}

void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

m_ptOrigin=point;

CView::OnLButtonDown(nFlags, point);

}

void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

CClientDC dc(this);

CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));

dc.SelectObject(pBrush);

switch(m_nDrawType)

{

case 1:

dc.SetPixel(point,RGB(0,0,0));

break;

case 2:

dc.MoveTo(m_ptOrigin);

dc.LineTo(point);

break;

case 3:

dc.Rectangle(CRect(m_ptOrigin,point));

break;

case 4:

dc.Ellipse(CRect(m_ptOrigin,point));

break;

}

CGraph *pGraph=new CGraph(m_nDrawType,m_ptOrigin,point);

//m_obArray.Add(pGraph);

CGraphicDoc *pDoc=GetDocument();

pDoc->m_obArray.Add(pGraph);

CView::OnLButtonUp(nFlags, point);

}

void CGraphicDoc::Serialize(CArchive& ar)

{

POSITION pos=GetFirstViewPosition();

CGraphicView *pView=(CGraphicView*)GetNextView(pos);

if (ar.IsStoring())

{

// TODO: add storing code here

}

else

{

// TODO: add loading code here

}

// pView->m_obArray.Serialize(ar);

m_obArray.Serialize(ar);

}

void CGraphicView::OnDraw(CDC* pDC)

{

CGraphicDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

// TODO: add draw code for native data here

int nCount;

//nCount=m_obArray.GetSize();

nCount=pDoc->m_obArray.GetSize();

for(int i=0;i

{

//((CGraph*)m_obArray.GetAt(i))->Draw(pDC);

((CGraph*)pDoc->m_obArray.GetAt(i))->Draw(pDC);

}

}

释放在堆上所分配的堆内存:

void CGraphicDoc::DeleteContents()

{

// TODO: Add your specialized code here and/or call the base class

int nCount;

nCount=m_obArray.GetSize();

while(nCount--)

{

delete m_obArray.GetAt(nCount);

m_obArray.RemoveAt(nCount);

}

CDocument::DeleteContents();

}

4. Document/View结构

在MFC中,文档类负责管理数据,提供保存和加载数据的功能。视类负责数据的显示,以及给用户提供对数据的编辑和修改功能。

MFC给我们提供Document/View结构,将一个应用程序所需要的“数据处理与显示”的函数空壳都设计好了,这些函数都是虚函数,我们可以在派生类中重写这些函数。有关文件读写的操作在CDocument的Serialize函数中进行,有关数据和图形显示的操作在CView的OnDraw函数中进行。我们在其派生类中,只需要去关注Serialize和OnDraw函数就可以了,其它的细节我们不需要去理会,程序就可以良好地运行。

当我们按下“File/Open”,Application Framework会激活文件打开对话框,让你指定文件名,然后自动调用CGraphicDoc::Serialize读取文件。Application Framework还会调用CGraphicView::OnDraw,传递一个显示DC,让你重新绘制窗口内容。

MFC给我们提供Document/View结构,是希望我们将精力放在数据结构的设计和数据显示的操作上,而不要把时间和精力花费在对象与对象之间、模块与模块之间的通信上。

一个文档对象可以和多个视类对象相关联,而一个视类对象只能和一个文档对象相关联。

 

标签:code,void,Serialize,here,ar,文档,串行化,TODO
来源: https://blog.51cto.com/liyuanjinglyj/2978839

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有