How to enumerate all views of the document

17 03 2010

 

 

A document object can have many views. How to enumerate and communicate with each view individually from document.

 

 

CDocument class has the following members GetFirstViewPosition and GetNextView to enumerate all views associated with the document. Then call view’s OnUpdate function to communicate with them.

 

 

void EnumerateViews()
{
   //get the position of the first view in the list
   //of views associated with the document.
   POSITION pos = GetFirstViewPosition();

   //terate through all of the document's views.
   while (pos != NULL)
   {
      CView* pView = GetNextView(pos);

      //update view
      pView->OnUpdate(pSender, lHint, pHint);
   }
}

 

 

CDocument’s UpdateAllViews using same logic to update all views associated with the document.

Advertisements




How to use CSplitterWnd class

11 03 2010

 

 

If you want to create an SDI application with three views, that means we have to split the main window into three different parts and each window have to handle separately. How can we do it?

 

 

MFC provides CSplitterWnd class to establish the functionality of a splitter window, which is a window that contains multiple panes. 

In our case we need three different views, so first of all we have to derive three new classes from CFormView to our SDI application and attach three windows to these classes. Then overrides parent frame’s OnCreateClient function and within that function call the CreateStatic member function of CSplitterWnd with number of  rows and columns specifications. Then we can put the views in the splitter panes using CreateView function.

 

 

include "MyView1.h"
#include "MyView2.h"
#include "MyView3.h"

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
   return CreateSplitters(pContext);
}
bool CMainFrame::CreateSplitters( CCreateContext* pContext )
{
   const bool cbCreated
      = TRUE == m_TimelineSplitter.CreateStatic(
      this,
      1,
      3 );
   int nxScreen, nyScreen;
   nxScreen = GetSystemMetrics(SM_CXFULLSCREEN);
   nyScreen = GetSystemMetrics(SM_CYFULLSCREEN);
   if( cbCreated )
   {
      // Put the views in the splitter panes
      m_TimelineSplitter.CreateView(
         0,
         0,
         RUNTIME_CLASS( MyView1 ),
         CSize(nxScreen/3.5,nyScreen/3.6),
         pContext );
      m_TimelineSplitter.CreateView(
         0,
         1,
         RUNTIME_CLASS( MyView1 ),
         CSize( nxScreen/2.8,nyScreen/2.7 ),
         pContext );
      m_TimelineSplitter.CreateView(
         0,
         2,
         RUNTIME_CLASS( MyView1 ),
         CSize(nxScreen,nyScreen/1.4 ),
         pContext );
   }
   return cbCreated;
}

 

 

Use CSplitterWnd::Create to create a dynamic splitter window and use CSplitterWnd::CreateStatic to create a static splitter window.