Click here to Skip to main content
15,860,859 members
Articles / Desktop Programming / MFC

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

Rate me:
Please Sign up or sign in to vote.
4.89/5 (56 votes)
29 Sep 2018GPL33 min read 139.7K   7.2K   75   19
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:
    C++
    CSSplitter m_MainSplitterPane;
  4. In the window initialization, create the objects of each splitter pane:
    C++
    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:

C++
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)


Written By
Ukraine Ukraine
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

 
QuestionGood work! Pin
forrest feng9-Aug-12 20:30
forrest feng9-Aug-12 20:30 
Good work, I'm just looking for a splitter tool! Thanks!
AnswerRe: Good work! Pin
Alexander Atamas9-Aug-12 22:09
Alexander Atamas9-Aug-12 22:09 
QuestionHow to add CDialog based object as one of the pane Pin
Shashidhar Kamath7-Dec-11 8:52
Shashidhar Kamath7-Dec-11 8:52 
AnswerRe: How to add CDialog based object as one of the pane Pin
Alexander Atamas8-Dec-11 10:50
Alexander Atamas8-Dec-11 10:50 
GeneralRe: How to add CDialog based object as one of the pane Pin
Shashidhar Kamath8-Dec-11 10:58
Shashidhar Kamath8-Dec-11 10:58 
GeneralRe: How to add CDialog based object as one of the pane Pin
Alexander Atamas8-Dec-11 22:55
Alexander Atamas8-Dec-11 22:55 
GeneralRe: How to add CDialog based object as one of the pane Pin
Shashidhar Kamath8-Dec-11 13:10
Shashidhar Kamath8-Dec-11 13:10 
GeneralRe: How to add CDialog based object as one of the pane Pin
Alexander Atamas8-Dec-11 23:08
Alexander Atamas8-Dec-11 23:08 
QuestionWhy not have HideTopPane() / ShowTopPane() function? Pin
yanglinwei5-Jul-10 16:56
yanglinwei5-Jul-10 16:56 
GeneralSStest Pin
PowerPlus5-Feb-09 2:26
PowerPlus5-Feb-09 2:26 
GeneralGreat job dude! Pin
Member 438770612-Jul-08 11:22
Member 438770612-Jul-08 11:22 
GeneralThe window moves! Pin
arta828-Dec-07 15:20
arta828-Dec-07 15:20 
GeneralUNICODE compatibility Pin
sevar29-Nov-07 0:48
sevar29-Nov-07 0:48 
GeneralCute Pin
muralidhar0016-Nov-07 13:15
muralidhar0016-Nov-07 13:15 
GeneralSmall Remark Pin
kidako23-Aug-05 22:54
kidako23-Aug-05 22:54 
QuestionTAB among controls? Pin
Blake Miller3-Aug-04 5:09
Blake Miller3-Aug-04 5:09 
GeneralWorks with CResizableFormView Pin
dobbs@riskeng.com20-Jul-04 7:45
dobbs@riskeng.com20-Jul-04 7:45 
Generalsimple and strong Pin
araud21-Apr-04 21:48
araud21-Apr-04 21:48 
GeneralThe second CWnd-based splitter ! Pin
Kochise13-Apr-04 6:59
Kochise13-Apr-04 6:59 

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.