In-depth introduction to the MFC Document/view architecture (4) view

Source: Internet
Author: User
Tags ole
View class cview

In the MFC "document/View" architecture, the cview class is the base class of all view classes. It provides public interfaces for custom view classes. In the document/view architecture, the document is responsible for managing and maintaining data, while the View class is responsible for the following:

(1) extract the data in the document from the document class and display it to the user;

(2) accept users' edits and modifications to the data in the document;

(3) feedback the modified results to the document class, which saves the modified content to the disk file.

The document is responsible for the storage and reading of data in permanent media. view rendering only presents the data in the document to the user in some form. Therefore, a document can correspond to multiple views.

Let's take a look at the cview class declaration:

Class cview: Public cwnd
{
Declare_dynamic (cview)
// Constructors
Protected:
Cview ();

// Attributes
Public:
Cdocument * getdocument () const;

// Operations
Public:
// For standard printing setup (override onprepareprinting)
Bool doprepareprinting (cprintinfo * pinfo );

// Overridables
Public:
Virtual bool isselected (const cobject * pdocitem) const; // support for Ole

// Ole scrolling support (used for drag/drop as well)
Virtual bool onscroll (uint nscrollcode, uint NPOs, bool bdoscroll = true );
Virtual bool onscrollby (csize sizescroll, bool bdoscroll = true );

// OLE drag/drop support
Virtual dropeffect ondragenter (coledataobject * pdataobject, DWORD dwkeystate, cpoint point );
Virtual dropeffect ondragover (coledataobject * pdataobject, DWORD dwkeystate, cpoint point );
Virtual void ondragleave ();
Virtual bool ondrop (coledataobject * pdataobject, dropeffect, cpoint point );
Virtual dropeffect ondropex (coledataobject * pdataobject,
Dropeffect dropdefault, dropeffect droplist, cpoint point );
Virtual dropeffect ondragscroll (DWORD dwkeystate, cpoint point );

Virtual void onpreparedc (CDC * PDC, cprintinfo * pinfo = NULL );

Virtual void oninitialupdate (); // called first time after construct

Protected:
// Activation
Virtual void onactivateview (bool bactivate, cview * pactivateview, cview * pdeactiveview );
Virtual void onactivateframe (uint nstate, cframewnd * pframewnd );

// General drawing/updating
Virtual void onupdate (cview * psender, lparam lhint, cobject * phint );
Virtual void ondraw (CDC * PDC) = 0;

// Printing support
Virtual bool onprepareprinting (cprintinfo * pinfo );
// Must override to enable printing and print preview

Virtual void onbeginprinting (CDC * PDC, cprintinfo * pinfo );
Virtual void onprint (CDC * PDC, cprintinfo * pinfo );
Virtual void onendprinting (CDC * PDC, cprintinfo * pinfo );

// Advanced: end print preview mode, move to Point
Virtual void onendprintpreview (CDC * PDC, cprintinfo * pinfo, point, cpreviewview * pview );

// Implementation
Public:
Virtual ~ Cview ();
# Ifdef _ debug
Virtual void dump (cdumpcontext &) const;
Virtual void assertvalid () const;
# Endif // _ debug

// Advanced: for implementing custom print preview
Bool doprintpreview (uint nidresource, cview * pprintview, cruntimeclass * ppreviewviewclass, cprintpreviewstate * pstate );

Virtual void calcwindowrect (lprect lpclientrect, uint nadjusttype = adjustborder );
Virtual cscrollbar * getscrollbarctrl (INT nbar) const;
Static csplitterwnd * Pascal getparentsplitter (const cwnd * pwnd, bool banystate );

Protected:
Cdocument * m_pdocument;

Public:
Virtual bool on1_msg (uint NID, int ncode, void * pextra, afx_cmdhandlerinfo * phandlerinfo );
Protected:
Virtual bool precreatewindow (createstruct & CS );
Virtual void postncdestroy ();

// Friend classes that call protected cview overridables
Friend class cdocument;
Friend class cdoctemplate;
Friend class cpreviewview;
Friend class cframewnd;
Friend class cmdiframewnd;
Friend class extends ichildwnd;
Friend class csplitterwnd;
Friend class coleserverdoc;
Friend class cdocobjectserver;

// {Afx_msg (cview)
Afx_msg int oncreate (lpcreatestruct lpcs );
Afx_msg void ondestroy ();
Afx_msg void onpaint ();
Afx_msg int onmouseactivate (cwnd * p0000topwnd, uint nhittest, uint message );
// Commands
Afx_msg void onupdatesplitcmd (ccmdui * pcmdui );
Afx_msg bool onsplitcmd (uint NID );
Afx_msg void onupdatenextpanemenu (ccmdui * pcmdui );
Afx_msg bool onnextpanecmd (uint NID );

// Not mapped commands-must be mapped in derived class
Afx_msg void onfileprint ();
Afx_msg void onfileprintpreview ();
//} Afx_msg
Declare_message_map ()
};

