文档与串行化

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;

}

<wbr></wbr>

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;

<wbr></wbr>

// TODO: add reinitialization code here

// (SDI documents will reuse this document)

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

return TRUE;

}

<wbr></wbr>

//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);

<wbr></wbr>

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

<wbr></wbr>

<wbr></wbr>

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

<wbr></wbr>

<wbr></wbr>

String TableIDR_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,这也是存储在注册表中的文件类型名称。它会显示于程序中用以访问注册表的对话框内。

<wbr></wbr>

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

<wbr></wbr>

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);

}

}

<wbr></wbr>

<wbr></wbr>

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

注:参考MSDNCObject::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);

}

<wbr></wbr>

如何拷贝一个资源:将菜单资源中的绘图菜单拷贝到我们的工程中。

。。。。。。

void CGraphicView::OnDot()

{

// TODO: Add your command handler code here

m_nDrawType=1;

}

<wbr></wbr>

void CGraphicView::OnLine()

{

// TODO: Add your command handler code here

m_nDrawType=2;

}

<wbr></wbr>

void CGraphicView::OnRectangle()

{

// TODO: Add your command handler code here

m_nDrawType=3;

}

<wbr></wbr>

void CGraphicView::OnEllipse()

{

// TODO: Add your command handler code here

m_nDrawType=4;

}

<wbr></wbr>

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);

}

<wbr></wbr>

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);

<wbr></wbr>

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);

<wbr></wbr>

CView::OnLButtonUp(nFlags, point);

}

<wbr></wbr>

void CGraphicDoc::Serialize(CArchive& ar)

{

POSITION pos=GetFirstViewPosition();

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

if (ar.IsStoring())

{

// TODO: add storing code here

<wbr></wbr>

}

else

{

// TODO: add loading code here

<wbr></wbr>

}

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

m_obArray.Serialize(ar);

}

<wbr></wbr>

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);

}

}

<wbr></wbr>

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

void CGraphicDoc::DeleteContents()

{

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

int nCount;

nCount=m_obArray.GetSize();

<wbr></wbr>

while(nCount--)

{

delete m_obArray.GetAt(nCount);

m_obArray.RemoveAt(nCount);

}

CDocument::DeleteContents();

}

<wbr></wbr>

4. Document/View结构

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

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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值