Have you ever spent time trying to get the splitting windows right, then added a method of switching views and even worse, tried to make the individual splitters to expand over the whole window? And then you want to make the whole damn (sorry) thing persistent for the next time you start the program? Well I did, and every time I had to look up in old code and every time I made errors and every time it took me a long time to get it to work the way I had it planned.
Well, I now sat down and solved it - once for all. By writing a reusable base class. Of course, I had a little help from many useful articles posted by this and other sites. The methods
HideColumn are taken from an article posted by Oleg Galkin on www.codeguru.com, the articles by Caroline Englebienne, Adrian Roman and Dongik Shin helped me writing the methods to support switchable views.
And now I'm ready to share once more. You may use this class for your own projects as you like, but it is AS IS, use at your own risk, I cannot be held responsible for any damage...blah blah blah ...
To use this base class is simple, however there are a few things you need to consider, else you will get one of the many asserts from within the
This class uses only static splitters and will only split each window once, either vertically or horizontally. Once the window is split, you can split one ore both panes again and again and... Have a look at the screen shot above. I simple split the first window into two panes, a left and right one. Then I split the left pane again, this time into a top and bottom pane. The bottom pane is now split again vertically and so on.
To do this, you need to include the source files ST_splitterwnd.h and ST_splitterwnd.cpp into your project.
Then add a
ST_SplitterWnd member for each split to your
CMainFrame::OnCreateClient, you need to create the first
m_pSplitterWnd = new ST_SplitterWnd();
Create method is defined as follows:
bool Create(CWnd* pParentWnd,
int nID = AFX_IDW_PANE_FIRST);
Note: If you want to split a window into further subdivisions, pass
NULL to the
pView2 parameter. If you fail to do this, the application will assert.
The view1 and view2 is used to pass the view class to the SplitterWnd, just like it is done with the 'normal'
CSplitterWnd. It has to be passed using the macro
Use the method
AddSubDivision to apply a further split:
The above sample will split the left pane horizontally and fill the top pane with a view, while leaving the bottom part empty for further splitting by passing
NULL. The method
AddSubDivision is defined as follows:
ST_SplitterWnd* AddSubDivision(int nSide,
Basically, the parameters are used in the same way as the
Create parameters. The first parameter (
nSide) is used to describe the pane in which the split has to be added. Four constants (
BOTTON_SIDE) are defined for this purpose. The last parameter tells the method to perform a vertical split (
true) or a horizontal split (
AddSubDivision returns a pointer to a new instance of a
ST_SplitterWnd object which can be split again (and again..):
m_pSplitterWnd3 = m_pSplitterWnd2->AddSubDivision(BOTTOM_SIDE,
m_pSplitterWnd4 = m_pSplitterWnd3->AddSubDivision(LEFT_SIDE,
An additional feature of this class is the possibility to switch between views within a pane. In order to do this, you use the method
AddView. Note: When using
AddView, the pane to which the views must be added must not contain a view, with other words, they must be initialized with
NULL just like the pane which will be split once more.
m_nViewNo1 = m_pSplitterWnd->AddView(RIGHT_SIDE,
m_nViewNo2 = m_pSplitterWnd->AddView(RIGHT_SIDE,
AddView is defined as follows:
int AddView(int nSide,
CRuntimeClass * pViewClass,
Basically that will do the job. There are three more methods of interest:
SetInitialStatus is used to restore the settings, such as size and position of each pane, from the registry.
SwitchToView toggles between multiple views within a pane.
ToggleSide hides and displays a pane.