Click here to Skip to main content
11,434,695 members (47,039 online)
Click here to Skip to main content

ToolBar with Customization and Controls

, 18 Dec 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
A ToolBar class with customization and Controls

Sample Image - ToolBarEx.jpg

Introduction

The CToolbarEx class supports basic customization (As in IE) with controls on it. Additionally it can hide the controls when the toolbar is docked vertically. This class uses the framework provided by ToolBarCtrl to do the customization of the Toolbar. It also supports Large Icons and Text on Buttons.

It uses a modified CCustomizeDialog class by Nikolay Denisov to provide extra options in the Toolbar customize Dialog.

I have hardcoded a few things in CCustomizeDialog to avoid resource dependences It also overrides CDockBar with CDockBarEx to provide 3D looks and overcome some docking bugs.

To use these in your project, do the following steps:

  1. Add ToolBarEx.cpp and ToolBarEx.h in your Project.
  2. Include ToolbarEx.h in MainFrame.h and Replace CToolBar with CToolBarEx in CMainFrame
    #include <span class="code-string">"ToolBarEx.h"</span>
    . .
    
    // CToolBar      m_wndToolBar;
    CToolBarEx    m_wndToolBar;
    
  3. In your OnCreate override in your CMainFrame class, when the creation of the Toolbar is done (including controls), call SetToolBarInfoForCustomization to set the Customization Data in the Toolbar. This function should be called after the creation of the toolbar, controls and dropdown is done.
    CRect rt(0,0,200,120);
    
    //Insert Control
    m_pComboBox =(CComboBox *) m_wndToolBar.InsertControl(
        RUNTIME_CLASS(CComboBox),_T(""),
        rt,ID_FIND,WS_VSCROLL|CBS_DROPDOWNLIST);
    m_pComboBox->AddString(_T("One"));
    m_pComboBox->AddString(_T("Two"));
    m_pComboBox->AddString(_T("Three"));
    
    //Add DropDown
    m_wndToolBar.AddDropDownButton(ID_OP,IDR_OP,TRUE);
    
    //Enable Customization
    m_wndToolBar.SetToolBarInfoForCustomization();
  4. Restore the last saved data of the Toolbar
    //Restore State
    m_wndToolBar.RestoreState();

    Similarly you can also add SaveState in OnClose of the CMainFrame.

  5. Then delete the buttons you do not want to show as default and call MarkDefaultState to set the default state of the toolbar. The default state is set when Reset button on the Customize Dialog Box is pressed.
    //Delete the button which do not need to shown initially.
    m_wndToolBar.GetToolBarCtrl().DeleteButton(
        m_wndToolBar.CommandToIndex(ID_CUSTOMIZE));
    
    
    // Mark the default state for reset
    m_wndToolBar.MarkDefaultState();
  6. Call FrameEnableDocking instead of EnableDocking to use CDockBarEx instead of CDockBar.
    //     EnableDocking(CBRS_ALIGN_ANY);
    FrameEnableDocking(this,CBRS_ALIGN_ANY);

Member Functions & Data Members

CWnd* InsertControl(CRuntimeClass* pClass,LPCTSTR lpszWindowName,
    CRect& rect,UINT nID,DWORD dwStyle );

This function creates and inserts the control into the Toolbar and returns the window inserted. In rect parameter, pass only the width and height.

CWnd* InsertControl(CWnd* pCtrl,CRect& rect,UINT nID);

This function inserts the already created control into the Toolbar. In rect parameter, pass only the width and height.

BOOL AddDropDownButton(UINT nIDButton,UINT nIDMenu,BOOL bArrow=TRUE);

This function a button to a Dropdown with a menu attached to it. Set bArrow to TRUE if you want to show arrow next to it.

void SetToolBarInfoForCustomization();

This function sets the Customization information for the Toolbar. The Names used for the buttons in Customize dialog box are taken from the Tooltip of the Button. (String after Last '\n' of Prompt in Button Properties in Toolbar resource editing.) Call this function after the creation of the Toolbar is done. i.e. Controls, Dropdown have been added.

void MarkDefaultState();

This function sets the default state of the Toolbar. The default state is set when Reset button of Customize Dialog Box is pressed.

void SaveState()

This function saves the State of the Toolbar in the Registry.

void RestoreState()

This function restores the State of the Toolbar from the Registry.

BOOL m_bHideChildWndOnVertical;

