5,401,186 members and growing! (16,540 online)
Email Password   helpLost your password?
Desktop Development » Toolbars & Docking windows » Toolbars     Intermediate License: The Code Project Open License (CPOL)

ToolBar with Customization and Controls

By Deepak Khajuria

A ToolBar class with customization and Controls
VC6, VC7, C++Windows, NT4, Win2K, MFC, VS.NET2002, VS6, Visual Studio, Dev

Posted: 6 Jun 2001
Updated: 18 Dec 2001
Views: 150,657
Bookmarked: 57 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
26 votes for this Article.
Popularity: 5.80 Rating: 4.10 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
2 votes, 22.2%
3
1 vote, 11.1%
4
6 votes, 66.7%
5

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 "ToolBarEx.h"
    . .
    
    // 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)

About the Author

Deepak Khajuria


Programming in MFC/ATL for 2-3 years.
Occupation: Web Developer
Location: United States United States

Other popular Toolbars & Docking windows articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 48 (Total in Forum: 48) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionProblem with docking on left or right (vertical) [modified]memberldaoust15:26 15 Sep '07  
AnswerRe: Problem with docking on left or right (vertical)memberldaoust16:57 15 Sep '07  
GeneralA Bug? :~memberKarstenK23:00 5 Apr '06  
QuestionProblem with tooltipmemberduronebis9:56 7 Jan '06  
GeneralCutting Toolbar in VC++ 7.1, and customizing not work in dialogsmembercsavie15:47 8 Aug '05  
GeneralXP Theme SupportmemberJustin Hallet6:23 24 Jun '05  
GeneralIt works but the example source is a jungle...memberPanic2k313:01 18 Apr '05  
GeneralCustomization for a Dialogmembercsavie2:37 5 Apr '05  
GeneralRe: Customization for a Dialogmembershawkins15:03 2 Feb '06  
GeneralA handle to the MENU of a drop down buttonmemberAlex Evans14:34 16 Nov '04  
GeneralFeature request: Inserting CToolBarEx into a CDialogmemberBrian van der Beek13:02 7 May '04  
GeneralRe: Feature request: Inserting CToolBarEx into a CDialogsussAnonymous6:08 14 Jun '04  
GeneralHaving several toolbars in one row by defaultmemberDonGuitar0:50 28 Apr '04  
GeneralNice, but bad size when docking isn't enabledmembertangens6:14 23 Jan '04  
GeneralRe: Nice, but bad size when docking isn't enabledmemberDeelip21:05 13 Aug '05  
GeneralIcon Background in Customize DialogmemberNicolas Bonamy1:29 14 Mar '03  
GeneralLoading bitmap on toolbarsussAnonymous5:05 11 Nov '02  
Generalcontext menu error when more than one toolbarmemberarc16:01 25 Sep '02  
GeneralRe: context menu error when more than one toolbarmemberKirk32:17 18 Dec '02  
GeneralRe: context menu error when more than one toolbarmemberDonGuitar0:29 28 Apr '04  
GeneralBasic CToolBarmemberAnthonyWinters9:03 8 Aug '02  
GeneralButtons are disabled?membernoble20:25 19 May '02  
AnswerRe: Buttons are disabled?membertangens6:33 21 Dec '05  
Generalhorizontal separators if vertically dockedmemberAnonymos22:51 2 Apr '02  
GeneralCOMCTL32.DLL VersionmemberGregory Nisnevich7:55 22 Mar '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Dec 2001
Editor: Nishant Sivakumar
Copyright 2001 by Deepak Khajuria
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project