Multi-view implementation of a single document in VC project development-non-split window [transfer]

Source: Internet
Author: User
Tags textout

K_eckel: http://www.mscenter.edu.cn/blog/k_eckel

Multi-view is one of the technologies frequently used in VC development. Generally, there are two ways to achieve multi-view of a single document.

1) view splitting technology (UseCsplitterwndThe window is divided into multiple parts, and each part shows different views. This technology is easy to implement and there are a lot of related information.

2) associate multiple views with a document to display the entire view in the window.

 

The second implementation is more complicated than the first one. Here we provide a detailed implementation method.

Step 1: Use VC 6.0 to create a project named multiview. In addition to selecting single-document attributes, the "default" method is used for all items. Therefore, you can obtain five classes:Cmainframe,Cmultiviewapp,Cmultiviewdoc,Cmultiviewview, AndCaboutdlg;

 

Step 2: Create a New View and add a new MFC class (insert-> New Class). The base class is cview (or a derived subclass of cview, such as ceditview ). The class name isCanotherviewThis is the new view.CanotherviewAdd the getdocument implementation:

CMultiViewDoc* CAnotherView::GetDocument(){       return (CMultiViewDoc*)m_pDocument;}

 

Step 3: InCmultiviewappAdd member variable records to these two views:

private:       CView* m_pFirstView;       CView* m_pAnotherView;

 

Program menuIdr_mainframeAdd a menu item "View", which has two sub-Menus "View 1" and "View 2", and add the corresponding function (voidCmultiviewapp: Onshowfirstview () and voidCmultiviewapp: Onshowsecondview ());

 

Step 4: Create a New View: In boolCmultiviewapp: Initinstance () to add code:

....... // Create a new view cview * m_pactiveview = (cframewnd *) m_pmainwnd)-> getactiveview (); m_pfirstview = m_pactiveview; m_panotherview = new canotherview (); // associate the document and view with cdocument * m_pdoc = (cframewnd *) m_pmainwnd)-> getactivedocument (); ccreatecontext context; context. m_pcurrentdoc = m_pdoc; // create a view uint m_idforanotherview = afx_idw_pane_first + 1; crect rect; m_panotherview-> Create (null, null, ws_child, rect, m_pmainw Nd, m_idforanotherview, & context );......

 

Step 5: A view has been created and associated with the document. What we need to do now is the conversion between views. In voidCmultiviewapp: Onshowfirstview () to add the implementation code:

 

void CMultiViewApp::OnShowFirstview(){       // TODO: Add your command handler code here       UINT temp = ::GetWindowLong(m_pAnotherView->m_hWnd, GWL_ID);    ::SetWindowLong(m_pAnotherView->m_hWnd, GWL_ID, ::GetWindowLong(m_pFirstView->m_hWnd, GWL_ID));    ::SetWindowLong(m_pFirstView->m_hWnd, GWL_ID, temp);        m_pAnotherView->ShowWindow(SW_HIDE);       m_pFirstView->ShowWindow(SW_SHOW);       ((CFrameWnd*)m_pMainWnd)->SetActiveView(m_pFirstView);       ((CFrameWnd*) m_pMainWnd)->RecalcLayout();    m_pFirstView->Invalidate();}

 

In voidCmultiviewapp: Add implementation code in onshowsecondview:

void CMultiViewApp::OnShowSecondview(){       // TODO: Add your command handler code here       UINT temp = ::GetWindowLong(m_pAnotherView->m_hWnd, GWL_ID);    ::SetWindowLong(m_pAnotherView->m_hWnd, GWL_ID, ::GetWindowLong(m_pFirstView->m_hWnd, GWL_ID));    ::SetWindowLong(m_pFirstView->m_hWnd, GWL_ID, temp);       m_pFirstView->ShowWindow(SW_HIDE);       m_pAnotherView->ShowWindow(SW_SHOW);             ((CFrameWnd*)m_pMainWnd)->SetActiveView(m_pAnotherView);       ((CFrameWnd*) m_pMainWnd)->RecalcLayout();    m_pAnotherView->Invalidate();}

 

Step 6: For demonstration, different views are marked here.CmultiviewviewAndCanotherviewAdd the following code to the ondraw method:

pDC->TextOut(400,300,"First View");       pDC->TextOut(400,320,pDoc->GetTitle());

And

pDC->TextOut(400,300,"Another View");       pDC->TextOut(400,320,pDoc->GetTitle());

 

This is all done, but there are four points in the implementation process:

1) Because related classes are used in the implementation, the relevant header files should be included where necessary, which is omitted here;CanotherviewThe default constructor of is protected. You need to change it to public, or provideCanotherviewObject method (because you want to create a view object );

2) Here is a sample code. In actual development, you can obtain the specific application you want to implement through reference implementation (for example, different view classes and different quantities, more importantly, different implementations of business logic );

3) The sample code in this article has been uploaded to the blog and can be obtained through the following address.

 

From: http://www.cppblog.com/Lee7/archive/2008/12/22/70036.html

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.