文档与串行化小结

来源:互联网 发布:发报机 淘宝 编辑:程序博客网 时间:2024/05/16 05:50

    数据持久性-串行化

     

    Constructs a CArchive object and specifies whether it will be used for loading or storing objects.

    CArchive(
       CFile* pFile,
       UINT nMode,
       int nBufSize = 4096,
       void* lpBuf = NULL
    );

     

    nMode

    A flag that specifies whether objects will be loaded from or stored to the archive. The nMode parameter must have one of the following values:

    • CArchive::load   Loads data from the archive. Requires only CFile read permission.
    • CArchive::store   Saves data to the archive. Requires CFile write permission.
    • CArchive::bNoFlushOnDelete   Prevents the archive from automatically calling Flush when the archive destructor is called. If you set this flag, you are responsible for explicitly calling Close before the destructor is called. If you do not, your data will be corrupted.

     

    CArchive does not have a base class.

    Later you can load the objects from persistent storage, reconstituting them in memory. This process of making data persistent is called "serialization."

    You can think of an archive object as a kind of binary stream. Like an input/output stream, an archive is associated with a file and permits the buffered writing and reading of data to and from storage. An input/output stream processes sequences of ASCII characters, but an archive processes binary object data in an efficient, nonredundant format.

    You must create a CFile object before you can create a CArchive object. In addition, you must ensure that the archive's load/store status is compatible with the file's open mode. You are limited to one active archive per file.

    When you construct a CArchive object, you attach it to an object of class CFile (or a derived class) that represents an open file. You also specify whether the archive will be used for loading or storing. A CArchive object can process not only primitive types but also objects of CObject-derived classes designed for serialization. A serializable class usually has a Serialize member function, and it usually uses the DECLARE_SERIAL and IMPLEMENT_SERIAL macros, as described under class CObject.

    The overloaded extraction (>>) and insertion (<<) operators are convenient archive programming interfaces that support both primitive types and CObject-derived classes.

    CArchive also supports programming with the MFC Windows Sockets classes CSocket and CSocketFile. The IsBufferEmpty member function supports that usage.

     

    文件读取代码:

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

     

    文件写入代码:

    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("Test Archive Usage");

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

     

     

    设置文档标题

    在OnNewDocument中调用SetTitle("新文档标题");

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

    Retrieves a string associated with the document type.

    virtual BOOL GetDocString(
       CString& rString,
       enum DocStringIndex index
    ) const;

     

  1. (1)CDocTemplate::windowTitle,主窗口标题栏上的字符串,MDI程序不需要指定,将以IDR_MAINFRAME字符串为默认值。
  2. (2)CDocTemplate::docName,缺省文档的名称。如果没有指定,缺省文档的名称是无标题。
  3. (3)CDocTemplate::fileNewName,文档类型的名称。如果应用程序支持多种类型的文档,此字符串将显示在"File/New"对话框中。如果没有指定,就不能够在"File/New"对话框处理这种文件。
  4. (4)CDocTemplate::filterName,文档类型的描述和一个适用于此类型的通配符过滤器。这个字符串将出现在File/Open对话框中的文件类型列表框中。要和CDocTemplate::filterExt一起使用。
  5. (5)CDocTemplate::filterExt,文档的扩展名。如果没有指定,就不能够在File/Open对话框中处理这种文档。要和CDocTemplate::filterName一起使用。
  6. (6)CDocTemplate::regFileTypeId,如果你以::RegisterShellFileTypes向系统的注册表注册文件类型,此值会出现在HEY_CLASSES_ROOT之下成为其子项,并仅供Windows内部使用。如果没有指定,这种文件类型就无法注册。
  7. (7)CDocTemplate::regFileTypeName,这也是存储在注册表中的文件类型名称。它会显示于程序中用以访问注册表的对话框内。
  8.  

  9.  
  10. POSITION   A value used to denote the position of an element in a collection; used by MFC collection classes.

    Call this function to get the position of the first view in the list of views associated with the document.

    virtual POSITION GetFirstViewPosition( ) const;

    Call this function to iterate through all of the document's views.

    virtual CView* GetNextView(
       POSITION& rPosition
    ) const;

     

     

    在文档中得到视图类对象的指针

    POSITION pos=GetFirstViewPosition();

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

     

     

    Document/View结构

  11. MFC中,文档类负责管理数据,提供保存和加载数据的功能视类负责数据的显示,以及给用户提供对数据的编辑和修改功能
  12. MFC给我们提供Document/View结构,将一个应用程序所需要的数据处理与显示函数空壳都设计好了,这些函数都是虚函数,我们可以在派生类中重写这些函数。有关文件读写的操作在CDocumentSerialize函数中进行,有关数据和图形显示的操作在CViewOnDraw函数中进行。我们在其派生类中,只需要去关注SerializeOnDraw函数就可以了,其它的细节我们不需要去理会,程序就可以良好地运行。
  13. 当我们按下File/OpenApplication Framework会激活文件打开对话框,让你指定文件名,然后自动调用CGraphicDoc::Serialize读取文件。Application Framework还会调用CGraphicView::OnDraw,传递一个显示DC,让你重新绘制窗口内容。
  14. MFC给我们提供Document/View结构,是希望我们将精力放在数据结构的设计和数据显示的操作上,而不要把时间和精力花费在对象与对象之间、模块与模块之间的通信上。
  15. 一个文档对象可以和多个视类对象相关联,而一个视类对象只能和一个文档对象相关联
  16.  

    在文件的打开和销毁以及重复使用都会调用DeleteContents。需要在文档类中重载该虚函数,在其中销毁在堆上分配的内存。

     

    Called by the framework to delete the document's data without destroying the CDocument object itself.

    virtual void DeleteContents( );

     

    It is called just before the document is to be destroyed. It is also called to ensure that a document is empty before it is reused. This is particularly important for an SDI application, which uses only one document; the document is reused whenever the user creates or opens another document. Call this function to implement an "Edit Clear All" or similar command that deletes all of the document's data. The default implementation of this function does nothing. Override this function to delete the data in your document.

     

 
原创粉丝点击