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

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

  • The operational costs of managing an x86 base are taxing IT budgets, making it difficult to fund and staff new initiatives. Today's IT organization must seek efficiencies in its operations and shift to a more agile infrastructure that's flexible enough to adapt to future changes in the business. Read this Q & A session with Jed Scaramella, research manager for IDC's Enterprise Platforms and Data Center Trends, to learn how the integrated nature of the blade platform delivers critically needed efficiencies …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds