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

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

  • The rapid evolution of enterprise storage technologies, combined with external forces, like the explosion of big data, can cause Linux® and server administrators to play catch-up when it comes to storage. Running a bunch of monolithic storage devices and proprietary, disconnected technologies forces administrators to spend valuable time creating and managing complex solutions. To reduce complexity and enable rapid deployment of new technologies and applications, server administrators need a single open …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds