Interaction between documents, views, and frameworks

Source: Internet
Author: User

 

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.

Related Article

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.