Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CSSplitter: A Splitter with the Ability to Save/Restore its Position

0.00/5 (No votes)
29 Sep 2018 4  
A splitter control derived from CStatic for dialog controls, and that can be used not only within the restricted splitter pane.

SSplitter

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 with those created on-the-fly. Besides, this splitter control works within the rectangular area, the dimensions of which are defined by the user of the class. You can also 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 ShowWindow() and MoveWindow() methods without deleting and re-creating.

There are two splitting modes. The first mode is chosen by default. This mode does the splitting on the mouse up event. If another mode is set, the splitting occurs on the mouse move event. In other words, the splitting is continuous like seen in MS Outlook Express. The toolbar button “M” of the demo application switches between the two splitting modes to play with.

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

Using the Code

With the CSSplitter class, you can easily add splitter functionality to your window using 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:
    CSSplitter m_MainSplitterPane;
  4. In the window initialization, create the objects of each splitter pane:
    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 for saving/restoring 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
        );
    //...

Some explanations 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 for saving/restoring the splitter position, and therefore it must be unique within your application.
  • rc - Rectangle of the splitter pane
  • nFConstr - The number of pixels of 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. Default value is 30 pixels.
  • nSConstr - The number of pixels of right margin (if dwStyle contains the SS_VERT style) and bottom margin (if dwStyle contains the SS_HORIZ style) beyond which the splitter is not allowed to move. Default value is 30 pixels.

Remarks

Use the SS_VERT style for vertical splitters, and SS_HORIZ for horizontal splitters. Use WS_CLIPCHILDREN and OnEraseBkgnd to prevent flicker during sizing. For hiding/ showing panes, one can use HideRightPane() / ShowRightPane(), HideLeftPane() / ShowLeftPane(), HideBottomPane() / ShowBottomPane() methods. The methods MakeVertSplitter() and MakeHorizSplitter() make the splitter vertical and horizontal, respectively. SetSplitterPos(int nPos) sets the new splitter position within its splitter pane. SetMouseMoveSplittingMode(BOOL bMouseMove) is used to set the splitting mode. If bMouseMove=TRUE, the splitting occurs on the mouse move event. If bMouseMove=FALSE (this is by default), the splitting occurs on the mouse up event. In the demo application, by clicking the toolbar button marked with “M”, you can change the splitting mode. Then, play with the splitter to see the splitting mode in action.

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