[Reprint] different views of an MFC Document

Source: Internet
Author: User

MDI (multipledocument Interface) is a specification of the Windows interface. It establishes multiple windows to browse document data. For example, programmanager in Windows is implemented according to the MDI specification. In actual engineering software development, many programmers regard it as a standard method to implement multiple windows. Microsoft's basic library (MFC) is a powerful general-purpose class library provided by Microsoft to facilitate windows program development. The core of MFC is to encapsulate a large number of windows APIs in the form of classes. Using MFC in visual programming language VC ++ is currently one of the most convenient ways to develop Windows programs. Various development tools provided by VC ++, such as Appwizard, classwizard, and appstudio, can create a Windows framework with basic functions ). What programmers need to do is to fill in their own code into the framework program, which greatly reduces the workload of user interface programming and speeds up development. For standard development methods of MDI, refer to general windows programming books. This article describes how to use MFC to implement the MDI interface.

Mfc2.0 and later versions support the document/view structure mode. The document is responsible for data management, and the browsing window is responsible for data display and interaction with users. This separates data from the interface and makes the whole program design more standardized and modular. In MFC, "document" is implemented by the class cdocument and Its Derived classes (Doc class for short); "View window" is implemented by the class cview and Its Derived classes (View class for short ). Both are included in and managed by the Framework Window of the application. When using a single document, the Framework Window is implemented by the class cframewnd and Its Derived classes. When using multiple documents, the Framework Window is implemented by the class cmdiframewnd and javasichildwnd. The document template links documents, browsing windows, and frame windows.

When the programmer selects multipledocumentinterface in the option of Appwizard, the MFC framework automatically generates the code to implement the basic functions of MDI. Cmdiframewnd is responsible for the main frame window of the entire application; cmdichildwnd is used to implement the Child Window Frame of MDI. It shares the menu with the main frame window without any menu items. The main frame window automatically replaces the menu items based on the currently activated subwindow. Cview is responsible for the specific content displayed in the customer area of the MDI subwindow. For example, the framework created by Appwizard with M01 as the project name includes some basic classes: the main framework window cmainframe: Derived from cmdiframewnd; the document cm01doc: Derived from cdocument; cm01view In the browsing window: Derived from cview. cm01doc, cm01view, and javasichildwnd are linked by the multi-Document Template cmultidoctemplate. The code in the cm01app: initinstance () function is as follows:

