Detailed description of VC menu commands (opening, saving, and closing files)

Source: Internet
Author: User

Part 1:

Five command IDs: processing functions
Id_file_new cwinapp: onfilenew
Id_file_open cwinapp: onfileopen
Id_file_save cdocument: onfilesave
Id_file_saveas cdocument: onfilesaveas
Id_file_close cdocument: onfileclose

1. id_file_new
Cwinapp: onfilenew call cdocmanager: onfilenew.
|
Cdocmanager: onfilenew determines whether there are more than one document template. The document type dialog box (afx_idd_newtypedlg) is displayed)
Select the document type to create. Then call cdoctemplate: opendocumentfile (null ).
|
Cdoctemplate: opendocumentfile (maid = true) for SDI and MDI
Processing is different.

For SDI, 1, if an existing document is opened and reinitialized, call cdocument: savemodified () to save the current document. If no
If a document exists, call createnewdocument () to create the document object, and then call createnewframe (pdoucment, null) to create the document.
The Framework Window of the document. 2. If lpszpathname is null, call cdocument: onnewdocument () to process the new document.
Pdocument-> setpathname (lpszpathname) to set the document path. 3. Check whether the main framework window of the current thread exists and does not exist.
Then, the new framework created in 1 is used as the main framework. 4. Call initialupdateframe to display the frame window.

For MDI, it is basically the same as SDI, but there is no step 3rd.
|
Cdocument: onnewdocument () First, call deletecontents () to delete the original document content, and use m_strpathname.empty ()
Clear the current document path, setmodifiedflag (false ).

2. id_file_open
Cwinapp: onfileopen call cdocmanager: onfileopen.
|
Cdocmanager: onfileopen first displays the file opening dialog box (afx_ids_openfile), and then calls cwinapp ::
Opendocumentfile (filename ).
|
Cwinapp: opendocumentfile (filename) calls cdocmanager: opendocumentfile.
|
Cdocmanager: opendocumentfile (lpctstr lpszfilename) traverses the document template and uses
Matchdoctype (szpath, popendocument) matches the document type. Matching is mainly based on the file extension. If the file already exists in
If a document is opened, the first view of the document is activated. Otherwise, use the matching document template pbesttemplate-> opendocumentfile.
(Szpath ).
|
Cdoctemplate: opendocumentfile call cdocument: onopendocument to open the file.
|
Cdocument: onopendocument open the file, use deletecontents to delete the existing document content, and create the carchive
The object loadarchive, serialize (loadarchive) reads the file content, and setmodifiedflage (false ).
MDI: New Frame, new view?
SDI: Update view?

3. id_file_save
Cdocument: onfilesave call cdocument: dofilesave ().
|
Cdocument: onfilesave checks whether the file is read-only. If yes, dosave (null); otherwise, dosave (m_strpathname ).
|
The process of cdocument: dosave (lpctstr lpszpathname, bool breplace = true) is: 1. Check whether lpszpathname is empty.
The second step is skipped. If it is null, the path name of the document is obtained and an extension is added. The Save file dialog box (afx_ids _
Otherwise, the copy and save dialog box (afx_ids + savefilecopy) is displayed ). 2. Call cdocument: onsavedocument to save
Save the document. 3. If breplace = true, setpathname (newname) overwrites the current path name.
|
Cdocument: onsavedocument open the file, create the carchive object savearchive, serialize (savearchive) read and write
File, setmodifiedflag (false ).

4. id_file_saveas
Cdocument: onfilesave calls dosave (null ).

5. id_file_close
Cdocument: onfileclose call savemodified () to save the file, and then use onclosedocument to process the document.
|
Cdocument: savemodified () Use ismodified () to determine whether to modify the file. If yes, the file storage dialog box (afx_idp_ask_to _) is displayed _
Save). If you select "yes", call dofilesave () to save the file.
|
Cdocument: onclosedocument () First destroys the document framework, and then deletes the document content using deletecontents (). If M _
Bautodelete: delete this.

Part 2:

MDI file opening process
Cwinapp: onfileopen ()
{
Cdocmanager: onfileopen ()
{
Cdocmanager: dopromptfilename ()
{
Cfiledialog: domodal ();
}
Cwinapp: opendocumentfile ()
{
... Select a document template;
... Adjust the view and framework;
Cdoctemplate: opendocumentfile ();
{
Determine whether there are existing documents and whether to save them
Create framework
To determine whether a file exists, call cmydoc: onopendocunment/onnewdocument
}
}
}
}
----------------------------------------
Document/view command processing process
Id_file_new
Cwinapp: onfilenew ()
{
Cdocmanager: onfilenew ()
{
If (no document template is available)
Return;
If (there are multiple document templates)
{
The pop-up dialog box allows users to select;
Get template pointer;
}
Cmultidoctemplate: opendocumentfile ()
{
A new document;
Create a sub-framework;
Build the relationship between frame, Doc, and view;
Cdocument: onnewdocument ()
{
Deletecontents ();
}
Initialupdateframe ();
Return pdoc;
}
}
}

Id_file_open
Cwinapp: onfileopen ()
{
Cdocmanager: onfileopen ()
{
Cdocmanager: dopromptfilename ()
{
Cfiledialog: domodal ();
}
Cwinapp: opendocumentfile ()
{
... Select a document template;
... Adjust the view and framework;
Cdoctemplate: opendocumentfile ();
{
Determine whether there are existing documents and whether to save them
Create framework
Determine whether a file exists
Cmydoc: onopendocunment ()
{
Open the file;
Deletecontents ();
Create carchive;
Serialize;
Disable carchive;
}
}
}
}
}

Id_file_save
Cdocument: onfilesave ()
{
Dofilesave (null)
{
Cfiledialog: domodal ();
Onsavedocument ()
{
Carchive;
Serialize;
}
}
}

**************************************** ***********************

The relationship between application object, the main frame window, the document, the view, the document template object, and the associate string and menu Resources in the SDI program is described here.
 
The Windows Application Object
The Implement file of the cwinapp derived class contains statements similar to cmyapp theapp. Theapp is a global variable, which is the mechanism for starting the MFC application.
The following is a summary of the startup of the MFC application:
1. Windows loads the application into the memory;
2. Construct the global variable theapp (all global variables are constructed when the program is loaded into the memory );
3. Windows calls the global function winmain, which is a part of MFC. It is equivalent to the main function of the windowless application-the entry of the main program;
4. winmain finds the object theapp of the cwinapp derived class;
5. winmain calls the initinstance function for theapp. The initinstance function can be overloaded;
6. the overload initinstance function starts loading document and displaying the main frame and View windows;
7. winmain starts the run function for theapp. The run function is responsible for allocating window messages and command messages.
 
The document template class
The following code is available in the initinstance of the cwinapp derived class:
Csingledoctemplate * pdoctemplate;
Pdoctemplate = new csingledoctemplate (
Idr_mainframe,
Runtime_class (cstudentdoc ),
Runtime_class (cmainframe), // main SDI frame window
Runtime_class (cstudentview ));
Adddoctemplate (pdoctemplate );
This Code creates a connection between the application class, the document class, the View window class, and the main frame window. At this time, the object of the application class already exists (theapp), but the object of the other four classes has not yet been constructed, and the dynamic creation mechanism of MFC plays a role.
The two figures respectively represent the relationship between the above four classes and the relationship between the objects of the four classes:

 
Creating an empty document-the cwinapp: onfilenew Function
When the initinstance of application class calls adddoctemplate, it will call onfilenew.
Onfilenew:
1. Construct a document object, but do not read data from the hard disk;
2. Construct the main frame object and the main frame window, but do not display it. It includes idr_mainframe, the toolbar, And the status bar;
3. Construct the view object and View window, but do not display it;
4. establish the relationship between the document, main frame, and view object. here we need to use the class relationship established by adddoctemplate;
5. Call the cdocument: onnewdocument, cdocument: onnewdocument function for the document object to call the deletecontent function;
6. Call the cview: oninitialupdate virtual function for the view object;
7. Call cframewnd: activateframe for the frame object to display the main frame window (including the menus, View window, control bars ).
 
The document class's onnewdocument Function
The less work a constructor does, the better. In this way, the chance of a constructor making an error becomes smaller and smaller. Cdocument: onnewdocument and cview: oninitialupdate are good places for initialization. Here you can pop up a MessageBox. If an error occurs, onnewdocument can return false. It is worth noting that onnewdocument can be called many times, so if some commands only need to be executed once, a "first time" flag Member variable needs to be made.
 
Connecting file open to your serialization code-the onfileopen Function
The file opening menu maps to the cwinapp: onfileopen function. It will perform the following steps:
1. prompt the user to select a file;
2. Call the virtual function cdocument: onopendocument, cdocument: onopendocument to open the file, call cdocument: deletecontents, and construct a carchive object. Then, call the serialize function of document to load data from archive;
3. Call the oninitialupdate function of view.
 
Connecting File Save and File Save As to your serialization code
The onfilesave function maps the File Save and save as menus to the onfilesave function of cdocument. The onfilesave function calls the serialize function in sequence, which uses an archive object for storage.
 
The document's "dirty" flag
Cdocument has a protected member function m_bmodified. You can use the setmodifiedflag and ismodified functions of cdocument to access m_bmodified. When you create a document or read the data on the hard disk, m_bmodified is set to false by the application framework. After the document content is rewritten, make sure that m_bmodified is set to true.
 
Summary: Both onfilenew and onfileopen are in the application class. The application class contains documenttemplate, which can mobilize both document and view (oninitialupdate ).
Onfilesave and onfilesaveas are both in the document class. They only need to perform serialization and do not involve view operations. Therefore, they do not need to be placed in the application class.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/wirror800/archive/2009/03/21/4011847.aspx

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.