Serialization of data
--Example program: Memo
Create a new single document SDI application, the view class selects CFormView so that users can enter them in the window. Create three edit boxes in the interface, and then add three corresponding edit box variables. These three variables are member variables of the view class, and in order to interact with the data, three corresponding variables are also created in the document class. Both the document class and the view class then initialize the data member, which is typically done in the onnewdocument () function in the document class. The document class OnNewDocument () function executes when any of the following actions occur:
When the user launches the application;
When the user selects the "New" option from the "File" menu;
The initialization of a view class is usually held by OnInitialUpdate (), and the code triggers the View class OnInitialUpdate () function execution when any of the following actions occur:
When the user launches the application;
When the user selects the "New" option from the "File" menu;
When the user selects the "Open" option from the "File" menu;
The way to get a document class pointer in a view class is: cfoodoc* PDoc = Gerdocument ();
Use this document pointer to manipulate document class data: M_viewdata = pdoc->m_docdata;
The serialized code is simple, and AR is a Document object (CArchive object) corresponding to the file selected by the user:
// CFooDoc 序列化
void CFooDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// 将数据写入文件
ar << m_DocData;
}
else
{
// 从文件中读取数据
ar >> m_DocData;
}
}
This writes the data to the file, selecting Save or Save as in the "File" menu to serialize the data. If you do not save the data, the exit program prompts the user to save the modified data. Please refer to the source code for specific details.