User input/The output is basically based on the view, but with some exceptions, it may need to directly act with the framework, and how to transmit data between views in the case of multiple views.
When using the menu, you will find that when a menu is not mapped for processing, it is forbidden. In the case of multiple views, the menu status and processing ing are associated with the current active view.MFC can ensure that various messages can be correctly received, but sometimes it may cause inconvenience. One solution is to process messages in the framework, which ensures that the current document can get the current message through the framework.
How can I update the status after the user inputs it? This problem does not exist when a document corresponds to a view, but now one document corresponds to two views, how can I ensure that the other view is notified when input is made on one view?The MFC approach is to use documents for processing, because documents manage the views currently associated with them, and it is most appropriate for them to notify various views. Let's take a look at two functions at the same time:
- Void CView: OnUpdate (CView * pSender, LPARAM lHint, CObject * pHint)
- Void CDocument: UpdateAllViews (CView * pSender, LPARAM lHint = 0L, CObject * pHint = NULL)
WhenWhen UpdateAllViews is called, all the OnUpdate operations related to this document will be called, and the parameters lHint and pHint will be passed. In this way, other brothers can be notified as soon as a change occurs. There is another problem: how can I know that the view has changed in OnUpdate? In this case, we can use the pHint parameter, as long as the caller assigns the this pointer to the parameter, of course, you can use this parameter to transmit more complex structures.
Initialize ViewWhen a document is opened or a new document is createdCView: OnInitialUpdate () will be called. You can reload this function to initialize the object and call OnInitialUpdate of the parent class before the end, because this ensures that OnUpdate will be called.
Clear content in the documentWhen the document is closed (for example, exit or clear the previous document before it is created)Void CDocument: DeleteContents () will be called. You can reload this function to clear it.
The preceding two points are particularly important in the single document structure, because the software running document objects and view objects are generated and deleted only once. So we shouldThe structure and Analysis of C ++ objects are clear.
Finally, the document template (DocTemplate). The document templates are divided into two types of Single-document templates and multi-document templates, which are represented by CSingleDocTemplate and CMultiDocTemplate respectively. The role of the template is to record the relationship between documents, views, and frameworks. Another point is that the template can record the types of files that can be opened by the application. When the file is opened, the correct document and view will be selected based on the information in the document template. A template is a concept that is quite confusing. In general, we do not need to perform operations directly.
When the user modifies the data, it should callGetDocument ()-> SetModifiedFlag (TRUE) indicates that the document data has been updated. when the document is closed, the user is automatically asked whether to save the data.