1.
USER command processing sequence diagram2.
On1_msg
CodeBool cframewnd: on1_msg (...) {cview * pview = getactiveview (); If (pview! = NULL & pview-> on1_msg (...)) return true; If (cwnd: on1_msg (...)) return true; cwinapp * PAPP = afxgetapp (); If (PAPP! = NULL & PAPP-> on1_msg (...) return true; return false; // call: defwindowproc}
2.
Objects processed by user commands by default:
Application:
- File-New
- File-open
- File-exit
Document:
Cframewnd
Note: Only USER command messages and UI update messages are processed in the above Order. Keyboard Input and mouse input are directly processed by the active window (view or frame window ). Document objects and program objects do not accept any non-command messages.
1. id_file_new
Entry: on_command (id_file_new, cwinapp: onfilenew)
Cwinapp: onfilenew call cdocmanager: onfilenew.
Cdocmanager: onfilenew checks whether there are more than one document template. The document type dialog box (afx_idd_newtypedlg) is displayed, allowing users to select the document type to be created. Then call cdoctemplate: opendocumentfile (null ).
Cdoctemplate: opendocumentfile (lpctstr lpszpathname, bool bmake_visible = true) has different processing methods for SDI and MDI.
For SDI,
(1) If a 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) Determine 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 ).
--------------------------------------------------------------------
Excerpt processing code about 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;
}
}
}
2. id_file_open
Entry: on_command (id_file_open, cwinapp: onfileopen)
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 matches the document type with matchdoctype (szpath, popendocument) for each template. Matching is mainly based on the file extension. If the file has already been opened in a document, the first view of the document will be activated; otherwise, the matching document template pbesttemplate-> opendocumentfile (szpath) will be used ).
Cdoctemplate: opendocumentfile call cdocument: onopendocument to open the file.
Cdocument: onopendocument open the file, use deletecontents to delete the existing file content, create the carchive object loadarchive and serialize (loadarchive) to read the file content, and setmodifiedflage (false ).
MDI: New Frame, new view?
SDI: Update view?
Excerpt 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
To determine whether a file exists, call cmydoc: onopendocunment/onnewdocument
}
}
}
}
Excerpt the id_file_open code about SDI
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;
}
}
}
}
}
3. id_file_save
Entry: on_command (id_file_save, cdocument: onfilesave)
Cdocument: onfilesave call cdocument: dofilesave ().
Cdocument: onfilesave checks whether the file is read-only. If yes, dosave (null); otherwise, dosave (m_strpathname ).
Cdocument: dosave (lpctstr lpszpathname, bool breplace = true) process:
1. If lpszpathname is null, skip the Second Step. If it is null, obtain the path name of the document and add the extension. If the breplace value is true, the SAVE file dialog box (afx_ids_savefile) is displayed. Otherwise, the copy and save dialog box (afx_ids + savefilecopy) is displayed ).
2. Call cdocument: onsavedocument to 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 files, and setmodifiedflag (false ).
Excerpt code about id_file_save
Cdocument: onfilesave ()
{
Dofilesave (null)
{
Cfiledialog: domodal ();
Onsavedocument ()
{
Carchive;
Serialize;
}
}
}
4. id_file_saveas
Entry: on_command (id_file_saveas, cdocument: onfilesaveas)
Cdocument: onfilesave calls dosave (null ).
5. id_file_close
Entry: on_command (id_file_saveas, cdocument: onfileclose)
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 Save dialog box (afx_idp_ask_to_save) is displayed. If the user selects Yes, The dofilesave () method is called to save the file.
Cdocument: onclosedocument () First destroys the document framework, and then deletes the document content using deletecontents (). If m_bautodelete is used, delete this.