Click here to Skip to main content
15,393,377 members
Articles / Desktop Programming / MFC
Posted 12 Jul 2002


129 bookmarked

Another Splitter Control for Dialog

Rate me:
Please Sign up or sign in to vote.
4.86/5 (67 votes)
12 Jul 20022 min read
A very simple splitter control for dialogs

Sample Image


I'm a student and very interested in VC++. I often come to this web site to get free source code. I was in need of a splitter in a dialog. I downloaded one but it was very complex and I felt it was difficult to use the control (although it's very powerful) so I made one for myself. Maybe, it's not useful for you, but if there's only one person who thinks it's useful, I will be very happy. Sometimes, you don't need good skill, just a good idea, and in this simple way, a useful piece of code will be produced. My splitter control is one of this kind.

How to Use the CSplitterControl Class

First of all, add two files, SplitterControl.h and SplitterControl.cpp to the project. Remember to add <tt> #include "splittercontrol.h"</tt> to the header file of the class which uses it.

Add member variable to the dialog class:

   CSplitterControl     m_wndSplitter1;

Now, we create the control by calling its create function. This code would appear in the OnInitDialog or OnCreate function.

BOOL CSPDemoDlg::OnInitDialog()
	pWnd = GetDlgItem(IDC_SPLITTER1);
	m_wndSplitter1.Create(WS_CHILD | WS_VISIBLE, rc, this, IDC_SPLITTER1);
	m_wndSplitter1.SetRange(50, 50, -1);

There is a tip here. Instead of calculating the rect for the splitter, we add a static control on the dialog (by resource editor), give it an ID (IDC_SPLITTER1) and make it invisible. Size it and locate in the resource editor, and then call the function GetWindowRect(rc) to move the m_wndSplitter1 to the rect.

Image 2

And here is the code for resizing controls on the dialog when the user moves the splitter control.

// LRESULT CSPDemoDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
	if (message == WM_NOTIFY)
		if (wParam == IDC_SPLITTER1)
			SPC_NMHDR* pHdr = (SPC_NMHDR*) lParam;
	return CDialog::DefWindowProc(message, wParam, lParam);
void CSPDemoDlg::DoResize1(int delta)
	// Change the width for m_wndType, m_lstItem, m_txtContent	
	CSplitterControl::ChangeWidth(&m_wndType, delta);
	CSplitterControl::ChangeWidth(&m_lstItem, -delta, CW_RIGHTALIGN);
	CSplitterControl::ChangeWidth(&m_txtContent, -delta, CW_RIGHTALIGN);

About the Class CSplitterControl and Its Functions

Here's the interface for the class CSplitterControl.

class CSplitterControl : public CStatic
	// Construction

	// Attributes
	BOOL m_bIsPressed;
	int m_nType;
	int m_nX, m_nY;
	int m_nMin, m_nMax;
	int m_nSavePos; // Save point on the lbutton down

	// ClassWizard generated virtual function overrides

	// Implementation
	static void ChangePos(CWnd* pWnd, int dx, int dy);
	static void ChangeWidth(CWnd* pWnd, int dx, DWORD dwFlag = CW_LEFTALIGN);
	static void ChangeHeight(CWnd* pWnd, int dy, DWORD dwFlag = CW_TOPALIGN);

	void SetRange(int nMin, int nMax);
	void SetRange(int nSubtraction, int nAddition, int nRoot);
	int GetStyle();
	int SetStyle(int nStyle = SPS_VERTICAL);
	void Create(DWORD dwStyle, const CRect& rect, CWnd* pParent, UINT nID);
	virtual ~CSplitterControl();

	// Generated message map functions
	virtual void DrawLine(CDC* pDC, int x, int y);
	void MoveWindowTo(CPoint pt);
	afx_msg void OnPaint();
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);

// this struct is sent as lparam in WM_NOTIFY message 
typedef struct SPC_NMHDR
	NMHDR hdr;
	int delta;      // delta : the different position of the splitter before and 
                    // after being moved.


Well, that's all about my code. Maybe the explanation is not very clear, but I hope you'll find it easy to use. No special skill, you see. Very simple. Thanks for reading my article. Please give your ideas as to whether you like it or not.


This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


About the Author

Hung Nguyen
United States United States
No Biography provided

Comments and Discussions

AnswerRe: how can i make Splitter bar to be not allowed move Pin
Nvng YA27-Nov-06 23:04
MemberNvng YA27-Nov-06 23:04 
AnswerRe: how can i make Splitter bar to be not allowed move Pin
sharki198711-Aug-10 15:54
Membersharki198711-Aug-10 15:54 
QuestionHow to use your code in SDI application? Pin
Bhushan198014-Jul-06 11:54
MemberBhushan198014-Jul-06 11:54 
Generalmoving out of dialog Pin
mirex25-Aug-04 4:00
Membermirex25-Aug-04 4:00 
GeneralRe: moving out of dialog Pin
atripathi3-Aug-05 1:05
Memberatripathi3-Aug-05 1:05 
AnswerRe: moving out of dialog Pin
bm20-Jul-06 22:01
Memberbm20-Jul-06 22:01 
Generalone problem Pin
regale4-Sep-03 1:49
Memberregale4-Sep-03 1:49 
GeneralRe: one problem Pin
Selvam R3-Dec-03 13:39
professionalSelvam R3-Dec-03 13:39 

You set property in Picture Control Visible as false ( Remove default check mark in Visible property )

GeneralWorks great for me Pin
AleemSheikh7-Jul-03 20:22
MemberAleemSheikh7-Jul-03 20:22 
Generalsmall TCHAR.H -compatible modification Pin
nanev23-May-03 2:08
Membernanev23-May-03 2:08 
GeneralGood work. I've used it but with a little modification Pin
Jaime Stuardo8-Mar-03 19:29
MemberJaime Stuardo8-Mar-03 19:29 
GeneralThx a lot Pin
sinouhe15-Jan-03 2:22
Membersinouhe15-Jan-03 2:22 
GeneralMoak: 84 error(s), 0 warning(s) Pin
Mr Razz4-Jan-03 12:06
MemberMr Razz4-Jan-03 12:06 
GeneralRe: Moak: 84 error(s), 0 warning(s) Pin
Anonymous8-Jan-03 16:35
MemberAnonymous8-Jan-03 16:35 
GeneralRe: Moak: 84 error(s), 0 warning(s) Pin
jchan5-Feb-04 13:36
Memberjchan5-Feb-04 13:36 
GeneralRe: Moak: 84 error(s), 0 warning(s) Pin
GE - NBGYF20-Jun-04 20:48
MemberGE - NBGYF20-Jun-04 20:48 
GeneralRe: Moak: 84 error(s), 0 warning(s) Pin
Anonymous28-Jul-04 11:05
MemberAnonymous28-Jul-04 11:05 
GeneralRe: Moak: 84 error(s), 0 warning(s) Pin
Member 54398025-May-05 12:48
MemberMember 54398025-May-05 12:48 
GeneralMany thanks to Moak for updated version. Pin
Nguyen Huy Hung7-Dec-02 16:44
MemberNguyen Huy Hung7-Dec-02 16:44 
GeneralUpdated version (resizeable dialog) Pin
Moak14-Nov-02 20:13
MemberMoak14-Nov-02 20:13 
GeneralRe: Updated version (resizeable dialog) Pin
Moak27-Dec-02 10:08
MemberMoak27-Dec-02 10:08 
GeneralRe: Updated version (resizeable dialog) Pin
Cui Sheng30-Jan-07 7:20
MemberCui Sheng30-Jan-07 7:20 
GeneralRe: Updated version (resizeable dialog) Pin
MTenzer26-Jul-07 0:28
MemberMTenzer26-Jul-07 0:28 
GeneralRe: Updated version (resizeable dialog) Pin
Cui Sheng24-Aug-07 5:09
MemberCui Sheng24-Aug-07 5:09 
GeneralRe: Updated version (resizeable dialog) Pin
Andrea Cacciarru21-Nov-07 3:28
MemberAndrea Cacciarru21-Nov-07 3:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.