Yes, that is another-another splitter control. This control based the control of this(Another splitter control for dialog). I have take some changes of the origin code to make it much more easy to use. The new splitter-control can auto change the linked control's pos automatically, when the user change the split-bar. That it is.
I'm working for a GUI application which have many dialogs. But the developer before me make all of the dialogs fixed size. Some times that is ok. But the others will make the interface be had to use. I have to move the scroll bar to see the part which be hide by the fixed sized dialog. When I have a chance to modify it. I decide to make some change. From that time on, I looking for a splitter control which can be used in the dialog.
There are many kinds of splitter-control in CodeProject. I'm like the one which write by Hung Nguyen very much: Another splitter control for dialog. The commit dialog of the svn use that one too. when I used this control in many of my application. And I found it is have some small problem that I have to call ChangePos function in many place. In other words, it can not move the relation control automatically. So I make a new one to solve this problem.
Using the code
Step 1 Add a picture control on your dialog at the resource editor.
Put a picture control on your dialog, give it id IDC_SPLITTER1. Change the size of the control to make it look like a horizontal bar. Double click the control of IDC_SPLITTER1, change the property as below. And then add a vertical one as the same way, give it id IDC_SPLITTER2.
Actually, all the operate above is just want to make us not calc the spliter size. You can specify the size of the splitter by the CSplitterControl::Create function.
Step 2 Add splitters to the dialog class.
Add SplitterControl.h and SplitterControl.cpp to your project. Insert #include "splittercontrol.h" to the .h file of the dialog class.
And then, add member varibles:
In the function of OnInitDialog, create the splitters.
There are some notices here:
> Use SPS_VERTICAL or SPS_HORIZONTAL to special the splitter style.
> You can special a RGB color(the default id RGB(120, 120, 120)) for the splitter line.
> You can special the splitter line width(the default is 1 ).
> The width(SPS_VERTICAL) or height(SPS_HORIZONTAL) of the splitter depend on the width of rect when you call Create function.
pWnd = GetDlgItem(IDC_SPLITTER1);
BOOL bRet = m_wndSplitter1.Create(WS_CHILD | WS_VISIBLE, rc, this, IDC_SPLITTER1, SPS_VERTICAL|SPS_DELTA_NOTIFY); if (FALSE == bRet)
AfxMessageBox("m_wndSplitter1 create failed");
pWnd = GetDlgItem(IDC_SPLITTER2);
bRet = m_wndSplitter2.Create(WS_CHILD | WS_VISIBLE, rc, this, IDC_SPLITTER2, SPS_HORIZONTAL, RGB(0, 0, 255));
if (FALSE == bRet)
AfxMessageBox("m_wndSplitter2 create failed");
Step 3 Add linked windows.
The splitter can move the linked window pos automaticly as the user change the splitter's pos. So the we should specify which window need change the pos.CSplitterControl::RegisterLinkedWindow function take the work. Check the example below.
Remember that SPLS_LINKED_LEFT means the control is on the left side of the splitter. SPLS_LINKED_RIGHT means the right. The two used for the splitter of vertical. If the control linked to a splitter of vertical with SPLS_LINKED_LEFT, that means the right pos of the control will be changed by the splitter. SPLS_LINKED_RIGHT is similar with SPLS_LINKED_LEFT.
SPLS_LINKED_UP and SPLS_LINKED_DOWN as the name means the control will be at the up side of a horizontal splitter.
We almost finished, after we have linked the controls to the splitter. In order to make the interface looks like better. we should call CSplitterControl::Relayout function to make the initializing layout. you can call CSplitterControl::Relayout function at any time you need.
Step 4 Splitter's limit pos.
Usualy, we need set the splitter's moving range. That is not very important in the Document-View based application. But in the dialog based application, we have to process the window's edge ourself. so, the limit pos of the splitter is very important for dialog based application.
In the sizabled dialog, the limit pos of the splitter is not the fixed. Once we change the dialog size. We have to change the new limit pos of the splitter. That is not very good. In my splitter control, they send a notify message to the parent window before you ready to change the splitter pos every time. so if you want to use this set the limit pos, just handle the notify message. The notify message is named SPN_MAXMINPOS. There are some sample code here:
Add message handle function in the .h file of the dialog class for the notify message(SPN_MAXMINPOS):
afx_msg void OnMaxMinInfo(NMHDR* pNMHDR, LRESULT* pResult);
Map the message:
ON_NOTIFY(SPN_MAXMINPOS, IDC_SPLITTER2, OnMaxMinInfo)
ON_NOTIFY(SPN_MAXMINPOS, IDC_SPLITTER1, OnMaxMinInfo)
Implement the message handle function:
void CSplitterControlDemoDlg::OnMaxMinInfo(NMHDR* pNMHDR, LRESULT* pResult)
SPC_NM_MAXMINPOS* pNewMaxMinPos = (SPC_NM_MAXMINPOS*)pNMHDR;
if (IDC_SPLITTER1 == pNMHDR->idFrom)
pNewMaxMinPos->lMin = rcTree.left + 50;
pNewMaxMinPos->lMax = rcCancel.left - STD_GAP;
pNewMaxMinPos->lMin = rcList.top + 50;
pNewMaxMinPos->lMax = rcEdit.bottom - 50;
Step 5 Some special used method
Not everything be automaticly is ok. The splitter need us to register all of the linked controls to it when the dialog initializing. so if a control is not created at that time, we can not register it to the splitter. So the splitter proveded a another way to change the pos of these controls. The kernel function is CSplitterControl::ChangePos. This function accept a parameter dwLinkedSide to specify the control is which side of the splitter. And the lDelta usually is from the notify message SPN_DELTA.
The SPN_DELTA notify message is sent when the user release the mouse. It is defined in the SplitterControl.h.
#define SPN_DELTA (WM_USER + 2)
There are some sample code to show how to use this notify message:
firstly, we need use the SPS_DELTA_NOTIFY style as we create the splitter.
BOOL bRet = m_wndSplitter1.Create(WS_CHILD | WS_VISIBLE, rc, this, IDC_SPLITTER1, SPS_VERTICAL|SPS_DELTA_NOTIFY);
And then, add message maps:
ON_NOTIFY(SPN_DELTA, IDC_SPLITTER1, OnSplitter1Delta)
At last, implement it:
void CSplitterControlDemoDlg::OnSplitter1Delta(NMHDR* pNMHDR, LRESULT* pResult)
*pResult = 0;
SPC_NM_DELTA* pDelta = (SPC_NM_DELTA*)pNMHDR;
if (NULL == pDelta)
m_wndSplitter1.ChangePos(&m_edHelp, SPLS_LINKED_LEFT, pDelta->lDelta);
Thanks of Hung Nguyen and his(or her) article Another splitter control for dialog and the demo project. That help me a lot, and my sample code is base on his too. Oh,I'm so lazy.
Points of Interest
The sample way is alway exist.
2013-05-19 : first version.