Click here to Skip to main content
12,295,482 members (55,213 online)
Click here to Skip to main content
Add your own
alternative version


350 bookmarked

CSizingControlBar - a resizable control bar

, 15 Aug 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
DevStudio-like docking window

Sample Image

Features of CSizingControlBar 2.43

  • Resizable control bar, that can be resized both when docked and when floating.
  • Multiple sizing control bars can be docked on the same row/column.
  • Full dynamic resizing, both when docked and floating, including diagonal resizing when floating.
  • State persistence support (SaveState/LoadState).
  • Gripper with "hide bar" flat button.
  • Memory DC flickerless NC painting.
  • Sample extension class with focus autosensing text caption. On Win98/Win2k, the caption is painted with gradient.
  • No custom resources were used (bitmaps, cursors, strings, etc.), so the integration is easier and you have full control over the resources you eventually use in derived classes.
  • Easy to use: use directly one of the CSizingControlBar* classes or derive your own control bar class, then add your child controls.


Getting Started

1. Include the following files in your project:


2. Add these lines to your stdafx.h (if the files are in a different directory, include the path - see the stdafx.h file in the samples):

#include "sizecbar.h"
#include "scbarg.h"

3. Derive a class from CSizingControlBarG (you have an example in mybar.* files).

4. In mainfrm.h, include your class' header:

#include "mybar.h"
then add a member variable to CMainFrame:
CMyBar m_wndMyBar;

5. Create the bar in CMainFrame::OnCreate(). Then set bar styles, enable it to dock... like any control bar.

