C ++ MFC file opening process, mfc file opening process

Source: Internet
Author: User

C ++ MFC file opening process, mfc file opening process

To open a file, follow these steps:

The "Open File" dialog box is displayed. Click "get selected file" and display the file in the view.

We often need to customize the procedure in the program as follows:

1. Customize the pop-up file dialog box. For example, you need to modify the type or extension of the opened file.

2. Custom file display behavior

3. Custom file opening Behavior

 

1. CWinApp: OnFileOpen

It is the processing function of the ID_FILE_OPEN message and calls CDocManager: OnFileOpen internally.

Below is the source code of CWinApp: OnFileOpen:

1 void CWinApp: OnFileOpen ()

2 {
3 ENSURE (m_pDocManager! = NULL );
4 m_pDocManager->OnFileOpen(); // CDocManager m_pDocManager;
5}

CDocManager: the main function of OnFileOpen is to pop up the file selection dialog box. Its source code is as follows:

1 void CDocManager: OnFileOpen ()

2 {
3 // prompt the user (with all document templates)
4 CString newName;
5 if (!DoPromptFileName(NewName, AFX_IDS_OPENFILE,
6 OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, TRUE, NULL ))
7 return; // open canceled
8
9 AfxGetApp ()->OpenDocumentFile(NewName );
10 // if returns NULL, the user has already been alerted
11}

CDocManager: DoPromptFileName is used to bring up the file selection dialog box.

Here we can solve the first problem-the custom file opening dialog box. You can select either of the following methods:

First: Rewrite CWinApp: OnFileOpen

Example:

 

1 CYourApp: OnFileOpen ()
2 {
3 CString newName;
4 // 1. The open file dialog box appears.
5 // 2. directly call OpenDocumentFile (newName)
6 OpenDocumentFile (newName );
7}

 

The disadvantage of this method is that if you want to customize the save file dialog box, you have to rewrite another function, such as CDocument: DoSave.

Second: Override CDocManager: DoPromptFileName

In this way, both the open file dialog box and the Save file dialog box are changed, because the dialog box at the time of opening and saving is displayed through this function.

This method requires two steps:

1. Customize a CDocManager subclass, for example, CYourDocManager, where override the DoPromptFileName Method

2. Find AddDocTemplate in the CYourApp: InitInstance function, and add the following line before it: m_pDocManager = new CYourDocManager;

In AddDocTemplate, determine whether m_pDocManager is Null. If it is Null, create one. We instantiate m_pDocManager in front of it,

In this way, the custom DoPromptFileName function will be called.

 

2. CWinApp: OpenDocumentFile

After obtaining the path of the file to be opened, the Framework calls this function and reads and displays the file data.

By tracking the source code of MFC, we will find thatCDocument: OnOpenDocument,

CDocument: The default action of OnOpenDocument is to callDeleteContentsThe member function to ensure that the document is blank and then callSerializeFunction to read file data.

Therefore, if your program needs to open files, you must rewrite the Serialize function in your document class.

 

In the previous article, we have solved the first two custom requirements. Now we can solve the third problem. If we need to perform some operations after opening the document, such as initialization, we can rewrite three functions:

1. CDocument: OnOpenDocument

In Document, we can get the View associated with it. If you need to initialize the View or other initialization after opening the Document, you can rewrite this function, for example:

1 BOOL CMyDoc: OnOpenDocument (LPCTSTR lpszPathName)

2 {
3 if (! CDocument: OnOpenDocument (lpszPathName ))
4 return FALSE;
5 // initialize

6 return TRUE;
7}

You can also rewrite the following two functions for initialization.

2. CWinApp: OpenDocumentFile

Default behavior: CDocManager: OpenDocumentFile --> CDocument: OnOpenDocument

In other words, it will eventually call CDocument: OnOpenDocument. Here, CDocManager: OpenDocumentFile actually creates a Document.

3. CWinApp: OnFileOpen

There is no uniform standard for the specific initialization Code. For me, where Initialization is convenient, I will put the initialization code.

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.