CDocument::AddView

void AddView(CView* pView);

参数:
pView被添加的视图指针。

说明:
调用该成员函数将视图添加到文档中。该函数将指定视图加入与文档相联系的视图列表之中。函数还设置指向文档的视图指针。当添加一个新创建的视图对象到文档时,框架调用该函数。这是对File Open,FileNew,新的Windows 或窗口被分隔等作出的反应。
仅在手工创建和添加视图时调用该函数。通常通过定义与文档类、视图类和框架窗口类相联系的CDocTemplate对象来连接框架窗口与视图、文档。

示例:
// The following example toggle two views in an SDI(single document
// interface) frame window.A design decision must be made as to
// whether to leave the inactive view connected to the document,
// such that the inactive view continues to receive OnUpdate
// keep the inactive view continuously in sync with the document,even
//though it is inactive However,doing so incurs a performance cost,
// as well as the programming cost of implementing OnUpdate hints.It may be less expensive,in terms of performance and/or programming,
// to re-sync the inactive view with the document only with it is
// reactivated. This example illustrates this latter approach,by
// reconnecting the newly active view and disconnecting the newly
// inactive view,via calls to CDocument::AddView and RemoveView.
BOOL CMainFrame::OnViewChange(UNIT nCmdID)
{
  CView* pViewAdd;CView* pViewRemove;
  CDocument* pDoc = GetActiveDocument();
  UNIT nCmdID;nCmdID = LOWORD(GetCurrentMessage()->wParam);
  if ((nCmdID = = ID_VIEW_VIEW1)&&(m_currentView = =1)) return;
  if ((nCmdID = = ID_VIEW_VIEW2)&&(m_currentView = =2))return;
  if (nCmdID = = ID_VIEW_VIEW2)
  {
    if (m_pView2 = = NULL)
    {
      m_pView1 = GetActiveView();
      m_pView2 = new CMyView2;
      //Note that if onSize has beenoverridden in CMy View2
      //and GetDoument()is used in this overrideit can
      //cause assertions and,if theassertions are ignored,
      //cause access violation.
      m_pView2->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, rectDefault,
                       this, AFX_IDW_PANE_FIRST+1, NULL
                      );
    }

    pViewAdd = m_pView2;pViewRemove = m_pView1;
    m_currentView = 2
  }
  else
  {
     pViewAdd = m_pView1;
     pViewRemove = m_pView2;
     m_currentView = 1;
  }
  // Set the child i.d. of the active view to AFX_IDW_PANE_FIRST,
  // so that CFrameWnd::RecalcLayout will allocate to this
  // "first pane" that portion of the frame window's client area
  // not allocated to control bars.Set the child i.d. of the
  // other view to anything other than AFX_IDW_PANE_FIRST;this
  // examples switches the child id's of the two views.

  int nSwitchChildID = pViewAdd->GetDlgCtrlID();
  pViewAdd->SetDlgCtrlID(AFX_LDW_PANE_FIRST);
  pViewRemove->SetDlgCtrlID(nSwitchChildID);

  // Show the newly active view and hide the inactive view.

  pViewAdd->ShowWindow(SW_SHOW);
  pViewRemove->ShowWindow(SW_HIDE);

  // Connect the newly active view to the document,and
  // disconnect the inactive view.

  pDoc->AddView(pViewAdd);
  pDoc->RemoveView(pViewRemove);
  SetActiveView(pViewAdd);
  RecalcLayout();
}

请参阅:
CDocTemplate, CDocument::GetFirstViewPosition, CDocument::GetNextView, CDocument::RemoveView, CView::GetDocument