This flag controls whether the Controls are visible in the Vertical docking mode. Default Value is TRUE

BOOL HasButtonText( int nID)

This function is used to determine whether the button has Text in "Selective Text on Right". At present it returns TRUE for all. Override this to provide new logic. nID is the command Identifier.

Minimum Requirements

It requires 5.80 version of the Commctl32.dll. It uses few features of 5.81 version, but they seem to work fine on 5.80 also. Please look at the Demo for full details.

Thanks to all Code Project /Code Guru Developers.

History

  • 16 Oct 2001 - updated download files
  • 20 Dec 2001 - updated files, new .NET style!

License

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

Share

About the Author

Deepak Khajuria
Web Developer
United States United States
Programming in MFC/ATL for 2-3 years.

Comments and Discussions

 
BugApplication crashes... Pin
Denny V Abraham27-Jun-12 20:57
memberDenny V Abraham27-Jun-12 20:57 
Generala bug with 'toTextOnRight' option Pin
01_mental23-Aug-09 23:35
member01_mental23-Aug-09 23:35 
QuestionHow to Save/Restore CToolBar in/from *.INI file? Pin
p2002ad5-Aug-09 15:11
memberp2002ad5-Aug-09 15:11 
GeneralThanks... Pin
Maximilien10-Feb-09 9:36
memberMaximilien10-Feb-09 9:36 
QuestionProblem with docking on left or right (vertical) [modified] Pin
ldaoust15-Sep-07 15:26
memberldaoust15-Sep-07 15:26 
AnswerRe: Problem with docking on left or right (vertical) Pin
ldaoust15-Sep-07 16:57
memberldaoust15-Sep-07 16:57 
GeneralA Bug? :~ Pin
KarstenK5-Apr-06 23:00
memberKarstenK5-Apr-06 23:00 
QuestionProblem with tooltip Pin
duronebis7-Jan-06 9:56
memberduronebis7-Jan-06 9:56 
GeneralCutting Toolbar in VC++ 7.1, and customizing not work in dialogs Pin
csavie8-Aug-05 15:47
membercsavie8-Aug-05 15:47 

Hi All,

First thanks and congratulations for this code.

For me it works fine in VC++ 6.0, I have a Combo in the toolbar. But when I updated my dialog application to VC++ 7.1, ComboBox appears ok, but the toolbar buttons are taller than the "normal" toolbar button, causing all buttons to become cut off at the bottom of the toolbar itself.

Can anyone help me?, thanks in advance.

I have another problem with this class. It seems to me that customization feature can't work in a dialog, some one has a solution?


Thanks a lot.

