zoukankan      html  css  js  c++  java
  • 文档与串行化

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

    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类支持串行化的操作*****

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

    }

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

    。。。。。。

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

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

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

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

  • 相关阅读:
    为什么重写equals方法还要重写hashcode方法?
    提高数据库处理查询速度
    ibatis缓存初探(1)
    java四种数组排序
    前台将勾选的多个属性放到一个value里面,是一个字符串,传到后台
    Apache与Tomcat整合
    web服务器和应用服务器概念比较
    ibaits与spring整合的心得
    spring3.0的jar包详解
    JAVA:23种设计模式详解(转)2
  • 原文地址:https://www.cnblogs.com/liyuanjinglyj/p/4656629.html
Copyright © 2011-2022 走看看