VS2010-MFC (documents, views, and Frames: Overview)

Source: Internet
Author: User

Transferred from: http://www.jizhuomi.com/software/221.html

The first few sections are about the use of menus, toolbars, and status bars, and this section begins with the knowledge of documents, views, and frames.

Introduction to documents, views, and frames

In the single document engineering Example34 created with the introduction to VS2010/MFC programming 34 (Menu: VS2010 menu Resource), we can see that the MFC Wizard automatically generates the Cexample34doc class for us, The Cexample34view class and the CMainFrame class, respectively, are document classes, view classes, and frame window classes .

Document/view architecture is a good design provided by MFC, which separates the processing and display of data, which makes it easier for us to maintain and extend the program. The following are several important concepts that are involved in this structure.

Document

Document objects are used to manage and maintain data, including operations such as saving data, extracting data, and modifying data, and after the data has been modified, the document can notify all of its corresponding views to update the display.

View

The View object visualizes the data in the document, takes care of extracting the data from the Document object, displays it to the user, and accepts the user's input and edits to reflect the changes in the data to the Document object. The view acts as a medium between the document and the user.

Frame

A document may have multiple view interfaces, which requires a framework to manage. A framework is used to manage documents and views. The frame window is the main window of the application, and when the application executes, it creates a top-level frame window first. A view window is a child window that has no menus and boundaries, which must be contained within a frame window, which is placed in the client area of the frame window.

document Templates

Document templates contain information related to documents, views, and frames. The application creates document objects, frame window objects, and view objects through document templates. In addition, the relationships between documents, views, and frames are also managed by document templates.

Let's take a look at the Example34 single-document program, Cexample34app the member functions of the Application Class Cexample34app::initinstance () to create and register parts of the document template:

C + + code
BOOL cexample34app::initinstance () {... Slightly//Register The application ' s document templates. Document Templates//serve as the connection between documents, frame windows and viewscsingledoctemplate*pdoctemplate; Pdoctemplate=Newcsingledoctemplate (Idr_mainframe, Runtime_class (Cexample34doc), Runtime_class (Cmainfra Me),//main SDI frame windowRuntime_class (Cexample34view)); if(!pdoctemplate)returnFALSE;         AddDocTemplate (pdoctemplate); ...... Slightlyreturn TRUE; }  
 

When constructing the object of the document template class CSingleDocTemplate, the first parameter is the resource ID idr_mainframe, which includes the frame window icon and so on, and the next three parameters are called Runtime_class macros, runtime_ Class is used to get run-time information for a class, and document templates can create objects of the corresponding class, that is, document objects, frame window objects, and view objects, based on these dynamic creation information. The AddDocTemplate function is used to register a document template object.

Framework classes, document classes, and view classes

In VS2010 automatically generated code, the framework class inherits from the CFrameWndEx class, the document class inherits from the CDocument class, and the view class inherits from the CView class.

The CFrameWndEx class is also inherited from the CFrameWnd class, and the member functions in the CFrameWnd class for managing documents and views include:

virtual cdocument* getactivedocument ();

Gets a pointer to the document object for the currently active view and returns null if it does not exist.

cview* GetActiveView () const;

Gets a pointer to the currently active view object and returns null if it does not exist.

void SetActiveView (cview* pviewnew, BOOL bNotify = TRUE);

Sets the active view. The parameter pviewnew is a pointer to the view object to be activated, and the parameter bnotify specifies whether the view receives activation notifications.

main member functions of the CDocument class:

virtual BOOL onnewdocument ();

Create a new document. can be overloaded with.

virtual BOOL onopendocument (LPCTSTR lpszpathname);

Open the document. The parameter lpszpathname is the path to the document to open. can be overloaded with.

virtual BOOL onsavedocument (LPCTSTR lpszpathname);

Save the document. The lpszPathName parameter specifies the full path to which the document is saved. can be overloaded with.

cdoctemplate* getdoctemplate () const;

Gets a pointer to the document template object that corresponds to this document type. Returns NULL if this document is not managed by the document template.

virtual POSITION getfirstviewposition () const;

Gets the position of the first view of the View list in a document.

virtual cview* getnextview (position& rposition) const;

Use this function to iterate over all views of a document. The parameter rposition is a reference to the position value returned by the last call to the GetFirstViewPosition or GetNextView member function.

void AddView (cview* pView);

Adds a view to the document. The parameter pview is a pointer to the view object to be added.

void Removeview (cview* pView);

Removes the association of a view from a document. The parameter pview is a pointer to the view object to remove.

void UpdateAllViews (cview* psender, LPARAM lHint = 0L, cobject* phint = NULL);

Call this function to update the view after the document has been changed. The parameter Psender points to the view that modifies the document, and the actual application is often used to specify which view does not need to be updated, if the update all views is set to NULL, the parameter Lhint contains information about the document modification, and the parameter Phint points to the object that stores the document modification information.

The member functions in the CView class that are related to the document/view structure include:

cdocument* getdocument () const;

Gets a pointer to the document object associated with the view. Returns null if the view is not associated to the document.

VS2010-MFC (documents, views, and Frames: Overview)

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.