1. 程式人生 > >【VS開發】CTabView多頁卡介面

【VS開發】CTabView多頁卡介面

The CTabView class simplifies the use of the tab control class (CMFCTabCtrl ) in applications that use MFC's document/view architecture.

class CTabbedView : public CView
Members

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.

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。