VC ++ technical insider (Fourth Edition) Note (chapter 2)

Source: Internet
Author: User

/*************************************** *****/

Chapter 4: Reading and Writing documents-SDI applications

1. serialization:

1. serialization: objects can be sustained, that is, they are stored on the disk when the program exits, and they can be restored when the program restarts. The process of storage and restoration of objects is called serialization.
Note: In the MFC Library, serialization cannot be used instead of the database management system. All objects related to documents can only be read and written sequentially in a single disk file, but do not support random read and write of objects in disk files.

2. disk files and archives ):
1) In the MFC Library, disk files are represented by cfile objects.
2) If the application does not directly use disk I/O, but only depends on the serialization process, you can avoid using the cfile object directly.
3) There is an archive object (carchive Class Object) between the serialize function (serialization function) and the cfile object. The Archive object is the cfile object cache data, and an internal mark is also saved, indicates whether to archive an archive (write disk) or load (read disk ).
4) each time only one activity archive is connected to a file. The application framework manages the creation of cfile objects and carchive objects, opens the corresponding disk files for the cfile objects, and connects the corresponding archive objects to the file objects.
5) relationship diagram:
Continuous Document Object <--> serialize <--> carchive object <--> cfile object <--> Disk
6) when the user selects the file open or File Save command, the application framework automatically calls the serialize function.

3. Make the class serializable:
1) serializable classes must be derived directly or indirectly from cobject, and must include the declare_serial macro call in the class declaration. The class implementation file must include the implement_serial macro call.
2) Compile the serialize function.
For example:
// Example for cobject: serialize
Void Cage: serialize (carchive & AR)
{
Cobject: serialize (AR );
If (AR. isstoring () // isstoring to determine whether the current archive is stored or loaded.
Ar <m_years;
Else
Ar> m_years;
}
NOTE 1: The insert operator is overloaded with values, and the extract operator is overloaded with applications. Sometimes it must be forcibly converted to adapt to the compiler:
For example, if m_ntype is an enumeration type, then: Ar >>( Int &) m_ntype; Ar <(INT) m_ntype;
Note 2: The insert and extract operators do not apply to embedded objects in the cobject derived class.
3) Most serialization functions call the serialize function of the base class (generally called in the first line, for example, cstudent is derived from cperson, in cstudent, the first line of the serialize function should be cperson: serialize (AR );). Cobject class serialize is a virtual function and does not do any work, SO 2) the class child does not call the cobject: serialize function.
4) The serialize function in the embedded object of the cobject derived class always needs to directly call the serialize function of the embedded object.

4. serialize the set:
1) All collection classes are derived from the cobject class and include the declare_serial macro call in the Collection class declaration. Therefore, you can call the serialize member function of the Collection class to serialize the collection.
2) If the serialize function of the coblist set composed of a group of objects is called, The serialize function of each object in the coblist set is called in sequence.
3) if the collection contains pointers to different class objects, all class names are stored in the archive accordingly, so that all objects can call the constructor of the corresponding class when they are created.
4) if an inclusive object (such as a document) contains an embedded set, the loaded data will be appended to the existing set. (It is necessary to clear the existing set before loading. Usually completed by the deletecontents function .)
5) when the cobject pointer set is loaded from the archive, each object in the set is processed as follows:
* Specifies the class of the object.
* Allocate Heap Storage space for each object.
× Load the object data to the newly applied memory.
* Saves the pointer of the new object to the set.
6) when the user selects the Save or Open menu item of the File menu, the application framework will then create the carchive object (and the internal cfile object), and then call the serialize function of the document class, and pass the reference of the carchive object to it. The serialize function of the derived document class then serializes each non-temporary data member.

Ii. SDI applications:

1. The specific startup steps of the sdi mfc application:
1) Windows loads the program into the memory.
2) construct the Global Object theapp. (When a program is loaded, all global objects are created immediately .)
3) Windows calls the global function winmain.
4) winmain automatically searches for the unique instance of the cwinapp derived class.
5) winmain calls theapp's initinstance function, which is overloaded in the derived application.
6) load the loaded initinstance function to start the document and display and process the main framework and View window.
7) winmain calls theapp's run member function to start the process of distributing window messages and command messages.
You can also overload other cwinapp member functions. When the application is aborted and all windows are closed, the exitinstance function is called.

2. The derived application class initinstance function can see the following code:
Csingledoctemplate * pdoctemplate;
Pdoctemplate = new csingledoctemplate (
Idr_mainframe,
Runtime_class (cmy15adoc ),
Runtime_class (cmainframe), // main SDI frame window
Runtime_class (cmy15aview ));
Adddoctemplate (pdoctemplate );
Use this set of code to establish the relationships among the program class, document class, View window class, and main framework class (note that there is a relationship between classes ).
(Call adddoctemplate to link all application elements)
Note:
1) The application object exists before the template is created, but the document, view, and framework object are not yet created. The application framework will dynamically create these objects as needed in the future, which is implemented by applying runtime_class.
2) Two diagrams on pages P338: the relationship between classes and objects passes over here. You can view them in an electronic version.
3) idr_mainframe is used to mark string table resources. The string marked by idr_mainframe is divided into substrings ending with "/N. These substrings appear in various places during application execution.
The idr_mainframe string can be found in the stringtable of the RC file. For the specific correspondence between strings, see the p339 page.

3. Multiple Views of SDI documents: Provides menu items for users to select a view and arranges multiple views in the split window.

4. Create an empty document ...... Cwinapp: onfilenew Function
After the adddoctemplate function is called, the application class indirectly calls the cwinapp member function onfilenew through the cwinapp: processshellcommand function to complete the following tasks:
1) construct a document object, but do not read data from the disk.
2) construct the main framework object and create a main framework window, but it is not displayed. The main frame window includes the idr_mainframe menu, toolbar, and status bar.
3) construct a view object and create a View window, but do not display it.
4) create a document. The relationships between the main framework and view objects (note that the relationships between objects)
5) Call the onnewdocument virtual member function of the Document Object. It calls the deletecontents virtual function.
6) Call the cview: oninitiaupdate virtual member function of the view object.
7) Call the cframewnd: activateframe virtual member function of the Framework object to display the main frame window with menus, views, and control bars.
Note: The preceding functions are called indirectly through the framework.

In SDI applications, documents, main frameworks, and view objects are created only once and will exist throughout the entire process of the program.
Cwinapp: The onfilenew function is called by the initinstance function. It is also called when the file new menu item is selected. (You do not need to construct the document framework view object at this time, instead, use the existing one to complete the last three steps ).
Note: The onfilenew function always calls the deletecontents function to clear the document.

5, cdocument: onnewdocument
If the SDI application does not re-use the same document object, it is unnecessary to use the onnewdocument function (because all document initialization can be completed in the document class constructor ). However, the onnewdocument function must be reloaded to initialize the Document Object every time you choose file new or file open.
Note:
Generally, do as little work as possible in the constructor. The less work the constructor does, the less chance of failure. Functions such as cdocument: onnewdocument and cview: oninitialupdate are good places to complete initialization.

6. Connect file open and serialized code ......... Onfileopen Function
When Appwizard creates an application, it automatically maps the File Open menu item to the onfileopen member function of cwinapp.
The onfileopen function further calls a group of functions to complete the following work:
1) prompt the user to select a file.
2) Call the cdocument: onopendocument virtual member function of an existing document object. This function will open the file, call the deletecontents function, create a carchive object for loading data, and then call the serialize function of the document to load data from the archive.
3) Call the oninitialupdate function of the view.

In addition to the file open menu, you can also select the list of recently used files. (The Framework records four recently used files and displays their names in the File menu. These file names are recorded in the Windows registry during program running.
Note:
You can change the number of recently used files. (Provide the appropriate parameters for the loadstdprofilesetting function in the initinstance function)
Cwinapp: loadstdprofilesettings
Void loadstdprofilesettings (uint nmaxmru = _ afx_mru_count );
// Nmaxmru: the number of recently used files to track.
// Call this member function from within the initinstance member function to enable and load the list of most recently used (MRU) files and last preview state. if nmaxmru is 0, no MRU list will be maintained.

7, cdocument: deletecontents
1) when the file new or file open menu item is selected, the cdocument: onnewdocument or cdocument: onopendocument function must call the cdocument: deletecontents function (clear existing Document Object content ).
That is, after a document object is created for the first time, cdocument: deletecontents will be called immediately. when the document is closed, it will be called again.
2) The deletecontents function is often overloaded in the derived class to clear the content of the document object.
3) cdocument: deletecontents called by the Framework to delete the document's data without destroying the cdocument object itself. 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 particle ly 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.

8. Connect File Save and File Save As with the serialized code:
1) When Appwizard creates an application, It maps the File Save menu item to the onfilesave member function of the cdocument class.
2) The onfilesave function calls the cdocument: onsavedocument function. The onsavedocument function uses an archive object (stored in) to call the document's sarialize function.
3) The File Save As menu is processed in a similar way. Ing cdocument: onfilesaveas function. The onfilesaveas function calls the onsavedocument function.
4) The necessary file management work for document storage is completed by the application framework.
Note:
File new and file open are mapped to application class member functions, while File Save and File Save As are mapped to document class member functions.

9. The 'blog' sign of the document
Many document-oriented applications track users' modifications to the document. When a user tries to close or exit the document, the application pop-up dialog box asks whether to save the document.
1) MFC supports this function through cdocument: m_bmodified. If the document is modified (expanded), m_bmodified = true; otherwise, it is false.
2) m_bmodified is of the protected type and can be accessed through the setmodifiedflag and ismodified member functions in the cdocument class. (Note: The m_bmodified data member of the cdocument class cannot be found in msdn, but the following definition can be found in tracking the cdocument class definition: protected: bool m_bmodified ;)
3) cdocument: setmodifiedflag
Void setmodifiedflag (bool bmodified = true );

Cdocument: ismodified
Bool ismodified ();

10. Program Registration
1) Use Appwizard to create step-4 in the program. Use the advanced option to add a file extension to the Program (or add it in idr_mainframe in stringtable)
2) Add the following lines of code to the initinstance function: registershellfiletypes (true); (Note that the added position is after the adddoctemplate Function !)

11. Allow drag and drop of programs:
If you want a running program to open a file that is dragged to it, you must call the dragacceptfiles function of cwnd in the main program framework window.
Add the following code to the initinstance function (the function is not): m_pmainwnd-> dragacceptfiles ();

//////////////////////////////////////// //////////
//////////////////////////////////////// /////////
//////////////////////////////////////// ///////

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.