Show/Hide Static Panes

MFC static splitter windows have a significant limitation. Their panes can't be hidden or shown dynamically. To solve the problem I overrode the CSplitterWnd. The new class can hide a splitter window column. Sure, it's only one approach to the problem, but it's a good example anyway.

//////////////////////////////////////////////////////////////////
//
// splitex.h
// (c) 1997, Oleg G. Galkin

class CSplitterWndEx : public CSplitterWnd
{
protected:
   int m_nHidedCol;    // hide column number, -1 if all columns
                       // are shown

public:
   CSplitterWndEx();

   void ShowColumn();
   void HideColumn(int colHide);

// ClassWizard generated virtual function overrides
   //{{AFX_VIRTUAL(CSplitterWndEx)
   //}}AFX_VIRTUAL

// Generated message map functions
protected:
   //{{AFX_MSG(CSplitterWndEx)
      // NOTE - the ClassWizard will add and remove member
      //        functions here.
      //}}AFX_MSG

    DECLARE_MESSAGE_MAP()
};

//////////////////////////////////////////////////////////////////
//
// splitex.cpp
// (c) 1997, Oleg G. Galkin

#include "stdafx.h"
#include "splitex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////
/
// CSplitterWndEx

CSplitterWndEx::CSplitterWndEx() :
    m_nHidedCol(-1)
{
}

void CSplitterWndEx::ShowColumn()
{
   ASSERT_VALID(this);
   ASSERT(m_nCols < m_nMaxCols);
   ASSERT(m_nHidedCol != -1);

   int colNew = m_nHidedCol;
   m_nHidedCol = -1;
   int cxNew = m_pColInfo[m_nCols].nCurSize;
   m_nCols++;    // add a column
   ASSERT(m_nCols == m_nMaxCols);

   // fill the hidden column
   int col;
   for (int row = 0; row < m_nRows; row++)
   {
      CWnd* pPaneShow = GetDlgItem(
         AFX_IDW_PANE_FIRST + row * 16 + m_nCols);
      ASSERT(pPaneShow != NULL);
      pPaneShow->ShowWindow(SW_SHOWNA);

      for (col = m_nCols - 2; col >= colNew; col--)
      {
         CWnd* pPane = GetPane(row, col);
         ASSERT(pPane != NULL);
         pPane->SetDlgCtrlID(IdFromRowCol(row, col + 1));
      }

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

   // new panes have been created -- recalculate layout
   for (col = colNew + 1; col < m_nCols; col++)
      m_pColInfo[col].nIdealSize = m_pColInfo[col - 1].nCurSize;
   m_pColInfo[colNew].nIdealSize = cxNew;
   RecalcLayout();
}

void CSplitterWndEx::HideColumn(int colHide)
{
   ASSERT_VALID(this);
   ASSERT(m_nCols > 1);
   ASSERT(colHide < m_nCols);
   ASSERT(m_nHidedCol == -1);
   m_nHidedCol = colHide;

   // if the column has an active window -- change it
   int rowActive, colActive;
   if (GetActivePane(&rowActive, &colActive) != NULL &&
       colActive == colHide)
   {
      if (++colActive >= m_nCols)
         colActive = 0;
      SetActivePane(rowActive, colActive);
   }

   // hide all column panes
   for (int row = 0; row < m_nRows; row++)
   {
      CWnd* pPaneHide = GetPane(row, colHide);
      ASSERT(pPaneHide != NULL);
      pPaneHide->ShowWindow(SW_HIDE);
      pPaneHide->SetDlgCtrlID(
         AFX_IDW_PANE_FIRST + row * 16 + m_nCols);

      for (int col = colHide + 1; col < m_nCols; col++)
      {
         CWnd* pPane = GetPane(row, col);
         ASSERT(pPane != NULL);
         pPane->SetDlgCtrlID(IdFromRowCol(row, col - 1));
      }
   }
   m_nCols--;
   m_pColInfo[m_nCols].nCurSize = m_pColInfo[colHide].nCurSize;
    RecalcLayout();
}

BEGIN_MESSAGE_MAP(CSplitterWndEx, CSplitterWnd)
//{{AFX_MSG_MAP(CSplitterWndEx)
   // NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds