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

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.



Downloads

Comments

  • There are no comments yet. Be the first to comment!

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

Top White Papers and Webcasts

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds