Adding Multiple Views to a Single Document

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 the COLLECT MFC sample.

You can implement this functionality by adding a new CView-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 of the CWinApp-derived object is CMyWinApp, and CMyWinApp is declared and defined in MYWINAPP.H and MYWINAPP.CPP.

  • CNewView is the name of the new CView-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:

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:

#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 using the New Class command 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 to public for 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 modify the InitInstance function 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 within the InitInstance function, both the new and existing views persist for the lifetime of the application. However, the application could just as easily create the new view dynamically.

Insert this code after the call to ProcessShellCommand:

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:

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 calls the 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 the 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 Notifications.

See Also

Concepts

Document/View Architecture