The CTabView class simplifies the use of the tab control class ( ) in applications that use MFC's document/view architecture.
class CTabbedView : public CView
Public Methods
Name | Description |
| Adds a new view to the tab control. |
| Returns the index of the specified view in the tab control. |
| Returns a pointer to the currently active view |
| Returns a reference to the tab control associated with the view. |
| Removes the view from the tab control. |
| Makes a view active. |
Protected Methods
Name | Description |
| Called by the framework when creating a tab view to determine whether the tab view has a shared horizontal scroll bar. |
| Called by the framework when the tab view is made active or inactive. |
使用CTabView要特别注意获取视图的指针的操作,一般的途径获取只能获取CTabView里面的当前View不能获取到CTabView指针,必须通过下面方法获取,以下为在主框架获取CTabView视图指针的示例:
void CMainFrame:: OnGetBlog() { CChildFrame * pChildFrm = ( CChildFrame *) GetActiveFrame(); CView * pView = pChildFrm-> GetActiveView(); CMFCTabCtrl * pParent1 = ( CMFCTabCtrl *) pView-> GetParent(); CXXXTabView * pTabView =( CXXXTabView *) pParent1-> GetParent(); pTabView-> OnBlog(); //调用CTabView视图类里面的函数}
要禁止CTabView里面的Tab拖动,只需要在CTabView里面调用下面:
this -> GetTabControl().EnableTabSwap( FALSE );
一些CTabView样式设置,如下:
void CXXXTabView:: OnInitialUpdate() { CTabView:: OnInitialUpdate(); AddView ( RUNTIME_CLASS ( CView1), _T( " simple " ), 100 ); this -> GetTabControl().SetLocation( CMFCTabCtrl:: LOCATION_TOP); //方向上顶 this -> GetTabControl().ModifyTabStyle( CMFCTabCtrl:: STYLE_3D_ONENOTE); //风格 this -> GetTabControl().EnableAutoColor( TRUE ); //自动着色 this -> GetTabControl().SetTabBorderSize( 2 ); //边框大小 this -> GetTabControl().HideSingleTab( TRUE ); //单个Tab时候不显示Tab标签 this -> GetTabControl().EnableTabSwap( FALSE ); //禁止拖动}
若是要禁止CTabView上的滚动条,只要在CTabView的头文件上,定义以下函数即可:
BOOL IsScrollBar () const { return FALSE ; }
在基于CTabView的多文档中,遍历每个CTabView视图可以通过获取框架指针。下面是关闭除当前视图外的其余视图:
void CMainFrame::OnFileAllClose(){ CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd; CMDIChildWnd *pChild = (CMDIChildWnd*)pFrame->GetActiveFrame(); CView * pView; CMFCTabCtrl * pParent1; CXXXTabView * pTabView; CDocument* pDoc; CMDIChildWnd *pChild2=pFrame->MDIGetActive(); if (pFrame) { //依次关闭右边视图 pFrame->MDINext(); pChild2=pFrame->MDIGetActive(); while (pChild2!=pChild) { pView = pChild2->GetActiveView(); pParent1 = (CMFCTabCtrl *)pView->GetParent(); pTabView =(CXXXTabView *) pParent1->GetParent(); pDoc = pTabView->GetDocument(); pDoc->OnCloseDocument(); pChild2=pFrame->MDIGetActive(); } //依次关闭左边视图 pFrame->MDIPrev(); pChild2=pFrame->MDIGetActive(); while (pChild2!=pChild) { pView = pChild2->GetActiveView(); pParent1 = (CMFCTabCtrl *)pView->GetParent(); pTabView =(CXXXTabView *) pParent1->GetParent(); pDoc = pTabView->GetDocument(); pDoc->OnCloseDocument(); pFrame->MDIPrev(); pChild2=pFrame->MDIGetActive(); } } } 更多的资料,可以参考MSDN。