Adding multiple views to a single document

Source: Internet
Author: User

In a single-Document Interface (SDI) application created with the Microsoft Foundation Class (MFC) library, each document type is associated with a single view type. in some cases, it is desirable to have the ability to switch the current view of a document
With a new view.

Tip

For additional procedures on implementing multiple views for a single document, see
Cdocument: addview and
Collect MFC sample.

You can implement this functionality by adding a newCview-Derived class and additional code for switching the views dynamically to an existing MFC application.

The steps are as follows:

  • Modify the existing application class

  • Create and modify the New View class

  • Create and attach the New View

  • Implement the switching function

  • Add support for switching the view

The remainder of this topic assumes the following:

  • The name ofCwinapp-Derived object is
    Cmywinapp, and cmywinapp is declared and defined in mywinapp. h and mywinapp. cpp.

  • Cnewview is the name of the newCview-Derived object, and
    Cnewview is declared and defined in newview. h and newview. cpp.

Modify the existing application class

For the application to switch between views, You need to modify the application class by adding member variables to store the views and a method to switch them.

Add the following code to the declaration of cmywinapp in mywinapp. h:

C ++
CView* m_pOldView;CView* m_pNewView;CView* SwitchView( );

The new member variables, m_poldview and
M_pnewview, point to the current view and the newly created one. The new method (switchview) switches the views when requested by the user. The body of the method is discussed later in this topic in

Implement the switching function.

The last modification to the application class requires including a new header file that defines a Windows message (Wm_initialupdate) That is used in the switching function.

Insert the following line in the include section of mywinapp. cpp:

C ++ copy
#include <AFXPRIV.H>

Save your changes and continue to the next step.

Create and modify the New View class

Creating the New View class is made easy by usingNew ClassCommand available from Class View. The only requirement for this class is that it derives from
Cview. Add this new class to the application. For specific information on adding a new class to the project, see
Adding a class.

Once you have added the class to the project, you need to change the accessibility of Some View class members.

Modify newview. h by changing the access specifier from protected
PublicFor the constructor and destructor. This allows the class to be created and destroyed dynamically and to modify the view appearance before it is visible.

Save your changes and continue to the next step.

Create and attach the New View

To create and attach the new view, you need to modifyInitinstanceFunction of your application class. The modification adds new code that creates a new view object and then initializes both
M_poldview and m_pnewview with the two existing view objects.

Because the new view is created withinInitinstanceFunction, both the new and existing views persist for the lifetime of the application. However, the application cocould just as easily create the new view dynamically.

Insert this code after the call to processshellcommand:

C ++
CView* pActiveView = ((CFrameWnd*) m_pMainWnd)->GetActiveView();m_pOldView = pActiveView;m_pNewView = (CView*) new CNewView;if (NULL == m_pNewView)   return FALSE;CDocument* pCurrentDoc = ((CFrameWnd*)m_pMainWnd)->GetActiveDocument();// Initialize a CCreateContext to point to the active document. // With this context, the new view is added to the document // when the view is created in CView::OnCreate().CCreateContext newContext;newContext.m_pNewViewClass = NULL;newContext.m_pNewDocTemplate = NULL;newContext.m_pLastView = NULL;newContext.m_pCurrentFrame = NULL;newContext.m_pCurrentDoc = pCurrentDoc;// The ID of the initial active view is AFX_IDW_PANE_FIRST. // Incrementing this value by one for additional views works // in the standard document/view case but the technique cannot // be extended for the CSplitterWnd case.UINT viewID = AFX_IDW_PANE_FIRST + 1;CRect rect(0, 0, 0, 0); // Gets resized later. // Create the new view. In this example, the view persists for // the life of the application. The application automatically // deletes the view when the application is closed.m_pNewView->Create(NULL, _T("AnyWindowName"), WS_CHILD, rect, m_pMainWnd, viewID, &newContext);// When a document template creates a view, the WM_INITIALUPDATE // message is sent automatically. However, this code must // explicitly send the message, as follows.m_pNewView->SendMessage(WM_INITIALUPDATE, 0, 0);

Save your changes and continue to the next step.

Implement the switching function

In the previous step, you added code that created and initialized a new view object. The last major piece is to implement the switching method,
Switchview.

At the end of the implementation file for your application class (mywinapp. cpp), add the following method definition:

C ++
CView* CMyWinApp::SwitchView( ){   CView* pActiveView = ((CFrameWnd*) m_pMainWnd)->GetActiveView();   CView* pNewView = NULL;   if(pActiveView == m_pOldView)      pNewView = m_pNewView;   else      pNewView = m_pOldView;   // Exchange view window IDs so RecalcLayout() works.   #ifndef _WIN32   UINT temp = ::GetWindowWord(pActiveView->m_hWnd, GWW_ID);   ::SetWindowWord(pActiveView->m_hWnd, GWW_ID, ::GetWindowWord(pNewView->m_hWnd, GWW_ID));   ::SetWindowWord(pNewView->m_hWnd, GWW_ID, temp);   #else   UINT temp = ::GetWindowLong(pActiveView->m_hWnd, GWL_ID);   ::SetWindowLong(pActiveView->m_hWnd, GWL_ID, ::GetWindowLong(pNewView->m_hWnd, GWL_ID));   ::SetWindowLong(pNewView->m_hWnd, GWL_ID, temp);   #endif   pActiveView->ShowWindow(SW_HIDE);   pNewView->ShowWindow(SW_SHOW);   ((CFrameWnd*) m_pMainWnd)->SetActiveView(pNewView);   ((CFrameWnd*) m_pMainWnd)->RecalcLayout();   pNewView->Invalidate();   return pActiveView;} 

Save your changes and continue to the next step.

Add support for switching the view

The final step involves adding code that callthe switchview method when the application needs to switch between views. this can be done in several ways: by either adding a new menu item for the user to choose or switching
Views internally when certain conditions are met.

For more information on adding new menu items and command handler functions, see
Handlers for commands and control communications.

See also

Conceptsdocument/view architecture

Note: This article Reprinted from: http://msdn.microsoft.com/en-us/library/s199bks0.aspx

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.