Add multiple views to a single document in MFC

Transfer: http://ffwmxr.blog.163.com/blog/static/66372722201001605539213/

 

Multi-view is one of the technologies that are often used in VC development. Generally, there are two ways to realize single-document multi-view:

1) Through the view segmentation technology (implemented by CSplitterWnd ), the window is divided into multiple parts, and each part displays different views. This technology is relatively simple to implement, and there are many related materials.

2) Associate multiple views through a document, and the window displays the entire view.

The second implementation is more complicated than the first, and the detailed implementation method is given here.

Step 1 : Use VC 6.0 to create a new Project named: MultiView. Use the "default" method for everything except selecting single document properties . So you can get five classes: CMainFrame  , CMultiViewApp , CMultiViewDoc , CMultiViewView , and CAboutDlg ;

 

Step 2 : Create a new view View, add a new MFC Class (Insert->New Class), the base class is CView (or a derived subclass of CView, such as CEditView, etc.). The class name is CAnotherView , which is the new view; and add an implementation of GetDocument for CAnotherView :

 

CMultiViewDoc* CAnotherView::GetDocument()

{

       return (CMultiViewDoc*)m_pDocument;

}

 

 

Step 3 : Add member variables to CMultiViewApp to record these two views:

 

private:

       CView* m_pFirstView;

       CView* m_pAnotherView;

 

Add a menu item "View" to the program menu IDR_MAINFRAME , this menu item has two submenus "View One" and "View Two", and add corresponding functions (void  CMultiViewApp ::OnShowFirstview() and void  CMultiViewApp ::OnShowSecondview());

 

Step 4 : Create a new view: Add code in BOOL  CMultiViewApp ::InitInstance():

 

…….

//create a new view

       CView* m_pActiveView = ((CFrameWnd*)m_pMainWnd)->GetActiveView();

       m_pFirstView = m_pActiveView;

      

       m_pAnotherView = new CAnotherView();

       // Implement document and view association

       CDocument* m_pDoc = ((CFrameWnd*)m_pMainWnd)->GetActiveDocument();

 

       CCreateContext context;

       context.m_pCurrentDoc = m_pDoc;

 

       //Create the implementation of the view

       UINT m_IDFORANOTHERVIEW = AFX_IDW_PANE_FIRST + 1;

       CRect rect;

       m_pAnotherView->Create(NULL,NULL,WS_CHILD,rect,m_pMainWnd,

m_IDFORANOTHERVIEW,&context);

    ……

 

 

Step 5 : The view has now been created and associated with the document. All we have to do now is the transition between views. Add implementation code in void  CMultiViewApp ::OnShowFirstview():

 

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();

}

 

 

在void CMultiViewApp:: 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:为了演示,这里将不同的视图给予一个标记,在CMultiViewViewCAnotherView的OnDraw方法中分别添加以下代码:

 

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

       pDC->TextOut(400,320,pDoc->GetTitle());

 

 

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

       pDC->TextOut(400,320,pDoc->GetTitle());

 

 

至此就大功告成了,但是实现过程中有4点说明:

1) 实现中由于使用到相关的类,因此在必要的地方要include相关的头文件,这里省略;CAnotherView的默认构造函数是Protected的,需要将其改为Public,或者提供一个产生CAnotherView对象的方法(因要创建视图对象);

2) 这里给出的是一个示例代码,实际开发中可以通过参考实现获得自己想要实现的具体应用情况(例如视图类的不同、数量不同,更重要的还有业务逻辑的不同实现等);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325256429&siteId=291194637