程序如下:
void CMainFrame::OnExample(UINT nCmdID) //nCmdID为用户选择的菜单的ID
{
if (nCmdID == m_nCurrentExample)
return;  // already selected // Set the child window ID of the active view to AFX_IDW_PANE_FIRST.
// This is necessary so that CFrameWnd::RecalcLayout will allocate
// this "first pane" to that portion of the frame window's client
// area not allocated to control bars.  Set the child ID of
// the previously active view to some other ID; we will use the
// command ID as the child ID.
CView* pOldActiveView = GetActiveView();
::SetWindowLong(pOldActiveView->m_hWnd, GWL_ID, m_nCurrentExample);
//上句不懂,上面的英文也没看懂,我注释掉也未发现什么异常 CRuntimeClass* pNewViewClass;
switch (nCmdID)
{
case ID_STRINGLIST:
pNewViewClass = RUNTIME_CLASS(CStringListView);
break;
case ID_TYPEDLIST:
pNewViewClass = RUNTIME_CLASS(CTypedPtrListView);
break;
case ID_INTLIST:
pNewViewClass = RUNTIME_CLASS(CIntListView);
break;
case ID_DWORDARRAY:
pNewViewClass = RUNTIME_CLASS(CDWordArrayView);
break;
case ID_TYPEDPTRARRAY:
pNewViewClass = RUNTIME_CLASS(CTypedPtrArrayView);
break;
case ID_POINTARRAY:
pNewViewClass = RUNTIME_CLASS(CPointArrayView);
break;
case ID_MAPSTRINGTOSTRING:
pNewViewClass = RUNTIME_CLASS(CMapStringToStringView);
break;
case ID_TYPEDPTRMAP:
pNewViewClass = RUNTIME_CLASS(CTypedPtrMapView);
break;
case ID_MAPDWORDTOMYSTRUCT:
pNewViewClass = RUNTIME_CLASS(CMapDWordToMyStructView);
break;
default:
ASSERT(0);
return;
} // create the new view
CCreateContext context;
context.m_pNewViewClass = pNewViewClass;
context.m_pCurrentDoc = GetActiveDocument();
CView* pNewView = STATIC_DOWNCAST(CView, CreateView(&context));
if (pNewView != NULL)
{
// the new view is there, but invisible and not active...
pNewView->ShowWindow(SW_SHOW);
pNewView->OnInitialUpdate();
SetActiveView(pNewView);
RecalcLayout();
m_nCurrentExample = nCmdID; // finally destroy the old view...
pOldActiveView->DestroyWindow();
}
}

解决方案 »

  1.   

    ::SetWindowLong(pOldActiveView->m_hWnd, GWL_ID, m_nCurrentExample);
    //上句不懂,上面的英文也没看懂,我注释掉也未发现什么异常
      

  2.   

    void CMainFrame::SwitchViews(DWORD dwViewID)
    {
    CView* pOldActiveView = GetActiveView();
    CView* pNewActiveView = (CView*)GetDlgItem(dwViewID);
    if (pNewActiveView == NULL)
    {
    switch(dwViewID) 
    {
    case VIEW_REAGENT_CHEMINFO:
    pNewActiveView = (CView*)new CChemInfo;
    break;
    case VIEW_REAGENT_STORAGE:
    pNewActiveView = (CView*)new CChemicalsView;
    break;
    case VIEW_REAGENT_INUSE:
    pNewActiveView = (CView*)new CInuseView;
    break;
    case VIEW_REAGENT_ONORDER:
    pNewActiveView = (CView*)new COnOrderView;
    break;
    case VIEW_SUPPLIER:
    pNewActiveView = (CView*)new CSupplierView;
    break;
    case VIEW_SAMPLELOG:
    pNewActiveView = (CView*)new CSampleLogView;
    break;
    }

    CCreateContext context;
    context.m_pCurrentDoc = pOldActiveView->GetDocument();
    pNewActiveView->Create(NULL, NULL, 0L, CFrameWnd::rectDefault,
    this, dwViewID, &context);
    pNewActiveView->OnInitialUpdate();
    } SetActiveView(pNewActiveView);
    pNewActiveView->ShowWindow(SW_SHOW); if(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CChemInfo))
    dwViewID=VIEW_REAGENT_CHEMINFO;
    if(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CChemicalsView))
    dwViewID=VIEW_REAGENT_STORAGE;
    if(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CInuseView))
    dwViewID=VIEW_REAGENT_INUSE;
    if(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(COnOrderView))
    dwViewID=VIEW_REAGENT_ONORDER;
    if(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CSupplierView))
    dwViewID=VIEW_SUPPLIER;
    if(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CSampleLogView))
    dwViewID=VIEW_SAMPLELOG; pOldActiveView->ShowWindow(SW_HIDE);
    pOldActiveView->SetDlgCtrlID(dwViewID);
    pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
    RecalcLayout();
    pNewActiveView->SetWindowPos(&wndBottom, 0, 0, 0, 0,
    SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
    }
      

  3.   

    活动视图的ID必须为AFX_IDW_PANE_FIRST,既然你要销毁oldview,最好就把它的ID给换过来。