Csavi
GeneralXP Theme Support Pin
Justin Hallet24-Jun-05 6:23
memberJustin Hallet24-Jun-05 6:23 
GeneralIt works but the example source is a jungle... Pin
Panic2k318-Apr-05 13:01
memberPanic2k318-Apr-05 13:01 
GeneralCustomization for a Dialog Pin
csavie5-Apr-05 2:37
membercsavie5-Apr-05 2:37 
GeneralRe: Customization for a Dialog Pin
shawkins2-Feb-06 15:03
membershawkins2-Feb-06 15:03 
GeneralA handle to the MENU of a drop down button Pin
Alex Evans16-Nov-04 14:34
memberAlex Evans16-Nov-04 14:34 
GeneralFeature request: Inserting CToolBarEx into a CDialog Pin
Brian van der Beek7-May-04 13:02
memberBrian van der Beek7-May-04 13:02 
GeneralRe: Feature request: Inserting CToolBarEx into a CDialog Pin
Anonymous14-Jun-04 6:08
sussAnonymous14-Jun-04 6:08 
GeneralHaving several toolbars in one row by default Pin
DonGuitar28-Apr-04 0:50
memberDonGuitar28-Apr-04 0:50 
GeneralNice, but bad size when docking isn't enabled Pin
tangens23-Jan-04 6:14
membertangens23-Jan-04 6:14 
GeneralRe: Nice, but bad size when docking isn't enabled Pin
Deelip13-Aug-05 21:05
memberDeelip13-Aug-05 21:05 
GeneralIcon Background in Customize Dialog Pin
Nicolas Bonamy14-Mar-03 1:29
memberNicolas Bonamy14-Mar-03 1:29 
GeneralLoading bitmap on toolbar Pin
Anonymous11-Nov-02 5:05
sussAnonymous11-Nov-02 5:05 
Generalcontext menu error when more than one toolbar Pin
arc25-Sep-02 16:01
memberarc25-Sep-02 16:01 
GeneralRe: context menu error when more than one toolbar Pin
Kirk318-Dec-02 2:17
memberKirk318-Dec-02 2:17 
GeneralRe: context menu error when more than one toolbar Pin
DonGuitar28-Apr-04 0:29
memberDonGuitar28-Apr-04 0:29 
GeneralBasic CToolBar Pin
AnthonyWinters8-Aug-02 9:03
memberAnthonyWinters8-Aug-02 9:03 
QuestionButtons are disabled? Pin
noble19-May-02 20:25
membernoble19-May-02 20:25 
AnswerRe: Buttons are disabled? Pin
tangens21-Dec-05 6:33
membertangens21-Dec-05 6:33 
Generalhorizontal separators if vertically docked Pin
Anonymos2-Apr-02 22:51
memberAnonymos2-Apr-02 22:51 
GeneralCOMCTL32.DLL Version Pin
Gregory Nisnevich22-Mar-02 7:55
memberGregory Nisnevich22-Mar-02 7:55 
GeneralRe: COMCTL32.DLL Version Pin
Deepak Khajuria24-Mar-02 21:20
memberDeepak Khajuria24-Mar-02 21:20 
QuestionHow to set default style Pin
Azrul20-Mar-02 13:32
memberAzrul20-Mar-02 13:32 
AnswerRe: How to set default style Pin
Deepak Khajuria20-Mar-02 18:53
memberDeepak Khajuria20-Mar-02 18:53 
GeneralRe: How to set default style Pin
Azrul24-Mar-02 4:49
memberAzrul24-Mar-02 4:49 
GeneralGreat but problem if introducing CSliderCtrl Pin
pelikhan22-Feb-02 3:45
memberpelikhan22-Feb-02 3:45 
GeneralRe: Great but problem if introducing CSliderCtrl Pin
Nils Schneider11-Aug-02 23:48
sussNils Schneider11-Aug-02 23:48 
GeneralNope Pin
Jonathan de Halleux12-Aug-02 0:19
memberJonathan de Halleux12-Aug-02 0:19 
GeneralRe: Great but problem if introducing CSliderCtrl Pin
Anonymous26-Sep-02 14:07
sussAnonymous26-Sep-02 14:07 
GeneralRe: Great but problem if introducing CSliderCtrl Pin
la Torre Berardino11-Dec-03 3:00
sussla Torre Berardino11-Dec-03 3:00 
GeneralToolBar buttons (with text) in different sizes Pin
michal_zu25-Nov-01 4:11
membermichal_zu25-Nov-01 4:11 
GeneralRe: ToolBar buttons (with text) in different sizes Pin
Azrul20-Mar-02 13:28
memberAzrul20-Mar-02 13:28 
Generalcan't find customizedlg.h and the .dsp file!! Pin
HKMP56-Oct-01 19:49
memberHKMP56-Oct-01 19:49 
GeneralDisable toolbar button Pin
ALT7-Sep-01 15:51
memberALT7-Sep-01 15:51 
GeneralLooks like a bug Pin
Vigen Dolbakyan21-Aug-01 10:32
memberVigen Dolbakyan21-Aug-01 10:32 
GeneralRe: Looks like a bug Pin
Deepak Khajuria21-Aug-01 20:27
memberDeepak Khajuria21-Aug-01 20:27 
GeneralEven more control over Customization Pin
Don Sanders6-Jul-01 11:10
memberDon Sanders6-Jul-01 11:10 
GeneralRe: Even more control over Customization Pin
jayakumar6-Jul-01 13:49
memberjayakumar6-Jul-01 13:49 
GeneralRe: Even more control over Customization Pin
Kannan Kalyanaraman23-Sep-01 3:18
memberKannan Kalyanaraman23-Sep-01 3:18 
GeneralRe: Even more control over Customization Pin
Anonymous22-Oct-01 16:57
memberAnonymous22-Oct-01 16:57 
GeneralRe: Even more control over Customization Pin
Anonymous6-Feb-04 22:02
sussAnonymous6-Feb-04 22:02 
GeneralAll your links are broken Pin
Christian Graus4-Jul-01 18:14
memberChristian Graus4-Jul-01 18:14 

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
Web03 | 2.8.150428.2 | Last Updated 19 Dec 2001
Article Copyright 2001 by Deepak Khajuria
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid