Splitter in an MFC dialog based application
Introduction
This is my first time posting an article in CodeProject. CodeProject has been very helpful to me. I want to give back the help given by the authors by submitting my own article.
In MFC, CSplitterWnd
is usually available MDI or SDI. I just think splitter controls are just dragable buttons. So I decided to implement a class named CControlSplitter
derived from CButton
to work like CSplitterWnd
for dialog based MFC applications.
Background
The implementation is very simple. As long as you know how to subclass MFC controls, you can understand how this works and improve it.
Using the code
In your project, include the following files:
- ControlSplitter.h
- ControlSplitter.cpp
Include ControlSplitter.h in your dialog header:
#include "ControlSplitter.h"
Add splitter buttons in your dialog using the usual button control which will serve as the splitter and set its control ID such as IDC_SPLITTER1
.
Using the MFC Class Wizard, add control member variables in your dialog for the added splitter buttons with type CControlSplitter
.
After adding member control variables, the following must be implemented:
- Declaration of the control splitter in your dialog header:
- Sub class the control using
DDX_Control
in theDoDataExchange
of your dialog.
//{{AFX_DATA(CSplitterDlg)
...
CControlSplitter m_splitter;
...
//}}AFX_DATA
void CSplitterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSplitterDlg)
...
DDX_Control(pDX, IDC_SPLITTER, m_splitter);
...
//}}AFX_DATA_MAP
}
Setup your splitter in the OnInitDialog() method of your dialog
- [Required] Set the type of the control splitter:
- Add sibling controls to the splitter's control listing.
- Top control listing (for
CS_VERT
type) or left control listing (forCS_HORZ
type) - Bottom control listing (for
CS_VERT
type) or right control listing (forCS_HORZ
type)
m_splitter.SetType(CControlSplitter::CS_VERT);
The available types are CS_NONE
(default), CS_VERT
, and CS_HORZ
.
public:
typedef enum {
CS_VERT = 1,
CS_HORZ = 2,
CS_NONE = 0
};
Control splitters have two control listings:
To add the control's top control listing or left control listing, use the AddToTopOrLeftCtrls
method.
m_splitter.AddToTopOrLeftCtrls(IDOK);
To add the control's bottom control listing or right control listing, use the AddToBottomOrRightCtrls
method.
m_splitter.AddToBottomOrRightCtrls(IDC_EDIT1);
Here is the declaration for AddToTopOrLeftCtrls
and AddToBottomOrRightCtrls
:
void AddToBottomOrRightCtrls(UINT nCtrlId, WORD nFlags =
SPF_TOP|SPF_LEFT|SPF_RIGHT|SPF_BOTTOM);
void AddToTopOrLeftCtrls(UINT nCtrlId, WORD nFlags =
SPF_TOP|SPF_LEFT|SPF_BOTTOM|SPF_RIGHT);
[Note:] The two methods have extra flag parameters that can be used to specify the behaviour of the sibling controls when the control splitter is being dragged. Possible values could be the combination of the following:
/* distance of the control to the top of the window will be constant */
#define SPF_TOP 0x0010
/* distance of the control to the bottom of the window will be constant */
#define SPF_BOTTOM 0x0020
/* distance of the control to the left of the window will be constant */
#define SPF_LEFT 0x0040
/* distance of the control to the right of the window will be constant */
#define SPF_RIGHT 0x0080
Class working methods
CControlSplitter();
This is the contstructor.
void SetType(UINT nType);
This sets the type of the control splitter in either CS_VERT
or CS_HORZ
.
void AddToTopOrLeftCtrls(UINT nCtrlId, WORD nFlags);
This is used to add the control to the top or the left control listing.
void AddToBottomOrRightCtrls(UINT nCtrlId, WORD nFlags);
This is used to add the control to the bottom or the right control listing.
void OnLButtonDown(UINT nFlags, CPoint point);
This is fired when the user presses down the left button of the mouse over the control.
void OnLButtonUp(UINT nFlags, CPoint point);
This is fired when the user releases up the left button of the mouse over the control.
void OnMouseMove(UINT nFlags, CPoint point);
This is fired when the user moves the mouse over the control.
void OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
This is fired when the system asks for the cursor. The cursor is settled using SetClassLong(m_hWnd,GCL_HCURSOR,(LONG)m_hCursor);
.
Class working member variables
//Holds the Type of the control splitter
unsigned int m_nType;
//Holds the Top or Left Control Listing
std::vector<DWORD> m_vtTopLeftControls;
//Holds the Bottom or Right Control Listing
std::vector<DWORD> m_vtBottomRightControls;
//Holds the POINT position used to calculate the movement changes
CPoint m_ptStartDrag,m_ptStartPos;
//Used to determine if user Starts to Drag, Dragging, and Ends Draging
bool m_bDragging;
//Used to hold the loaded HCURSOR
HCURSOR m_hCursor;
//Holds the bounds the splitter is allowed to be moved.
CRect m_rectMax;
//Holds the old control that is previously holdes cursor capture.
CWnd * m_pOldDragCapture;
History
First release.
发表评论
fC9pv4 your placement in google and could damage your quality score if advertising
4KNOeK My brother suggested I might like this website. He was entirely right. This post truly made my day. You cann at imagine just how much time I had spent for this information! Thanks!
TtmZK3 Thanks so much for the post.Really thank you! Really Great.
k28eU2 Wow! This can be one particular of the most useful blogs We have ever arrive across on this subject. Basically Fantastic. I am also a specialist in this topic so I can understand your hard work.
LpJlFJ Really appreciate you sharing this blog post.Much thanks again. Will read on
npUgWw Precisely what I was looking for, appreciate it for posting.
yHH17C This blog is really entertaining as well as amusing. I have found many helpful tips out of this blog. I ad love to return over and over again. Thanks a bunch!
xUmfXQ You made some decent factors there. I looked on the internet for the difficulty and located most people will go together with along with your website.
Until then the michael kors outlet should take a serious interest.
6o6vTp Looking forward to reading more. Great blog.Really thank you! Keep writing.
qlaoXt Woah! I am really digging the template/theme of this website. It as simple, yet
t4aYwF Normally I don at read article on blogs, but I wish to say that this write-up very compelled me to try and do so! Your writing style has been amazed me. Thank you, quite great post.
P8XIzJ
2fRsSx neverwinter astral diamonds THE HOLY INNOCENTS. MEMBER GROUPS.
sUlpCC Thanks for some other wonderful article. The place else may anyone get that kind of info in such an ideal approach of writing? I ave a presentation next week, and I am at the look for such info.
kJHQeJ Hey, thanks for the blog article. Want more.
fhDhqL Regards for helping out, excellent info. аЂааЂ You must do the things you think you cannot do.аЂ аЂа by Eleanor Roosevelt.
yMaH1F Thanks so much for the article post.Really looking forward to read more. Really Cool.
DcjkLg Thanks for sharing, this is a fantastic article post.Much thanks again. Will read on...
bvyVgV This web page is mostly a stroll-by means of for all the info you wished about this and didn't know who to ask. Glimpse right here, and you'll positively uncover it.
dLX05M Hello my friend! I wish to say that this post is amazing, nice written and come with approximately all vital infos. I'd like to look more posts like this .
kIv6e2 Hi there, I found your website via Google while looking for a related topic, your site came up, it looks good. I've bookmarked it in my google bookmarks.
q66ca7 I am so grateful for your blog article.Really thank you! Great.
7a9Edh Thanks for the blog post.Really thank you! Really Great.
6IOkNt I really like and appreciate your blog.Much thanks again. Really Great.
YXMNbk Awesome post.Much thanks again. Keep writing.
5Tsx1T This is one awesome blog.Thanks Again. Fantastic.
2RHaUg Thanks so much for the blog post.Thanks Again.
UXOPqK Muchos Gracias for your blog article.Really looking forward to read more.
qWTsqJ wow, awesome blog.Thanks Again. Great.
OBL4qK I really liked your article.Really thank you! Want more.
jkFobQ Thank you ever so for you article.Much thanks again. Really Cool.
DOXXdA Major thankies for the article.Much thanks again. Awesome.
u7YvcL Enjoyed every bit of your blog.Really looking forward to read more. Will read on...
eDlhYO Thank you for your article.Much thanks again. Will read on...