Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC

A Static Splitter with the Ability to Hide/Show Multiple Columns/Rows

Rate me:
Please Sign up or sign in to vote.
4.75/5 (14 votes)
6 Aug 20022 min read 130.4K   2.9K   35   13
A static splitter with the ability to hide/show multiple columns/rows

Image 1

Introduction

When I was working on an application, I realized that it needed a static splitter with the ability to hide/show several rows or columns simultaneously. Surprisingly, I couldn't find such component. The closest solution was proposed by Oleg Galkin here. His splitter can hide/show one of the splitter rows (columns), but only one. A static splitter with the ability to hide/show multiple columns/rows is proposed in this article.

Problems with Algorithm Extension

MFC CSplitterWnd accesses its panes by ID and ID defines the position of a pane in the splitter. Assume that column n is to be hidden. In Oleg Galkin's algorithm, column n gets the last column ID. The columns following n are shifted to the left by 1, i.e., ID of n+1 control is assigned to control n, n+2 ID is assigned to n+1, etc. Attempts to extend this approach for multiple column hiding led to an overcomplicated algorithm. Every time you hide column, the IDs of columns hidden in previous operations are changed again. So if you hide three columns, the IDs of some controls are changed three times. It is a non-trivial problem to trace all of these changes.

New Approach

CExtSplitter class uses absolute and relative addresses to operate with splitter panes. The absolute column address is an initial column number and the relative address is current column number in splitter. HideColumn and ShowColumn public functions work with absolute addresses. Relative addresses are used internally. CExtSplitter class saves pointers to all controls in an internal array. The array initialized once when the splitter is created and doesn't change during the splitter existence. Rows and columns of this array are used to access splitter panes by absolute ID. If the splitter was initialized with m rows and n columns then hiding column k (0 < k < n), means hide the column that was column k initially. Note that after several hide operations, column k can appear in the splitter at any position less than k.

Implementation Details

CExtSplitter class has a list of shown and hidden columns. The value of a list member is an absolute column address and position is and relative column address.

C++
class CExtSplitter : public CSplitterWnd
{
public:
    typedef std::list < int > LIST_INT;

    CExtSplitter();   

    virtual ~CExtSplitter();

    BOOL CreateStatic(CWnd* pParentWnd,
        int nRows, int nCols,
        DWORD dwStyle = WS_CHILD | WS_VISIBLE,
        UINT nID = AFX_IDW_PANE_FIRST);

    virtual BOOL CreateView( int row, int col, 
        CRuntimeClass* pViewClass, SIZE sizeInit,
        CCreateContext* pContext );

    void    HideColumn(int colHide);
    void    ShowColumn(int colShow);
    void    HideRow(int colRow);
    void    ShowRow(int row);

public: 
    LIST_INT m_shown_cols;      //shown  column list
    LIST_INT m_hid_cols;        //hidden column list
    LIST_INT m_shown_rows;      //shown  rows list
    LIST_INT m_hid_rows;        //hidden rows list

protected:
    //array of pointers to splitter panes
    C2DArray m_pane_ptr_array;  
};

Function HideColumn moves column from list of shown columns to list of hidden columns and re-numerate the splitter panes. C++ code for re-numeration rule is shown below:

C++
void CExtSplitter::RenumeratePanes()
{
    int i,j,id;

    for(i=0; i < m_nMaxRows; i++)
    {
        for(j=0; j < m_nMaxCols; j++)
        {
            CPoint pos  = RelToAbsPosition(i,j);
            CWnd* pPane = 
                (CWnd*) m_pane_ptr_array(pos.x, pos.y);
            ASSERT(pPane != NULL);

            id=AFX_IDW_PANE_FIRST + i * 16 + j;

            int r=pPane->SetDlgCtrlID(id);
            ASSERT(r);

            if(IsPaneVisible(pos.x,pos.y))
                pPane->ShowWindow(SW_SHOW);
            else
                pPane->ShowWindow(SW_HIDE);

        }
    }
}

where RelToAbsPosition function transforms the relative pane position to the absolute pane position.

Demo Project

In the demo project, you can call the hide and show functions through the "View" submenu. CExtSplitter class depends on C2DArray, which is included in the project.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe source code license type Pin
Member 1372285013-Mar-18 1:43
Member 1372285013-Mar-18 1:43 
QuestionNeed Dynamically add and delete unlimited no of views of a document Pin
officialid24-Aug-11 0:58
officialid24-Aug-11 0:58 
QuestionHow to create 4 splitter windows? Pin
srikadi24-Sep-08 20:50
srikadi24-Sep-08 20:50 
GeneralVisual Studio 2005 Problems / Does not compile Pin
User 2694215-Mar-07 1:28
professionalUser 2694215-Mar-07 1:28 
GeneralProblem with nested splitters Pin
_stefanu_11-Oct-05 20:30
_stefanu_11-Oct-05 20:30 
GeneralVery Good Pin
tommy790729@126.com22-Dec-04 15:14
tommy790729@126.com22-Dec-04 15:14 
Generalproblem clearing divider bar between two panes on hiding Pin
Oreille4-Apr-04 18:21
Oreille4-Apr-04 18:21 
Hi,
I am new to VC and splitter windows.
I needed a similar functionality on the PPC and your code does just what i needed. Well i used the logic in a more specfic way just for my requirement as follows. It is still in the crude form. I need to display any one of the existing 5 panes/columns in the splitter window that i have. The problem that arises is that the divider bar that appears between any two panes does not get cleared. A divider bar appears on the extreme right on the screen. How do i get rid of that?
What would i need to add to my existing code. I would be grateful to receive a reply asap. Thanks in advance.

The code used is as follows:

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
BOOL bCreateSpltr = m_pSplitterWnd.CreateStatic(this,1,5,WS_CHILD|WS_VISIBLE|WS_BORDER,AFX_IDW_PANE_FIRST);

m_pSplitterWnd.CreateView(0,0,RUNTIME_CLASS(CSoftcorderAlbumView),CSize(240,270),pContext);
/*pWnd is an array of (CWnd *)------it is a member variable of the mainframe class*/
pWnd[0] = m_pSplitterWnd.GetPane(0,0);

m_pSplitterWnd.CreateView(0,1,RUNTIME_CLASS(CFeaturesDlgView),CSize(240,270),pContext);
pWnd[1] = m_pSplitterWnd.GetPane(0,1);

m_pSplitterWnd.CreateView(0,2,RUNTIME_CLASS(CFOpenThumbnailView),CSize(240,270),pContext);
pWnd[2] = m_pSplitterWnd.GetPane(0,2);

m_pSplitterWnd.CreateView(0,3,RUNTIME_CLASS(CImageView),CSize(240,270),pContext);
pWnd[3] = m_pSplitterWnd.GetPane(0,3);

m_pSplitterWnd.CreateView(0,4,RUNTIME_CLASS(CImageEditorView),CSize(240,270),pContext);
pWnd[4] = m_pSplitterWnd.GetPane(0,4);

ShowOnlyColumn(4);

m_pSplitterWnd.RecalcLayout();
return bCreateSpltr;

// return CFrameWnd::OnCreateClient(lpcs, pContext);
}

void CMainFrame::ShowOnlyColumn(int colNum)
{

int colCtr,idColIdx,nMaxCols = 5,id;

for(colCtr=0, idColIdx=1; colCtr < nMaxCols;colCtr ++)
{
if(colCtr == colNum)
{
id=AFX_IDW_PANE_FIRST + 0 * 16 + 0;
pWnd[colNum]->SetDlgCtrlID(id);
pWnd[colNum]->ShowWindow(SW_SHOW);
continue;
}

id=AFX_IDW_PANE_FIRST + 0 * 16 + idColIdx;

pWnd[colCtr]->SetDlgCtrlID(id);
pWnd[colCtr]->ShowWindow(SW_HIDE);
idColIdx++;
//m_pSplitterWnd.RecalcLayout();
}

}
Generalpropertysheet Pin
niallmack3-Nov-03 13:30
niallmack3-Nov-03 13:30 
GeneralPane ID Algorithm Pin
Jesper Knudsen18-Jun-03 3:20
Jesper Knudsen18-Jun-03 3:20 
GeneralBug in HideRow() Pin
cmd6735-May-03 4:26
cmd6735-May-03 4:26 
QuestionHow can I hide the last row/column? Pin
Davidi20-Oct-02 0:53
Davidi20-Oct-02 0:53 
AnswerRe: How can I hide the last row/column? Pin
Mark Ruzon14-Apr-03 8:37
Mark Ruzon14-Apr-03 8:37 
GeneralRunning error in VC6.0 Pin
Anonymous16-Sep-02 21:31
Anonymous16-Sep-02 21:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.