CSSplitter: Splitter with the Ability to Save/Restore Its Position

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction

CSSplitter is a class derived from CStatic. CSSplitter provides splitter functionality (in other words, sizing functionality) for dialogs, property sheets, and any other CWnd-derived windows.

This class can work both with controls from dialog resources and those created on-the-fly. Besides, this splitter control works within the rectangular area, whose dimensions are defined by the user of the class. You also can assign margins for the splitter rectangular area beyond which the splitter is not allowed to move. To cancel splitting, the end-user can push the ESC key. Using the ESC key is convenient for CFormView-based applications and other applications, like CView based ones, but it is unusable for dialogs. CSSplitter also hides/shows controls using the ShowWindow() and MoveWindow() methods without deleting and re-creating.

Moreover, splitters created by CSSplitter save their positions into the Registry and then restore them the next time a window containing controls is created. It is very important that splitter panes can be NESTED.

Using the Code

With the CSSplitter class, you can easily add splitter functionality to your window. Just use the following steps:

  1. Add “SSplitter.cpp” and “SSplitter.h” to the project.
  2. Include “SSplitter.h” in the header file where the controls are defined.
  3. Add a member variable for each splitter pane you want to create:
  4. CSSplitter m_MainSplitterPane;
  5. In the window initialization, create the objects of each splitter pane:
  6. BOOL CTestDlg::OnInitDialog()
    {
    //...
       m_MainSplitterPane.Create(
          WS_CHILD|WS_VISIBLE|WS_BORDER|WS_CLIPCHILDREN|SS_VERT,
          this,               // the parent of the splitter pane
          &m_TreeCtrl,        // left pane
          &m_ListCtrl,        // right pane
          IDC_VERT_SPLITTER,  // this ID is used to save/restore
                              // the splitter position and
                              // therefore it must be unique
                              // within your application
          rect,               // dimensions of the splitter pane
          90,                 // left constraint for splitter
                              // position
          110                 // right constraint for splitter
                              // position
       );
    //...
    

Following is some explanation about the Create() method of the CSSplitter class:

BOOL Create(DWORD dwStyle,
      CWnd* pParentWnd,
      CWnd* pFPane,
      CWnd* pSPane,
      UINT nID,
      const RECT& rc,
      UINT nFConstr = 30,
      UINT nSConstr = 30
   );

Parameters

  • dwStyle: Specifies the splitter pane style.
  • pParentWnd: The pointer to the parent of the splitter pane.
  • pFPane: The pointer to the left pane if dwStyle contains the SS_VERT style and to the top pane if dwStyle contains the SS_HORIZ style.
  • pSPane: The pointer to the right pane if dwStyle contains the SS_VERT style and to the bottom pane if dwStyle contains the SS_HORIZ style.
  • nID: The resource ID. This ID is used to save/restore the splitter position; therefore, it must be unique within your application.
  • rc: Rectangle of the splitter pane.
  • nFConstr: The number of pixels of the left margin (if dwStyle contains the SS_VERT style) and top margin (if dwStyle contains the SS_HORIZ style) beyond which the splitter is not allowed to move. The default value is 30 pixels.
  • nSConstr: The number of pixels of right margin (if dwStyle contains SS_VERT style) and bottom margin (if dwStyle contains SS_HORIZ style) beyond which splitter is not allowed to move. Default value is 30 pixels.

Remarks

Use the SS_VERT style for the vertical splitter and SS_HORIZ for the horizontal splitter. Use WS_CLIPCHILDREN and OnEraseBkgnd to stop flicker during sizing.

To hide/show panes, one can use the HideRightPane()/ShowRightPane(), HideLeftPane()/ShowLeftPane(), and HideBottomPane()/ShowBottomPane() methods. The MakeVertSplitter() and MakeHorizSplitter() methods make the splitter vertical and horizontal, respectively. the SetSplitterPos(int nPos) method sets a new splitter position within its splitter pane.

About Alexander Atamas

PhD in Physics.

Fields of interest: C/C++, GUI application development using VC++ and MFC; databases such as MS SQL Server, MS Access, and MySQL; Web site development.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read