如何实现SDI程序使用CSplitterWnd创建的多个视图的动态地显示和关闭视图

为了实现该功能我们需要对CSplitterWnd进行增强,具体实现如下:

一、定义头文件


显示代码打印01 ////////////////////////////////////////////////////////////////// 

02 // 

03 // splitex.h 

04 class CSplitterWndEx : public CSplitterWnd 

05 { 

06 protected: 

07    int m_nHidedCol;    // hide column number, -1 if all columns 

08                        // are shown 

09   

10 public: 

11    CSplitterWndEx(); 

12   

13    void ShowColumn(); 

14    void HideColumn(int colHide); 

15   

16 // ClassWizard generated virtual function overrides 

17    //{{AFX_VIRTUAL(CSplitterWndEx) 

18    //}}AFX_VIRTUAL 

19   

20 // Generated message map functions 

21 protected: 

22    //{{AFX_MSG(CSplitterWndEx) 

23       // NOTE - the ClassWizard will add and remove member 

24       //        functions here. 

25       //}}AFX_MSG 

26   

27     DECLARE_MESSAGE_MAP() 

28 };

 

 

二、具体实现代码


显示代码打印001 ////////////////////////////////////////////////////////////////// 

002 // 

003 // splitex.cpp 

004 #include "stdafx.h" 

005 #include "splitex.h" 

006   

007 #ifdef _DEBUG 

008 #define new DEBUG_NEW 

009 #undef THIS_FILE 

010 static char THIS_FILE[] = __FILE__; 

011 #endif 

012   

013 ////////////////////////////////////////////////////////////////// 

014 / 

015 // CSplitterWndEx 

016   

017 CSplitterWndEx::CSplitterWndEx() : 

018     m_nHidedCol(-1) 

019 { 

020 } 

021   

022 void CSplitterWndEx::ShowColumn() 

023 { 

024    ASSERT_VALID(this); 

025    ASSERT(m_nCols < m_nMaxCols); 

026    ASSERT(m_nHidedCol != -1); 

027   

028    int colNew = m_nHidedCol; 

029    m_nHidedCol = -1; 

030    int cxNew = m_pColInfo[m_nCols].nCurSize; 

031    m_nCols++;    // add a column 

032    ASSERT(m_nCols == m_nMaxCols); 

033   

034    // fill the hidden column 

035    int col; 

036    for (int row = 0; row < m_nRows; row++) 

037    { 

038       CWnd* pPaneShow = GetDlgItem( 

039          AFX_IDW_PANE_FIRST + row * 16 + m_nCols); 

040       ASSERT(pPaneShow != NULL); 

041       pPaneShow->ShowWindow(SW_SHOWNA); 

042   

043       for (col = m_nCols - 2; col >= colNew; col--) 

044       { 

045          CWnd* pPane = GetPane(row, col); 

046          ASSERT(pPane != NULL); 

047          pPane->SetDlgCtrlID(IdFromRowCol(row, col + 1)); 

048       } 

049   

050       pPaneShow->SetDlgCtrlID(IdFromRowCol(row, colNew)); 

051    } 

052   

053    // new panes have been created -- recalculate layout 

054    for (col = colNew + 1; col < m_nCols; col++) 

055       m_pColInfo[col].nIdealSize = m_pColInfo[col - 1].nCurSize; 

056    m_pColInfo[colNew].nIdealSize = cxNew; 

057    RecalcLayout(); 

058 } 

059   

060 void CSplitterWndEx::HideColumn(int colHide) 

061 { 

062    ASSERT_VALID(this); 

063    ASSERT(m_nCols > 1); 

064    ASSERT(colHide < m_nCols); 

065    ASSERT(m_nHidedCol == -1); 

066    m_nHidedCol = colHide; 

067   

068    // if the column has an active window -- change it 

069    int rowActive, colActive; 

070    if (GetActivePane(&rowActive, &colActive) != NULL && 

071        colActive == colHide) 

072    { 

073       if (++colActive >= m_nCols) 

074          colActive = 0; 

075       SetActivePane(rowActive, colActive); 

076    } 

077   

078    // hide all column panes 

079    for (int row = 0; row < m_nRows; row++) 

080    { 

081       CWnd* pPaneHide = GetPane(row, colHide); 

082       ASSERT(pPaneHide != NULL); 

083       pPaneHide->ShowWindow(SW_HIDE); 

084       pPaneHide->SetDlgCtrlID( 

085          AFX_IDW_PANE_FIRST + row * 16 + m_nCols); 

086   

087       for (int col = colHide + 1; col < m_nCols; col++) 

088       { 

089          CWnd* pPane = GetPane(row, col); 

090          ASSERT(pPane != NULL); 

091          pPane->SetDlgCtrlID(IdFromRowCol(row, col - 1)); 

092       } 

093    } 

094    m_nCols--; 

095    m_pColInfo[m_nCols].nCurSize = m_pColInfo[colHide].nCurSize; 

096     RecalcLayout(); 

097 } 

098   

099 BEGIN_MESSAGE_MAP(CSplitterWndEx, CSplitterWnd) 

100 //{{AFX_MSG_MAP(CSplitterWndEx) 

101    // NOTE - the ClassWizard will add and remove mapping macros here. 

102 //}}AFX_MSG_MAP 

103 END_MESSAGE_MAP()

 


文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/3_program/c++/cppsl/2007919/72107.html

posted on 2011-01-06 19:25  cy163  阅读(1271)  评论(0编辑  收藏  举报

导航