The cview class must first maintain the association between the document and the view. It records the pointer of the associated document through the cdocument * m_pdocument protective member variable, and provides the cview :: the getdocument interface function allows applications to obtain documents associated with views. In the cview class destructor, You need to delete the current view in the corresponding document Class View list:

Cview ::~ Cview ()
{
If (m_pdocument! = NULL)
M_pdocument-> removeview (this );
}

The most important function in cview is virtual void ondraw (CDC * PDC) = 0. The declaration of this function shows that cview is a pure virtual base class. This function must be overloaded. It generally performs the following steps:

(1) Use the getdocument () function to obtain the pointer of the document corresponding to the view;

(2) read the data in the corresponding document;

(3) display the data.

An initial "document/View" architecture project created using the MFC Wizard will reload the ondraw () function, note: "add draw code for native data here (draw code for adding activity data )":

//////////////////////////////////////// /////////////////////////////////////
// Cexampleview drawing
Void cexampleview: ondraw (CDC * PDC)
{
Cexampledoc * pdoc = getdocument ();
Assert_valid (pdoc );
// Todo: Add draw code for native data here
}
Cview: precreatewindow initializes the View:
//////////////////////////////////////// /////////////////////////////////////
// Cview second phase construction-bind to document
Bool cview: precreatewindow (createstruct & CS)
{
Assert (CS. Style & ws_child );

If (CS. lpszclass = NULL)
{
Verify (afxdeferregisterclass (afx_wndframeorview_reg ));
CS. lpszclass = _ afxwndframeorview; // color_window background
}

If (afxdata. bwin4 & (CS. Style & ws_border ))
{
CS. dwexstyle | = ws_ex_clientedge;
CS. Style & = ~ Ws_border;
}

Return true;
}

Cview: The onupdate function is called when the data in the document is changed (that is, it is used to notify a view that the content of the associated document has been modified ), it indicates that we need to redraw the view to display the changed data. Invalidate (true) sets the entire window to an invalid area to be repainted, which generates the wm_paint message, so that ondraw will be called:

Void cview: onupdate (cview * psender, lparam/* lhint */, cobject */* phint */)
{
Assert (psender! = This );
Unused (psender); // unused in release builds

// Invalidate the entire pane, erase background too
Invalidate (true );
}

If the data in the document changes, you must notify all views linked to the document. At this time, the updateallviews function of the document class needs to be called.

In addition, the cview class contains a series of functions for printing and previewing documents:

(1) cview: onbeginprinting is called at the beginning of the printing task to allocate GDI resources;

(2) cview: The onprepareprinting function is called before printing or previewing a document. It can be used to initialize the Print dialog box;

(3) cview: onprint is used to print or preview a document;

(4) cview: The onendprinting function is called at the end of the print operation to release the GDI resource;

(5) cview: onendprintpreview is called when you exit print preview mode.

  Cview derived class

MFC provides a wide range of cview Derived classes. Different Derived classes support different types of controls and provide users with diversified Display Interfaces. These cview Derived classes include:

(1) cscrollview: supports rolling;

(2) cctrlview: supports tree, list, and rich edit controls;

(3) cdaorecordview: displays database records in the dialog-box control;

(4) ceditview: provides a simple multi-line text editor;

(5) cformview: contains the dialog-box control, which can be scrolled and is based on the dialog box template resources;

(6) clistview: The list control is supported;

(7) crecordview: displays database records in the dialog-box control;

(8) cricheditview: supports the rich edit control;

(9) ctreeview: tree control is supported.

Cricheditview, ctreeview, and clistview are inherited from the cctrlview class, cformview is inherited from the cscrollview class, And crecordview and cdaorecordview are further inherited from the cformview class.

Describes the inheritance relationship of the cview class system:

 

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.