How to change the default document operation mode of MFC
When MFC is used for programming, its document/view structure automatically generates new, open, and saved code for the file. This saves us a lot of trouble. But when the operation object is non-text files such as ODBC databases, the problem arises. How can I change the default document operation process?
After analyzing the source code of MFC, we can see that. The "New" "open" message in the default menu corresponds to the cwinapp: onfilenew () and cwinapp: onfileopen (), and the two functions directly call cdocmanager :: onfilenew and cdocmanager: onfileopen. Cdocmanager: onfileopen works as follows:
1. Call cdocmanager: dopromptfilename, while dopromptfilename is used to display the file opening dialog box to obtain the file name.
2. Call cwinapp: opendocumentfile (lpctstr lpszfilename ).
To change the document opening method, you only need to reload dopromptfilename and opendocumentfile in the inheritance class of cwinapp. For example, to change the default file opening dialog box. You only need to create a file to open the dialog box and call it in the overloaded dopromptfilename to get a file name.
The following is an analysis of cwinapp: opendocumentfile. The working process of opendocumentfile is:
1. Determine which document template to use by the suffix of the parameter lpszfilename.
2. Adjust the document, view, and framework windows of the document template.
3. Call cdoctemplate: opendocumentfile
Cdoctemplate: opendocumentfile is a pure virtual function, which is implemented by its derived classes csingledoctemplate and cmultidoctemplate. It is illustrated by csingledoctemplate: opendocumentfile.
1. Determine whether there are existing documents. If yes, determine whether the documents have been saved.
2. Create a Framework Window.
3. Check whether the file exists by the file name path. If yes, call the custom cmydoc: onopendocument. Otherwise, call cmydoc: onnewdocument.
From the above analysis, we can make multiple changes to the default file operation method of MFC, such as changing the file opening and saving mode and changing the file opening and saving dialog box.
The various source codes shown above can be obtained from the mfcinclude and mfcsrc subdirectories under the VC installation directory. Cwinapp, cdocmanager, and csingledocmanager. the header file of the cdocument class is afxwin. h. The source code of the cwinapp class is in appcore. in CPP, the source code of cdocmanager is in docmgr. in CPP, the source code of the cdoctemplate class and csingledocmanager class are located in doctempl. CPP and docsingl. CPP.