CView Encyclopedia Business Card
CView is a view is the most used in the design of the Window object, it is the user's main operating interface. Because it usually represents document data in some form, it is called a view. A view object is associated with only one document object, and one document object can associate multiple views, and each View object represents the document data in different forms.Brief IntroductionDisplays data from a document class to the user, accepts user edits and changes to the data in the document, and feeds the modified results to the document class, where the modified content is saved to the disk file by the document class. The document is responsible for storing and reading the data in a permanent medium, and the view rendering simply renders the data in the document to the user in some form, so a document can correspond to multiple views. Invalidate (TRUE) sets the entire window to an invalid area that needs to be redrawn, which produces a WM_PAINT message so that OnDraw is invoked. If the data in the document changes, you must notify all the views that are linked to the document, and the Updateallview function of the document class needs to be invoked. However, in addition to print and print preview, the view class is simple to display.Associating Document Objects As mentioned earlier, the view needs to represent document data, so the document object must be associated with the View object. In this way, when the document data changes, it can notify the view in time, and when the view needs to display different document data, it can be extracted from the Document object. In a document/view framework program, a document object is always established before the view, and in the WM_CREATE message handler function of the view, it establishes its association with the document object. The code is as follows: Int cview::oncreate (lpcreatestruct lpcs) {if (cwnd::oncreate (lpcs) = = 1) return-1; A m_pdocuemnt view member that stores a pointer to the corresponding Document object, which is an empty ASSERT (m_pdocument = null); ccreatecontext* PContext = (ccreatecontext*) lpcs->lpcreateparams; Pcontext->m_pcurrentdoc is a pointer to an already established Document object (PContext!= null && pcontext->m_pcurrentdoc!= null) {//the current The view is added to the list of views in the Document object because a document can associate multiple views Pcontext->m_pcurrentdoc->addview (this); /* in the AddView () function of the document, the current document object pointer has been assigned to the M_pdocument view member. Thus, at the beginning of the view, the relationship is established. */ASSERT (m_pdocument!= NULL); else {TRACE ("warning:creating a pane with no cdocument./n"); return 0; OK} at the same time, the view class defines the member function getdocument (), which returns a pointer to the document object. The code is as follows: cdocument* cview::getdocument () const {ASSERT (this!= NULL); return m_pdocument; The view is always destroyed before the document object and is disassociate from the Document object in the view's destructor. The code is as follows: Cview::~cview () {if (m_pdocument!= NULL) M_pdocument->removeview (this); }Drawing of ViewsThe drawing of the window is always done in WM_PAINT message processing, and when the window needs to be drawn, it receives the WM_PAINT message from the system. In the drawing process, first prepare to display the device handle, and finally release the handle. As is well known, drawing in the view window does not require overloading the WM_PAINT message handler function OnPaint () because there is a more user-friendly drawing of the new port: OnDraw (). The argument for this function is a ready display device and is not released at all. In fact, all this is WM_PAINT message processing function OnPaint () for us, the code is as follows: void Cview::onpaint () {//CPAINTDC class Maintenance display device CPAINTDC DC (this); OnPrepareDC (&DC); OnDraw (&DC); OnPrepareDC () is a virtual function that is always executed before OnDraw () and can be overloaded and set drawing mode. Because OnDraw () uses a different CPaintDC object each time it executes, the state of this drawing pattern cannot be persisted to the next time. OnDraw () is defined as a pure virtual function in the graph class CView. For example: virtual void OnDraw (cdc* PDC) = 0; So CView is an abstract base class that cannot be instantiated, and derived classes must overload OnDraw ().virtual function OnUpdateWhen the document data changes, the Document object invokes Cdocument::updateallview () to notify all views, and as a response, the OnUpdate () member of the view is invoked. Therefore, the overloaded onupdate () should be able to reflect changes in the document data in the view as needed. Cview::onupdate () Simply invalidates the client area, causing the client area to repaint. For example: void Cview::onupdate (cview* psender, LPARAM, cobject*) {ASSERT (Psender!= this); Unused (Psender); Unused in release builds Invalidate (TRUE); }virtual function OnInitialUpdate ()After the initial creation, invocation of OnCreate (), or after the file/new, File/open command, is invoked by the framework. The base class CView::OnInitialUpdate () simply invokes OnUpdate (), which can be overloaded to complete initialization work. Note, however, that it may be invoked multiple times. void CView::OnInitialUpdate () {OnUpdate (null, 0, NULL); }virtual function CalcwindowrectWhenever the client area of the main frame window changes or the position of the control bar changes, and the client area needs to be rearranged, the function is called to calculate the size of the view window based on the view client area size. We know that the client area of the arranging main window is completed by CFrameWnd::RecalcLayout (). Obviously, the calcwindowrect () function of the view is also invoked by it. The client area size of the main window drops all control-occupied parts, and the remaining areas are divided into views, which are passed into Calcwindowrect () as arguments. Within the Calcwindowrect () function, you need to calculate the dimensions of the view window. The code is as follows: void Cview::calcwindowrect (lprect lpclientrect, Unit nadjusttype) {//Lpclientrect This is the size of the entire view client area//need to increase the size of the scroll bar if (nadjusttype!= 0) {//Call API, calculate window size according to window style:: Adjustwindowrectex (lpclientrect, 0, FALSE, Getexstyle ()); DWORD dwstyle = GetStyle (); if (Dwstyle & Ws_vscroll) {//is the vertical scroll bar increases the dimension int nadjust = Afxdata.csvscroll; if (Dwstyle & ws_border) Nadjust-= Cx_border; Lpclientrect->right + = Nadjust; The IF (Dwstyle & Ws_hscroll) {//is the horizontal scroll bar increases the dimension int nadjust = Afxdata.cyhscroll; if (Dwstyle & ws_border) Nadjust-= Cy_border; Lpclientrect->bottom + = Nadjust; } return; ///No need to increase the size of the scroll bar, call the base class member to complete the calculation cwnd::calcwindowrect (Lpclientrect, Nadjusttype); }virtual function PostNcDestroyThe last called member function when the view window closes, and it completes the same function as the CFrameWnd::P Ostncdestroy, that is, to delete the View object. The code is as follows: void CView::P Ostncdestroy () {Delete this; In this way, you do not have to care about the release of the view, even if it is constructed in the heap.virtual function OnCmdMsgThe function is already known in the discussion of Cframewnd::oncmdmsg (), and the following only gives its code: BOOL cview::oncmdmsg (UINT nID, int ncode, void* Pextra, afx_ cmdhandlerinfo* phandlerinfo) {///First find its own command message map if (Cwnd::oncmdmsg (NID, Ncode, Pextra, Phandlerinfo)) return TRUE; If the view itself does not handle the command, the opportunity is left to the document object associated with it if (m_pdocument!= NULL) {Cpushroutingview push (this); Return m_pdocument->oncmdmsg (NID, Ncode, Pextra, Phandlerinfo); return FALSE; }virtual function OnActivateView Called to notify the view when the view is activated as an active view, or when the activity becomes inactive. The implementation of the base class simply sets the view to focus. The code is as follows: void Cview::onactivateview (BOOL bactivate, cview* Pactivateview, cview*) {unused (Pactivateview);//unused in Re Lease builds if (bactivate)//current status is active {ASSERT (Pactivateview = = this); Sets the focus if its parent window is also active. (in MDI, its parent frame may be inactive) if (istopparentactive ()) SetFocus (); } }