1 bool cm01app: initinstance () 2 {3 ...... 4 5 cmultidoctemplate * pdoctemplate; 6 // cmultidoctemplate is used for MDI document 7 pdoctemplate = newcmultidoctemplate (8 idr_m01type, // resource ID 9 runtime_class (cm01doc ), 10 // document class 11 runtime_class (cmdichildwnd), 12 // standard MDI child window framework 13 runtime_class (cm01view); 14 // browse window class 15 adddoctemplate (pdoctemplate ); 16 // Add a new template for the entire application 17 ...... 18}

In this case, the data Doc class is associated with only one view class, and the content displayed in each child window of MDI is consistent. If you want different sub-windows to display different documents, you need to create new resource items, new document classes, and new view classes respectively, use the new template to associate them with cmdichildwnd. The MFC framework program hides complex message sending and receiving mechanisms to automatically schedule subwindows. Programmers only need to set their own data and reload the ondraw () function in each view to complete the necessary painting.

However, in actual development applications, you often want to display a type of data in different ways, which can both observe values and display graphs. This requires that the same Doc class be associated with multiple view classes, and each View class corresponds to a different MDI subwindow. A typical usage of cmultidoctemplate is to establish an independent document structure and view object. The following cmultidoctemplate uses the same document and multiple view classes.

(1) Use classwizard to create a new view class: cm02view.

(2) create a new template:

Cmultidoctemplate * pdoctemplate02 = newcmultidoctemplate (

Idr_m01type, // use the same resource

Runtime_class (cm01doc), // same document

Runtime_class (cmdichildwnd), // standard MDI child window framework

Runtime_class (cm02view); // New View

Then, add a new template using the CAPP: adddoctemplate function.

If you still Add a new template to the cm01app: initinstance () function, the Framework incorrectly considers that the program supports two types of documents, in this way, a dialog box is displayed when the generated EXE file is compiled. You must select the document type. In fact, the two types of documents are the same.

To avoid this problem, you can use the methods recommended by the MFC developer: In the runtime case, first copy the string resource idr_m01type to a new string resource idr_m02type in appstudio. Then, delete the second string m01document (the string is cdoctemplate: filenewname) in the string resource idr_m02type ). Then, use the new resource idr_m02type to create the second template. In this way, the compiled EXE file will not pop up a dialog box. After studying the source code of MFC, we found that the pop-up document type dialog box is because the onfilenew () function is called in the cm01app: initinstance () function. The onfilenew () function checks the number of document templates. When there are more than one template, a dialog box is displayed. After the user selects the template, an MDI window is created based on the selected document type. Because the filenewname item of the second template is deleted and the document type cannot be displayed, the dialog box is automatically stopped, and the first type is used as the default document type to create an MDI window.

In engineering applications, the onfilenew () function is generally called only once during program initialization (as for the menu file | new response, the user can take over the processing), so it can not be in the cmyapp :: add a new document template to the initinstance () function to bypass the onfilenew () function check. Add the required document template as needed and create a new subwindow. This avoids the Document Type dialog box and eliminates the need to add string resources.

A simple example is as follows: the first sub-window is automatically created by the Framework Program; A new menu item "newwindow" is set to process the menu message in cmainframe, the second subwindow is displayed in the message response function.

1 void cmainframe: onnewwindow () 2 {3 // Add a new document template 4 static cmultidoctemplate * pdoctemplate_new; 5 static boolbchildcreated = false; 6 // flag, whether a new window is created; if it is already created, 7 if (bchildcreated = false) 8 {9 pdoctemplate_new = new cmultidoctemplate (10 idr_m01type, // use the same resource 11 runtime_class (cm01doc ), 12 runtime_class (cmdichildwnd), 13 // standard MDI child window framework 14 runtime_class (cm02view); 15 afxgetapp ()-> adddoctemplate (pdoctemplate_new ); 16 // create a new sub-window 17 cmdichildwnd * pmdiactive = mdigetactive (); // obtain the pointer of the sub-window of the current activity 18 cmpvdoc * pdoc = (cmpvdoc *) pmdiactive-> getactivedocument (); // get the document pointer 19 cmdichildwnd * pnewframe = (cmdichildwnd *) (pdoctemplate_new-> createnewframe (pdoc, null )); 20 // create a new frame window 21 if (pnewframe = NULL) 22 {23 afxmessagebox ("new window cannot be created", mb_ OK, 0); 24 return; // notcreated25} 26 pdoctemplate_new-> initialupdateframe (pnewframe, pdoc); // display window 27 mditile (mditile_horizontal); // tile multiple Windows 28 bchildcreated = true; 29} 30}

Different views have their own drawing code in the ondraw () function. when data is updated, you only need to call the cdocument: updateallviews () function to update all MDI subwindows.

 

A summary is sometimes used to create a cmultidoctemplate-based window (in other words, cframe/cdocument/cview combination) without the use of the mechanism provided by cwinapp: onfilenew.
For example, if the program has multiple document templates, cwinapp: onfilenew will ask which types of documents are required to open a dialog box to prompt users. The programmer may already know which type of cmultidoctemplate to use, and therefore may not want to prompt the user, because it will be an inappropriate application's given context. For more information, the unrecorded cmultidoctemplate: opendocumentfile function assumes that the application was initially created with the Application Wizard to create a new cmultidoctemplate-based window. There are several steps involved:
  1. The cmultidoctemplate pointer added to the cwinapp-derived class:
          class CMyApp : public CWinApp      {        ...       public:        CMultiDocTemplate* m_pDocTemplate;        ...      }
    Note:: If you plan to use multiple document types, you must create a cmultidoctemplate pointer member variable for each document type.
  2. In the cwinapp: initinstance call, remove the created cmultidoctemplate from adddoctemplate to the call. Set the pointer to the new cmultidoctemplate. Use a pointer to call adddoctemplate:
          BOOL CMyApp::InitInstance()      {        ...        m_pDocTemplate = new CMultiDocTemplate(IDR_TEXTTYPE,                                      RUNTIME_CLASS(CMyDoc),                                      RUNTIME_CLASS(CMDIChildWnd),                                      RUNTIME_CLASS(CMyView));        AddDocTemplate(m_pDocTemplate);        ...      }
  3. Use a pointer to call cmultidoctemplate: opendocumentfile has a null parameter to create a new window. For this example, it is assumed that there is no button in the cview window. In the bn_clicked processing of the button, we need to create a window based on m_pdoctemplate:
          void CMyView::OnNewWindowButtonClicked()      {          CMyApp* pApp = (CMyApp*)AfxGetApp();          pApp->m_pDocTemplate->OpenDocumentFile(NULL);      }
    This technology can be used to create a csingledoctemplate-based window in a single document interface (SDI) application. However, it is not necessary. Since there is only one application document template, calling onfilenew () will create a window without prompting the user to enter the document type.
  4. References http://support.microsoft.com/kb/113257/zh-cn

[Reprint] different views of an MFC Document

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.