if (!m_wndMyBar.Create(_T("My Bar"), this, 123)
    TRACE0("Failed to create mybar\n");
    return -1;
    // fail to create
m_wndMyBar.SetBarStyle(m_wndMyBar.GetBarStyle() |



DockControlBar(&m_wndMyBar, AFX_IDW_DOCKBAR_LEFT);

Advanced Example

The instructions above will make a docking bar with a DevStudio-like gripper (with 2 raised lines and a hide button) when docked, and with no gripper when floating.

Let's explore some advanced features. Now we will use the CSizingControlBarCF class as a base class, and will hide the miniframe caption, showing the gripper in the floating state too. That's possible, because CSizingControlBarCF's gripper looks like a small caption. As a side effect of using a custom miniframe class, the resizing of the floating bar will be dynamic if "Show window contents while dragging" display property is enabled.

1. Add these files to your project too:


2. Change the stdafx.h file to look like this:

#include "sizecbar.h"
#include "scbarg.h"
#include "scbarcf.h"
#define baseCMyBar CSizingControlBarCF

3. Add these lines in CMainFrame::OnCreate(), after the EnableDocking() call

    m_pFloatingFrameClass = RUNTIME_CLASS(CSCBMiniDockFrameWnd);


These classes are intended to be used as base classes. Do not simply add your code to the files - instead create a new class derived from CSizingControlBarG or CSizingControlBarCF and put there what you need. If you want to customize your gripper, or simply don't want a gripper, you can use CSizingControlBar as a base class.

Window IDs: The usage of IDs in the range of AFX_IDW_CONTROLBAR_FIRST + 32 .. AFX_IDW_CONTROLBAR_LAST is required only if the bar will not be enabled for docking (that's is - it will stay fixed right under the frame's menu). But in this situation you won't be able to fully use the features of this class, so if you will enable it to dock (a reasonable guess Smile | :) then you can use any valid window ID.
Another place where the IDs are important is the saving/loading of the bar's state. You must use different IDs for each control bar that is enabled to dock, and this includes the other bars too. For example, if you have two toolbars, you can create the first one with the default ID (which is AFX_IDW_TOOLBAR = AFX_IDW_CONTROLBAR_FIRST), but the second one must have a different ID.

OnUpdateCmdUI: This member function is pure virtual in CControlBar (the base class of CSizingControlBar). Its purpose is to allow updating of controls at idle time (from here CCmdUI::DoUpdate() is called for the toolbars buttons, controls on dialog bars, panes of status bar, etc.).
However, I found it very useful to update the look of the "x" flat button in CSizingControlBarG and the color of the caption in CSizingControlBarCF (no timers needed). So, if you will use this function, don't forget to call the base class' member (see mybar.cpp).

Dynamic resizing: This feature allows redrawing of the bar during resizing. Also all the bars are repositioned and redrawn if necessary.
The SPI_GETDRAGFULLWINDOWS system parameter is queried for this (it is enabled by the "Show window contents while dragging" checkbox in Display Properties).

CBRS_SIZE_DYNAMIC: This bar style is required. Make sure you add it to the bar, otherwise the application will crash when the user floats a bar. You can add it using SetBarStyle() after Create(), or by changing the default style for Create() to something like: WS_VISIBLE|WS_CHILD|CBRS_TOP|CBRS_SIZE_DYNAMIC.

State persistence: The common MFC control bars' docking state is saved using CMainFrame::SaveBarState(). In addition to the info saved by this function, the CSizingControlBar class needs to save 3 sizes. This is done in CSizingControlBar::SaveState() function, so a m_wndMyBar.SaveState() call is required. Please note that the state storing code must be placed in CMainFrame's OnClose() or DestroyWindow(), not in OnDestroy(), because at the time the WM_DESTROY message is received, the floating bars are already destroyed.
In CMainFrame::OnCreate(), the m_wndMyBar.LoadState() call must be placed before LoadBarState().
Alternatively, if you have more than one resizable bars, you can call once the static member SizingControlBar::GlobalSaveState() instead of calling each bar's SaveState(). The same for LoadState() - there is a CSizingControlBar::GlobalLoadState() function. See both samples here for more details.

Precompiler flags: There are 2 symbols which can be defined to cause the floating bars to have different appearance and functionality:

  1. _SCB_REPLACE_MINIFRAME can be used to plug in CSCBMiniDockFrameWnd, which is a custom miniframe class. The main gain of using this class is that the floating bars can be resized dynamically, like all other windows. The other advantage is that the miniframe caption can be turned off, allowing the bar to display its own gripper, for increased functionality and/or custom designs.
    If you use this flag, you have to change the m_pFloatingFrameClass member of the main frame (see the advanced example above).
  2. _SCB_MINIFRAME_CAPTION can be defined only if the previous flag is also defined. It causes the custom miniframe to keep the tool window caption.
    Both CSizingControlBarG and CSizingControlBarCF classes do not display a gripper when floating if this flag is set.

See also for class reference, a full changelog, FAQ, a dedicated message board and more.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Cristi Posea
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGreat article, I added some code to handle when the toolbar is closed with the "x" button. Pin
Member 17562315-Oct-15 13:58
memberMember 17562315-Oct-15 13:58 
QuestionIt is updated at the web site Pin
Steve Wolf21-Jan-13 6:41
memberSteve Wolf21-Jan-13 6:41 
Generalis it memory leak Pin
lichongbin12-Apr-11 18:23
memberlichongbin12-Apr-11 18:23 
GeneralRe: is it memory leak Pin
zxl2004066-Sep-11 16:26
memberzxl2004066-Sep-11 16:26 
GeneralRe: is it memory leak Pin
Steve Wolf21-Jan-13 4:08
memberSteve Wolf21-Jan-13 4:08 
QuestionThe Code Is Very Good.I have some question.please help me! Pin
wzq00000011-Feb-11 15:31
memberwzq00000011-Feb-11 15:31 
GeneralThanks Cristi Posea Pin
lflljt31-Oct-10 23:36
memberlflljt31-Oct-10 23:36 
GeneralDockControlBar chrases vs2008 Pin
josip cagalj12-Mar-10 2:26
memberjosip cagalj12-Mar-10 2:26 
Hi I tried to use this control in myapp in VS2008, here is the code I use:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
	if (CFrameWndEx::OnCreate(lpCreateStruct) == -1)
		return -1;

	if (!m_wndMyLog.Create(" Error log", this, CSize(640, 60),
        TRACE0("Failed to create mylog\n");
        return -1;      // fail to create
	m_wndMyLog.SetBarStyle(m_wndMyLog.GetBarStyle() |

	// TODO: Delete these five lines if you don't want the toolbar and menubar to be dockable


	return 0;

When I trace it here is where it stops:
void CFrameWnd::DockControlBar(CControlBar* pBar, CDockBar* pDockBar, LPCRECT lpRect)
	// make sure CControlBar::EnableDocking has been called
	ASSERT(pBar->m_pDockContext != NULL);
	if (pDockBar == NULL)
		for (int i = 0; i < 4; i++)
			if ((dwDockBarMap[i][1] & CBRS_ALIGN_ANY) ==
				(pBar->m_dwStyle & CBRS_ALIGN_ANY))
				pDockBar = (CDockBar*)GetControlBar(dwDockBarMap[i][0]);
				ASSERT(pDockBar != NULL);                   //STOPS HERE BECAUSE pDockBar is NULL!!!
				// assert fails when initial CBRS_ of bar does not
				// match available docking sites, as set by EnableDocking()
	ENSURE_ARG(pDockBar != NULL);
	ASSERT(m_listControlBars.Find(pBar) != NULL);
	ASSERT(pBar->m_pDockSite == this);
	// if this assertion occurred it is because the parent of pBar was not initially
	// this CFrameWnd when pBar's OnCreate was called
	// i.e. this control bar should have been created with a different parent initially

	pDockBar->DockControlBar(pBar, lpRect);

Any help why is this happening?
Thanks in advance
GeneralRe: DockControlBar chrases vs2008 Pin
Steve Wolf21-Jan-13 4:12
memberSteve Wolf21-Jan-13 4:12 
GeneralSize after DinamycRecalcLayout Pin
Member 210934722-Dec-09 0:22
memberMember 210934722-Dec-09 0:22 
Question怎么在Dialog Based application 实现这样的效果呢? Pin
pophelix18-Dec-09 15:22
memberpophelix18-Dec-09 15:22 
GeneralDoubleClick on TreeView Pin
mbks7-Oct-09 3:24
membermbks7-Oct-09 3:24 
GeneralRe: DoubleClick on TreeView Solved Pin
mbks8-Oct-09 19:46
membermbks8-Oct-09 19:46 
GeneralProblem solved!! Pin
forhappy200920-Aug-09 19:00
memberforhappy200920-Aug-09 19:00 
QuestionWho can help me ? Pin
forhappy200920-Aug-09 18:25
memberforhappy200920-Aug-09 18:25 
QuestionHow show a CListView On the CSizingContralBar? Pin
luoxing12-Aug-09 3:31
memberluoxing12-Aug-09 3:31 
QuestionGDI Resource Leak? Pin
Andornot27-Jul-09 18:51
memberAndornot27-Jul-09 18:51 
GeneralCSizingControlBar with VS2005 and Windows XP Pin
kassinen20-Jul-09 21:22
memberkassinen20-Jul-09 21:22 
Questionsizecbar for Unicode? Pin
Andornot25-May-09 13:17
memberAndornot25-May-09 13:17 
Generalsizecbar for 64-bit platform? [modified] Pin
Andornot22-May-09 0:40
memberAndornot22-May-09 0:40 
Questioncan add resourceo of dialog to CSizingControlBar ? I want have a style like the property Bar of VS2005 ! Pin
zhangshaobing5178-Apr-09 7:24
memberzhangshaobing5178-Apr-09 7:24 
QuestionAbout docking quesiton Pin
yufenghao3-Sep-08 17:28
memberyufenghao3-Sep-08 17:28 
AnswerRe: About docking quesiton Pin
tangversion2-Oct-08 21:27
membertangversion2-Oct-08 21:27 
AnswerRe: About docking quesiton Pin
tangversion2-Oct-08 21:53
membertangversion2-Oct-08 21:53 
AnswerRe: About docking question Pin
Paul Darlington14-Jan-10 4:02
memberPaul Darlington14-Jan-10 4:02 
GeneralIt Captures Mouse messages of Main Window Pin
theali5-Aug-08 21:44
membertheali5-Aug-08 21:44 
GeneralLimited size when docking and sizing, Solved. [modified] Pin
c.nicos21-Jun-08 4:13
memberc.nicos21-Jun-08 4:13 
GeneralRe: Limited size when docking and sizing, Solved. Pin
Jake Jun11-Aug-08 19:39
memberJake Jun11-Aug-08 19:39 
GeneralRe: Limited size when docking and sizing, Solved. Pin
c.nicos14-Aug-08 22:39
memberc.nicos14-Aug-08 22:39 
GeneralRe: Limited size when docking and sizing, Solved. Pin
Bigsteve8719-Aug-11 7:38
memberBigsteve8719-Aug-11 7:38 
GeneralRe: Limited size when docking and sizing, Solved. Pin
c.nicos19-Aug-11 21:14
memberc.nicos19-Aug-11 21:14 
Generalchanging caption of floating toolbar dynamically Pin
hari prasad sathpadi17-Jun-08 19:54
memberhari prasad sathpadi17-Jun-08 19:54 
GeneralAccelerators Copy, Paste, Select All ... Pin
Julian Popov14-Apr-08 3:19
memberJulian Popov14-Apr-08 3:19 
GeneralTabbed?! Pin
corax@inbox.com16-Dec-07 15:24
membercorax@inbox.com16-Dec-07 15:24 
QuestionCSizingControlBar -- Help&#65281;who can help me&#65311; Pin
zhujt198129-Oct-07 19:56
memberzhujt198129-Oct-07 19:56 
GeneralCSizingControlBar - a resizable control bar Pin
SOUMEN BISWAS26-Sep-07 0:02
memberSOUMEN BISWAS26-Sep-07 0:02 
QuestionHow to set the size of CSizingControlBars docked on the same row/column? Pin
Merlin Ran11-Sep-07 23:21
memberMerlin Ran11-Sep-07 23:21 
GeneralIs possible to use your control on a Dialog Based application Pin
rbid21-May-07 20:48
memberrbid21-May-07 20:48 
QuestionChange border color &amp; Thin borders Pin
rm_pkt24-Jan-07 17:27
memberrm_pkt24-Jan-07 17:27 
GeneralDialog Window Pin
C0LDZ3R019-Jan-07 9:06
memberC0LDZ3R019-Jan-07 9:06 
Generalsuggested feature [modified] Pin
bquintero9-Jan-07 20:52
memberbquintero9-Jan-07 20:52 
Questionproblem with position of the dockbars [modified] Pin
Choi Sunok25-Dec-06 19:49
memberChoi Sunok25-Dec-06 19:49 
GeneralTo avoid warning 4312 Pin
pnugoodman20-Nov-06 19:53
memberpnugoodman20-Nov-06 19:53 
QuestionGetting the message handler for Toolbar in Sizing control bar Pin
shailesh429-Oct-06 19:13
membershailesh429-Oct-06 19:13 
QuestionWhen control bar is floating,i can,t receive message . Pin
chihyu12-Oct-06 23:20
memberchihyu12-Oct-06 23:20 
QuestionCan we add multiple dialogs in control bar with only one is shown at a time? Pin
shailesh410-Oct-06 3:46
membershailesh410-Oct-06 3:46 
Questionfloating,how to change width and height of the bar? Pin
Chris_kun10-Sep-06 18:54
memberChris_kun10-Sep-06 18:54 
AnswerRe: floating,how to change width and height of the bar? Pin
The Yariv9-Nov-08 20:18
memberThe Yariv9-Nov-08 20:18 
GeneralImprovement for multiple controls Pin
Source13-Aug-06 14:07
memberSource13-Aug-06 14:07 
QuestionRe: Improvement for multiple controls Pin
Roy F.1-Sep-06 1:40
memberRoy F.1-Sep-06 1:40 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 16 Aug 2000
Article Copyright 1999 by Cristi Posea
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid