Click here to Skip to main content
Click here to Skip to main content
Go to top

CSSplitter: a splitter with the ability to save/restore its position

, 24 Aug 2006
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

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.

Comments and Discussions

 
QuestionHow to add CDialog based object as one of the pane PinmemberShashidhar Kamath7-Dec-11 8:52 
AnswerRe: How to add CDialog based object as one of the pane PinmemberAlexander Atamas8-Dec-11 10:50 
GeneralRe: How to add CDialog based object as one of the pane PinmemberShashidhar Kamath8-Dec-11 10:58 
GeneralRe: How to add CDialog based object as one of the pane PinmemberAlexander Atamas8-Dec-11 22:55 
GeneralRe: How to add CDialog based object as one of the pane PinmemberShashidhar Kamath8-Dec-11 13:10 
GeneralRe: How to add CDialog based object as one of the pane PinmemberAlexander Atamas8-Dec-11 23:08 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 24 Aug 2006
Article Copyright 2004 by Alexander Atamas
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid