Click here to Skip to main content
13,667,657 members
Click here to Skip to main content
Add your own
alternative version

Stats

1.3M views
16.8K downloads
351 bookmarked
Posted 16 Nov 1999
Licenced CPOL

CSizingControlBar - A Resizable Control Bar

, 15 Aug 2000
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

Instructions

Getting Started

  1. Include the following files in your project:
    sizecbar.h
    sizecbar.cpp
    scbarg.h
    scbarg.cpp
  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() |
        CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
    
    m_wndMyBar.EnableDocking(CBRS_ALIGN_ANY);
    
    EnableDocking(CBRS_ALIGN_ANY);
    
    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:
    scbarcf.h
    scbarcf.cpp
  2. Change the stdafx.h file to look like this:
    #define _SCB_REPLACE_MINIFRAME
    #include "sizecbar.h"
    #include "scbarg.h"
    #include "scbarcf.h"
    #define baseCMyBar CSizingControlBarCF
  3. Add these lines in CMainFrame::OnCreate(), after the EnableDocking() call:
    #ifdef _SCB_REPLACE_MINIFRAME
        m_pFloatingFrameClass = RUNTIME_CLASS(CSCBMiniDockFrameWnd);
    #endif //_SCB_REPLACE_MINIFRAME

Remarks

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 :) 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 information 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 www.datamekanix.com for class reference, a full changelog, FAQ, a dedicated message board and more.

License

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

Share

About the Author

Cristi Posea
United States United States
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
AnswerRe: Dumb question: How can I write something into the bar at runtime? Pin
Cristi Posea14-Apr-01 4:47
memberCristi Posea14-Apr-01 4:47 
GeneralViews and Documents in CSizeableCtrlBar Pin
tsy14-Feb-01 0:06
membertsy14-Feb-01 0:06 
GeneralRe: Views and Documents in CSizeableCtrlBar Pin
Cristi Posea20-Feb-01 23:45
memberCristi Posea20-Feb-01 23:45 
GeneralSplitters vs. CSizingControlBar Pin
João Filipe Ferreira13-Feb-01 11:30
memberJoão Filipe Ferreira13-Feb-01 11:30 
GeneralMDI Interface and Undocking Pin
JM2-Feb-01 13:17
memberJM2-Feb-01 13:17 
GeneralGreat Pin
Cool11-Jan-01 20:34
memberCool11-Jan-01 20:34 
GeneralRe: Great Pin
Anonymous28-Feb-02 3:56
memberAnonymous28-Feb-02 3:56 
GeneralImprovement Pin
Leo Moll12-Dec-00 1:59
memberLeo Moll12-Dec-00 1:59 
Hi!

Many thanks for your fabulous control bar! I have made a small change that perhaps would be interesting for your next version. It fixes the following scenario:

Imagine you have a control bar docked on the left side, and one on the right side. Now you drag the right control bar under the left control bar. You expect the two control bars on to be docked on the left side, one under the other. You get also this, but the original control bar on the left ist shrinked to it's minimum size.

This is due to the following code:

void CSizingControlBar::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
// force non-client recalc if moved or resized
lpwndpos->flags |= SWP_FRAMECHANGED;

baseCSizingControlBar::OnWindowPosChanging(lpwndpos);

// find on which side are we docked
m_nDockBarID = GetParent()->GetDlgCtrlID();

if (!IsFloating())
if (lpwndpos->flags & SWP_SHOWWINDOW)
m_bKeepSize = TRUE;
^^^^^^^^^^^^^^^^^^^
}

This code is absolutely correct, since you want to preserve the size of the control bar you are moving. But in this case, the control bar you are moving is so big in respect of the usable area, that the impact on the other docked control bars is too extreme.

To prevent this, we define simply that the preservation of the size takes only place if the original docked size of the control bar does not exceed the half of the available area.

This small modification in CSizingControlBar::NegotiateSpace will sole the problem:

ORIGINAL:
if ((nL == nLMin) && (nDelta < 0) || // already at min length
pBar->m_bKeepSize) // or wants to keep its size
continue;

MODIFIED:
if ((nL == nLMin) && (nDelta < 0) || // already at min length
(pBar->m_bKeepSize && ((nL * 2) < nLengthAvail)) ) // or wants to keep its size
// LEOPOLDO: We keep our size ONLY when we do not shrink too much
// the others!
continue;


Regards
Leo Moll
GeneralRe: Improvement Pin
Kiran26-Feb-01 12:46
memberKiran26-Feb-01 12:46 
Questionhow to resize Pin
peter31-Aug-00 0:06
susspeter31-Aug-00 0:06 
AnswerRe: how to resize Pin
Anonymous17-Feb-02 11:42
memberAnonymous17-Feb-02 11:42 
Generalno ENTER and ESCAPE messages Pin
Thomas Barthel28-Aug-00 10:15
sussThomas Barthel28-Aug-00 10:15 
GeneralRe: no ENTER and ESCAPE messages Pin
Mark Ture3-Aug-01 7:52
memberMark Ture3-Aug-01 7:52 
GeneralResizing the control Bar for negotiating space... Pin
Prasad20-Aug-00 5:28
sussPrasad20-Aug-00 5:28 
GeneralRe: Resizing the control Bar for negotiating space... Pin
Cristi Posea20-Aug-00 5:40
sussCristi Posea20-Aug-00 5:40 
GeneralRe: Resizing the control Bar for negotiating space... Pin
Bill M.17-Mar-01 8:55
memberBill M.17-Mar-01 8:55 
GeneralRe: Resizing the control Bar for negotiating space... Pin
Cristi Posea18-Mar-01 9:47
memberCristi Posea18-Mar-01 9:47 
QuestionHow to add tabctlr in docking window Pin
Sanjay Bora21-Jul-00 19:58
sussSanjay Bora21-Jul-00 19:58 
GeneralHi ! Need Help..... Pin
Sanjay Bora20-Jul-00 14:50
sussSanjay Bora20-Jul-00 14:50 
GeneralOnClose( ) Pin
Andrew Welch20-Jul-00 14:42
sussAndrew Welch20-Jul-00 14:42 
GeneralOnClose( ) - Solution Pin
Andrew Welch21-Jul-00 19:31
sussAndrew Welch21-Jul-00 19:31 
GeneralHi Pin
Sanjay Bora18-Jul-00 16:00
sussSanjay Bora18-Jul-00 16:00 
QuestionHow can I update View from Tree control Pin
Sanjay Bora16-Jul-00 19:05
sussSanjay Bora16-Jul-00 19:05 
AnswerRe: How can I update View from Tree control Pin
Cristi Posea17-Jul-00 0:14
sussCristi Posea17-Jul-00 0:14 
GeneralTooltip Pin
JEAN-YVES23-May-00 4:28
sussJEAN-YVES23-May-00 4: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.

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