Click here to Skip to main content
Click here to Skip to main content

Another splitter control for dialog

, 12 Jul 2002
Rate this:
Please Sign up or sign in to vote.
A very simple splitter control for dialogs
Sample Image

Introduction

I'm a student and very interested in VC++. I often enter this web site to get free source code. I was in need of a splitter in a dialog. I downloaded one but it's 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 by 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 #include "splittercontrol.h" to the header file of the class which uses it.

Add member varible to the dialog class

 protected: 
    CSplitterControl     m_wndSplitter1; 

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

BOOL CSPDemoDlg::OnInitDialog()
{ 
	...
	pWnd = GetDlgItem(IDC_SPLITTER1);
	pWnd->GetWindowRect(rc);
	ScreenToClient(rc);
	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.

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;
			DoResize1(pHdr->delta);
		}
	}
	
	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);
	Invalidate();
	UpdateWindow();
}

About the class CSplitterControl and it's functions

Here's the interface for the class CSplitterControl
class CSplitterControl : public CStatic
{
	// Construction
public:
	CSplitterControl();

	// Attributes
protected:
	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

public:
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CSplitterControl)
	//}}AFX_VIRTUAL

	// Implementation
public:
	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);

public:
	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
protected:
	virtual void DrawLine(CDC* pDC, int x, int y);
	void MoveWindowTo(CPoint pt);
	//{{AFX_MSG(CSplitterControl)
	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);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

// 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.
} SPC_NMHDR;

Conclusion

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 or hate it.

License

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

Share

About the Author

Hung Nguyen

United States United States
No Biography provided

Comments and Discussions

 
QuestionThank you for your help Pinmemberiishero17-Mar-12 1:59 
QuestionAny License attached with this code PinmemberGreatestMaverick13-Dec-11 22:55 
GeneralMy vote of 5 PinmemberXiangism27-Oct-11 20:14 
GeneralMy vote of 3 Pinmemberhelloworldbomb9-Dec-10 4:45 
Generalvery simple,thanks Pinmemberjacksp28-Apr-10 23:49 
GeneralNice work! Pinmemberjcleung6-Nov-09 16:35 
GeneralI want to display a Bitmap on the splitter Pinmemberlan_63738369-Aug-09 18:22 
GeneralOne problem Pinmembermxs8102-Apr-09 1:19 
Generalnice code Pinmemberyusc198626-Feb-09 16:48 
GeneralGood source code Pinmemberziguowen27-Nov-08 15:15 
General[Message Removed] Pinmemberimmetoz6-Oct-08 8:51 
Questionno idea PinmemberMaryVeranis20-Mar-08 5:04 
GeneralVery nice work. I found a bug and my solution Pinmembertimyin2-Feb-08 22:17 
GeneralVery nice work PinmemberAndrea Cacciarru21-Nov-07 1:31 
GeneralNgu bo me PinmemberTo Quang Hiep7-Jun-07 0:08 
GeneralSlight fix PinmemberChrisHowe19-Feb-07 1:31 
Questionhow can i make Splitter bar to be not allowed move Pinmemberkiranin31-Oct-06 1:28 
AnswerRe: how can i make Splitter bar to be not allowed move PinmemberNvng YA28-Nov-06 0:04 
AnswerRe: how can i make Splitter bar to be not allowed move Pinmembersharki198711-Aug-10 16:54 
QuestionHow to use your code in SDI application? PinmemberBhushan198014-Jul-06 12:54 
Generalmoving out of dialog Pinmembermirex25-Aug-04 5:00 
GeneralRe: moving out of dialog Pinmemberatripathi3-Aug-05 2:05 
AnswerRe: moving out of dialog Pinmemberbm20-Jul-06 23:01 
Generalone problem Pinmemberregale4-Sep-03 2:49 
GeneralRe: one problem PinmemberR.selvam3-Dec-03 14:39 
GeneralWorks great for me PinmemberAleemSheikh7-Jul-03 21:22 
Generalsmall TCHAR.H -compatible modification Pinmembernanev23-May-03 3:08 
GeneralGood work. I've used it but with a little modification Pinmemberjstuardo8-Mar-03 20:29 
GeneralThx a lot Pinmembersinouhe15-Jan-03 3:22 
GeneralMoak: 84 error(s), 0 warning(s) PinmemberMr Razz4-Jan-03 13:06 
GeneralRe: Moak: 84 error(s), 0 warning(s) PinsussAnonymous8-Jan-03 17:35 
GeneralRe: Moak: 84 error(s), 0 warning(s) Pinmemberjchan5-Feb-04 14:36 
GeneralRe: Moak: 84 error(s), 0 warning(s) PinmemberGE - NBGYF20-Jun-04 21:48 
GeneralRe: Moak: 84 error(s), 0 warning(s) PinsussAnonymous28-Jul-04 12:05 
GeneralRe: Moak: 84 error(s), 0 warning(s) Pinmemberdream1213-wang25-May-05 13:48 
GeneralMany thanks to Moak for updated version. PinmemberNguyen Huy Hung7-Dec-02 17:44 
GeneralUpdated version (resizeable dialog) PinsussMoak14-Nov-02 21:13 
GeneralRe: Updated version (resizeable dialog) PinmemberMoak27-Dec-02 11:08 
GeneralRe: Updated version (resizeable dialog) PinmemberCui Sheng30-Jan-07 8:20 
GeneralRe: Updated version (resizeable dialog) PinmemberMTenzer26-Jul-07 1:28 
GeneralRe: Updated version (resizeable dialog) PinmemberCui Sheng24-Aug-07 6:09 
GeneralRe: Updated version (resizeable dialog) PinmemberAndrea Cacciarru21-Nov-07 4:28 
GeneralUpdated CSplitterControl version PinsussMoak31-Oct-02 8:53 
GeneralRe: Updated CSplitterControl version Pinmemberjohnyboy31-Oct-02 16:00 
GeneralRe: Updated CSplitterControl version PinsussVVS2-Nov-02 0:17 
GeneralRe: Updated CSplitterControl version PinsussMoak6-Nov-02 2:04 
GeneralRe: Updated CSplitterControl version PinsussMoak7-Nov-02 4:11 
GeneralRe: Updated CSplitterControl version Pinmembersk8ing5-Nov-06 20:36 
GeneralRe: Updated CSplitterControl version PinmemberZiem2-Mar-07 9:24 
GeneralPossible bug in CSplitterControl::SetRange(int nSubtraction, int nAddition, int nRoot) PinmemberAndreas.Stuebinger28-Oct-02 1:01 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411019.1 | Last Updated 13 Jul 2002
Article Copyright 2002 by Hung Nguyen
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid