Click here to Skip to main content
13,293,178 members (42,552 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 14 Feb 2003

WTL Snapping Splitter Window

, 14 Feb 2003
Rate this:
Please Sign up or sign in to vote.
A simple adaptation of the WTL CSplitterWindow to add snap-to support.


I was developing an application that used the calendar control and needed to control the behavior of the splitter window that contained the calendar control so I thought I would create a splitter window that supported a snap-to behavior.


My original decision led me astray as I tried to reimplement the entire CSplitterWindow as a new class, when it hit me, just override the OnMouseMove method. So this is where we stand. Its a simple change to the code but I thought it would be an interesting contribution so I decided to wrap it up and submit it to CodeProject.

Using the code

The code is very easy and straightforward to use. You use it just like the CSplitterWindow class but there is one additional method SetSnapSize that controls how big a snap to perform. The code takes care of whether you are using a vertical splitter or a horizontal splitter.

The "biggest" change to the class was a few lines of code that were added to the CSnapSplitterWindow class that derives from CSplitterWindow. In the OnSize method the following code was added to support the snap-to place. The variable m_nSnapSize holds the size that the programmer wants the window to snap-to. During a mouse move I determine if the new position of the splitter is greater than 1/2 the snap size and if it is, I adjust the splitter to the next snap position. This is a simple operation but is really the "meat" of the code.

// this is all that is required to make it snappable
if( static_cast<UINT>(abs( xyNewSplitPos - m_xySplitterPos )) > 
    ( m_nSnapSize / 2 ))
    if( xyNewSplitPos < m_xySplitterPos )
        xyNewSplitPos = m_xySplitterPos - m_nSnapSize;
        xyNewSplitPos = m_xySplitterPos + m_nSnapSize;
    xyNewSplitPos = m_xySplitterPos;

To use the code declare a variable of CSnapSplitterWindow if you want a vertical splitter that snaps on left and right movements or use CHorSnapSplitterWindow to create a horizontal splitter that snaps on up and down movements. The following code would be placed in your CMainFrame class in the OnCreate method.

class CMainFrame : public CFrameWindowImpl<CMainFrame>, 
    public CUpdateUI<CMainFrame>,
    public CMessageFilter, public CIdleHandler

    CSnapSplitterWindow    m_splitterContent;
    LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, 
        LPARAM /*lParam*/, BOOL& /*bHandled*/)
        m_hWndClient = m_splitterContent.Create( m_hWnd, rcDefault, 
                NULL, WS_CHILD | WS_VISIBLE );
        m_splitterContent.SetSnapSize( 75 );

Points of Interest

It wasn't very hard to write this class once I got over my initial attempt to try to rewrite the entire CSplitterWindow class but the one problem that I had was using the CMonthCalendarCtrl class. I made the call to GetMinReqRect expecting to get the minimum size the control needs to display itself. Thinking that the minimum size would be all that is required to set the snap size for both horizontal and vertical I used it but I noticed that the control wouldn't display another month in the display. So I had to play around with some different height and width adjustments before I found some values that were the smallest possible and still allow the control to behave as expected when it snapped to a larger size.


1.0 - 14 Feb 2003First release version.


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

Terry Denham
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralEnhancements Pin
Terry Denham19-Feb-03 6:01
memberTerry Denham19-Feb-03 6:01 
GeneralNo offense... Pin
Shog915-Feb-03 8:20
memberShog915-Feb-03 8:20 
GeneralRe: No offense... Pin
Terry Denham15-Feb-03 13:56
memberTerry Denham15-Feb-03 13:56 
Not that is a defense but this is exactly the way Outlook works. I happen to like the behavior and thought its better than letting the user drag it to their hearts content and if it they don't release it at the right spot then snap back. This would be EXTREMELY annoying.

At least this way the user has some idea something is afoot. Why isn't the splitter moving, so you keep dragging it and it jumps to the next snap point. This is better than allowing the user to drag it, thinking they are going to put the splitter bar at some point but then rejecting it because it doesn't fit.

I'm not sure what you mean by opaque resize, are you talking about full drag support. Well you can do this, but I even trap it when its in full drag mode, it doesn't draw the ghostbar and you keep moving the resize pointer until you get to the next snap point and then it will snap-to.

Please explain in more detail what you are thinking about the opaque resize design.

Thanks for your feedback.
GeneralRe: No offense... Pin
Shog917-Feb-03 12:33
memberShog917-Feb-03 12:33 
GeneralRe: No offense... Pin
Terry Denham17-Feb-03 12:50
memberTerry Denham17-Feb-03 12:50 
GeneralRe: No offense... Pin
Shog917-Feb-03 15:01
memberShog917-Feb-03 15:01 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 15 Feb 2003
Article Copyright 2003 by Terry Denham
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid