使用VC轻松实现视图切换

 
 
步骤(一):
在CMainFrame类中添加CSplitterWnd类对象:
CSplitterWnd m_wndSplitter;

重载CMainFrame类的OnCreateClient()函数,代码如下:
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
    // TODO: Add your specialized code here and/or call the base class
    if( !m_wndSplitter.CreateStatic(this, 1, 2) )
    {
      return FALSE;
    }

    CRect bClientRect;
    GetClientRect( &bClientRect );
    if ( !m_wndSplitter.CreateView(0, 0, pContext->m_pNewViewClass,
         CSize(bClientRect.Width()/4, 50), pContext) )
    {
       return FALSE;
    }

    if ( !m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CEliteWelcomeView),
         CSize(200, 50), pContext ) )
    {
       return FALSE;
    }

    SetActiveView( (CView *)m_wndSplitter.GetPane( 0, 1 ) );

    return TRUE;
}

步骤(二):
添加自定义消息:
#define   WM_CHANGEDVIEW    WM_USER+100

添加自定义消息处理函数:
afx_msg void OnChangedeView(WPARAM wParam, LPARAM lParam);

函数实现(本范例在Client创建了两个视,其中第一个是是固定显示TreeView,第二个视根据第一个视的选项来进行切换):
void CMainFrame::OnChangedeView( WPARAM wParam, LPARAM lParam )
{
    CCreateContext bContext;
    UINT uiDlgID = (UINT)wParam;

    CDocument *pDoc = ((CView *)m_wndSplitter.GetPane(0, 1))->GetDocument();

    pDoc->m_bAutoDelete = FALSE;
    ((CView *)m_wndSplitter.GetPane(0, 1))->DestroyWindow();
    pDoc->m_bAutoDelete = TRUE;

    switch( uiDlgID )
    {
     case IDD_WECLOME:
          bContext.m_pNewViewClass = RUNTIME_CLASS(CEliteWelcomeView);
          break;
     case IDD_ELITEIV:
          bContext.m_pNewViewClass = RUNTIME_CLASS(CEliteIVView);
          break;
     }

    bContext.m_pCurrentDoc = pDoc;
    bContext.m_pNewDocTemplate = NULL;
    bContext.m_pLastView = NULL;
    bContext.m_pCurrentFrame = NULL;

    m_wndSplitter.CreateView(0, 1, bContext.m_pNewViewClass, CSize(200, 50), &bContext);

    m_wndSplitter.RecalcLayout();
}

步骤(三):
在OnInitialUpdate函数中添加代码(CEliteToolView视类是从CTreeView类继承)
void CEliteToolView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();
    // TODO: You may populate your TreeView with items by directly accessing
    //  its tree control through a call to GetTreeCtrl().

    GetTreeCtrl().InsertItem("Test First", 0, 0);
    GetTreeCtrl().InsertItem("Test Second", 0, 0);
}

步骤(四):
重载CEliteToolView视类的TVN_SELCHANDED消息:
void CEliteToolView::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
{
     NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
     // TODO: Add your control notification handler code here

     HTREEITEM hTreeItem = GetTreeCtrl().HitTest( pNMTreeView->ptDrag );

     CString strItemText = GetTreeCtrl().GetItemText(hTreeItem);

     if (0xFFFFFFFF != strItemText.Find("Test First", 0))
     {
        ::SendMessage(AfxGetApp()->GetMainWnd()->m_hWnd, WM_CHANGEDVIEW, IDD_WECLOME, 0);
      }

      if (0xFFFFFFFF != strItemText.Find("Test Second", 0))
     {
        ::SendMessage(AfxGetApp()->GetMainWnd()->m_hWnd, WM_CHANGEDVIEW, IDD_ELITEIV, 0);
      }

      *pResult = 0;
}
完成上述几个步骤以后,即可以完成多个视图之间的任意切换
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值