博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

VC 树状 视图 切换 动态切换视图窗口 (转)

Posted on 2007-10-20 14:54  arowana  阅读(1950)  评论(2编辑  收藏  举报
本文转自 http://www.vczx.com/article/show.php?id=1132
文章标题:动态切换视图窗口
原 作 者:程红秀




在CMainFrame中定义变量:

CSplitterWnd m_wndSplitter;

BOOL CMainFrame::ReplaceView(
int row, int col, CRuntimeClass *pViewClass, SIZE
    size)
{
    CCreateContext context;
    BOOL bSetActive;

    
if ((this->m_wndSplitter.GetPane(row, col)->IsKindOf(pViewClass)) == TRUE)
        
return FALSE;

    
//获取文档对象的指针,以便在创建新视图的过程中能够使用它
    CDocument *pDoc = ((CView*)m_wndSplitter.GetPane(row, col))->GetDocument();

    CView 
*pActiveView = this->GetActiveView();
    
if (pActiveView == NULL || pActiveView == m_wndSplitter.GetPane(row, col))
        bSetActive 
= TRUE;
    
else
        bSetActive 
= FALSE;

    pDoc
->m_bAutoDelete = FALSE; //设置标志,这样当视图销毁时不会删除文档
    ((CView*)m_wndSplitter.GetPane(row, col))->DestroyWindow(); //删除存在的视图
    pDoc->m_bAutoDelete = TRUE; //设回默认的标志


    
//创建新视图
    context.m_pNewViewClass = pViewClass;
    context.m_pCurrentDoc 
= pDoc;
    context.m_pNewDocTemplate 
= NULL;
    context.m_pLastView 
= NULL;
    context.m_pCurrentFrame 
= NULL;
    m_wndSplitter.CreateView(row, col, pViewClass, size, 
&context);


    CView 
*pNewView = (CView*)m_wndSplitter.GetPane(row, col);

    
if (bSetActive == TRUE)
        
this->SetActiveView(pNewView);

    m_wndSplitter.RecalcLayout(); 
//重新计算位置
    
// m_wndSplitter.GetPane(row,col)->SendMessage(WM_PAINT);

    
return TRUE;
}


BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext 
*pContext)
{
    
if (!m_wndSplitter.CreateStatic(this12))
        
return FALSE;

    
if (!m_wndSplitter.CreateView(00, RUNTIME_CLASS(CTView), CSize(200100),
        pContext))
        
return FALSE;

    
if (!m_wndSplitter.CreateView(01, RUNTIME_CLASS(CFView), CSize(100100),
        pContext))
        
return FALSE;

    m_bFormView 
= true;
    
return TRUE;
}



void CMainFrame::OnFormView()
{
    ReplaceView(
01, RUNTIME_CLASS(CFView), CSize(100100));
    m_bFormView 
= true;
}


void CMainFrame::OnListView()
{
    ReplaceView(
01, RUNTIME_CLASS(CVVView), CSize(100100));
    m_bFormView 
= false;

}


void CMainFrame::OnUpdateFormView(CCmdUI *pCmdUI)
{
    pCmdUI
->SetCheck(m_bFormView);
}


void CMainFrame::OnUpdateListView(CCmdUI *pCmdUI)
{
    pCmdUI
->SetCheck(!m_bFormView);
}


源代码下载