void CMainFrame::OnViewSwitchview()
{
// TODO: Add your command handler code here
CView* pView=(CView*)m_SplitterCol.GetPane(0,0);
if (pView->IsKindOf(RUNTIME_CLASS(CTreeView)))
{
m_SplitterCol.DeleteView(0,0);
CCreateContext Context;

Context.m_pCurrentDoc = GetActiveView()->GetDocument();
Context.m_pCurrentFrame = this;
Context.m_pNewViewClass = RUNTIME_CLASS(CTomListView);

m_SplitterCol.CreateView(0,0,Context.m_pNewViewClass,CSize(333,200),&Context);

//m_SplitterRow.CreateView(0,0,RUNTIME_CLASS(CListView),CSize(200,200),NULL);
pView->GetParentFrame()->RecalcLayout();
m_SplitterCol.RecalcLayout();       pView->OnInitialUpdate();
m_SplitterCol.SetActivePane(0,0);
}         
}
void CTomListView::OnInitialUpdate()
{
CListView::OnInitialUpdate(); // TODO: Add your specialized code here and/or call the base class CListCtrl &ListCtrl = GetListCtrl(); ListCtrl.ModifyStyle(0,LVS_REPORT);
ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES); ListCtrl.InsertColumn(0,_T("Column 1"),LVCFMT_LEFT,100);
}我通过菜单点击,将原来左边的TreeView功换成CListView,切换没问题,但加上视图初始化OnInitialUpdate后便出现异常,原因找不到,希望能人指点!

解决方案 »

  1.   

    注释掉这两句试试看!
    pView->GetParentFrame()->RecalcLayout();  
    pView->OnInitialUpdate();
    异常错误是什么?
      

  2.   

    改了下代码,试下还有问题不。。void CMainFrame::OnViewSwitchview()
    {
    // TODO: Add your command handler code here
    CView* pView=(CView*)m_SplitterCol.GetPane(0,0);
    if (pView->IsKindOf(RUNTIME_CLASS(CTreeView)))
    {
    CRect rcClient;
    pView->GetWindowRect(&rcClient);
    SIZE size;
    size.cx = rcClient.Width();
    size.cy = rcClient.Height(); CCreateContext context; CDocument* pDoc = pView->GetDocument(); pDoc->m_bAutoDelete = FALSE;
    pView->DestroyWindow();
    pDoc->m_bAutoDelete = TRUE; context.m_pNewViewClass=RUNTIME_CLASS(CTomListView);
    context.m_pCurrentDoc=pDoc;
    context.m_pNewDocTemplate=NULL;
    context.m_pLastView=NULL; 
    context.m_pCurrentFrame=NULL; m_SplitterCol.CreateView(0, 0, RUNTIME_CLASS(CTomListView), size, &Context); m_SplitterCol.RecalcLayout();    pView->OnInitialUpdate();
    m_SplitterCol.SetActivePane(0,0);
    }
      

  3.   

    1) pView->OnInitialUpdate();
       这个需要显式调用吗? 
    2)如果要显式调用是不是要放到RecalcLayout() 之类的前面,3) 不建议每次切换的时候就 删除旧的,创建新的 
        我的做法是:左边创建两个View, 其中一个隐藏,另外一个占用左边的全部空间。