Click here to Skip to main content
13,249,174 members (33,380 online)
Click here to Skip to main content
Add your own
alternative version


55 bookmarked
Posted 6 Apr 2004

CToolbarDialog - dialog with floating toolbar

, 6 Apr 2004
Rate this:
Please Sign up or sign in to vote.
A CDialog derived class that contains a floating toolbar.

Sample Image - toolbardialog.jpg


This article shows how to add a floating/docking toolbar to a dialog.

In MSDN, there is a sample (dlgcbr32) in which is presented a way to add a fixed toolbar to a dialog. I have extended that example and created a CDialog derived class that contains a toolbar which can be either fixed, floating or hidden. The floating toolbar effect is obtained by dynamically creating a modeless dialog box which contains a fixed toolbar.

Using the code

To use the code, you must do the following steps:

  1. Include ToolbarDialog.h and ToolbarDialog.cpp in your project.
  2. Derive your dialog from CToolbarDialog:
    #include "ToolbarDialog.h"
    class CTDDlg : public CToolbarDialog

    Also make sure that you pass at least the ID of the toolbar resource you want to use to the CToolbarDialog constructor. This constructor has four parameters: the first two are the dialog ID and parent (just like CDialog), the third is the ID of the toolbar resource and the fourth is the initial state of the toolbar. Possible values are:

    TS_HIDDEN       //toolbar is not shown
    TS_FLOATING     //toolbar is floating
    TS_LEFT         //toolbar is fixed in the left size of the dialog
    TS_TOP          //toolbar is fixed at the top of the dialog
    TS_RIGHT        //toolbar is fixed in the right size of the dialog
    TS_BOTTOM       //toolbar is fixed at the bottom of the dialog

    Here is an example:

    CTDDlg::CTDDlg(CWnd* pParent /*=NULL*/)
        :CToolbarDialog(CTDDlg::IDD, pParent,IDR_TOOLBAR1,TS_HIDDEN)
  3. In your class, declare and implement the function:

    virtual LONG ToolbarButtonCommand(UINT uButtonID);

    This function receives as parameter the ID of the toolbar button that was pressed. By implementing this function, you can add handlers to the toolbar buttons.


    LONG CTDDlg::ToolbarButtonCommand(UINT uButtonID)
        CString msg;
        msg.Format("Button with ID %d was pressed in toolbar",uButtonID);
        return 0;
  4. If you want to set the state of the toolbar dynamically, just call PositionToolbar(DWORD dwPosition). The values the parameter can have are the ones described earlier.

    For example, to show the toolbar floating, just add the following function call to your code:

  5. To enable/disable the toolbar buttons, just add the UpdateCommandUI handlers to your dialog and edit the methods as you would, in every Frame/View application.

    For example:

    void CTDDlg::OnUpdateBtn(CCmdUI* pCmd)
          if(pCmd->m_nID == ID_BUTTON32771)
  6. If you want to have tooltips for the toolbar buttons, follow the steps described in Randy More's article.

You now have a dialog that support floating toolbars.

I have tested this class only on Windows 2000, but I think it will also work on Win9x and WinXP.

Revision History

Version 1.0.0 - 2004 April 7

  • First release.

Version 1.0.1 - 2004 April 8

  • FIXED BUG: when toolbar is attached to the right or the left, no dragging possible by clicking on the handle (thanks to ReorX for informing me about this bug).
  • FIXED BUG: when the floating toolbar is docked, the parent dialog does not receives the focus.

Version 1.0.2 - 2004 April 9

  • ADDED: VC6 compatibility
  • ADDED: Tooltips support (thanks to Randy More for his article about adding tooltips to toolbars in dialogs)
  • FIXED BUG: UPDATE_COMMAND_UI does not work (thanks to =[ Abin ]= for informing me about this bug)


  • Thanks to Cristian Teodorescu for giving me the idea of using a dynamic dialog for the floating toolbar.
  • Thanks to Microsoft Corporation for their Dlgcbr32 sample


You are free to use this software in any personal or freeware application. If you use this software in any shareware or commercial application, you MUST get my permission first. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.


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

Lucian Barbulescu
Software Developer (Senior) CS Romania
Romania Romania
No Biography provided

You may also be interested in...

Comments and Discussions

Questionfile is bad! Pin
xuhuan10-Jan-14 2:00
memberxuhuan10-Jan-14 2:00 
Questionhi,very nice Pin
codeangel13-Sep-07 19:35
membercodeangel13-Sep-07 19:35 
GeneralUPDATE_COMMAND_UI problem in a Single Document application Pin
Bumbala13-Nov-06 5:29
memberBumbala13-Nov-06 5:29 
GeneralTrue Colour Toolbars - How To: Pin
Dangta18-Sep-06 16:30
memberDangta18-Sep-06 16:30 
GeneralWorks Great! Pin
ajselvig8-Feb-06 7:41
memberajselvig8-Feb-06 7:41 
GeneralNice work, but how to disable the docking function Pin
taxtdod4-Oct-05 19:56
membertaxtdod4-Oct-05 19:56 
GeneralRe: Nice work, but how to disable the docking function Pin
tim6356-Oct-05 1:27
membertim6356-Oct-05 1:27 
GeneralDouble Click on Toolbar Pin
Morteza Ghasemi29-Jun-05 4:40
memberMorteza Ghasemi29-Jun-05 4:40 
GeneralRe: Double Click on Toolbar Pin
Lucian Barbulescu29-Jun-05 23:06
memberLucian Barbulescu29-Jun-05 23:06 
GeneralRe: Double Click on Toolbar Pin
psj6994-Oct-05 16:16
memberpsj6994-Oct-05 16:16 
Generalnice article, 1 more question :) Pin
mynh7916-Aug-04 4:12
membermynh7916-Aug-04 4:12 
QuestionFail to ??? Pin
KW-Rix10-Aug-04 22:43
memberKW-Rix10-Aug-04 22:43 
AnswerRe: Fail to ??? Pin
KW-Rix10-Aug-04 22:54
memberKW-Rix10-Aug-04 22:54 
GeneralRe: Fail to ??? Pin
Lucian Barbulescu11-Aug-04 22:52
memberLucian Barbulescu11-Aug-04 22:52 
QuestionGreat ,But Questions ?? Pin
KW-Rix9-Aug-04 23:34
sussKW-Rix9-Aug-04 23:34 
AnswerRe: Great ,But Questions ?? Pin
Lucian Barbulescu10-Aug-04 0:14
memberLucian Barbulescu10-Aug-04 0:14 
GeneralRe: Great ,But Questions ?? Pin
KW-Rix10-Aug-04 21:46
memberKW-Rix10-Aug-04 21:46 
QuestionHow I Add more than 1 toolbar Pin
cungdaus16-Jun-04 22:23
membercungdaus16-Jun-04 22:23 
AnswerRe: How I Add more than 1 toolbar Pin
Lucian Barbulescu16-Jun-04 22:45
memberLucian Barbulescu16-Jun-04 22:45 
GeneralAnother way... Pin
Anna-Jayne Metcalfe13-Apr-04 0:48
memberAnna-Jayne Metcalfe13-Apr-04 0:48 
GeneralOh, and UPDATE_COMMAND_UI Pin
=[ Abin ]=9-Apr-04 0:30
member=[ Abin ]=9-Apr-04 0:30 
GeneralRe: Oh, and UPDATE_COMMAND_UI Pin
Lucian Barbulescu9-Apr-04 4:46
memberLucian Barbulescu9-Apr-04 4:46 
GeneralRe: Oh, and UPDATE_COMMAND_UI Pin
=[ Abin ]=9-Apr-04 6:06
member=[ Abin ]=9-Apr-04 6:06 
GeneralVC6 Compatibility Pin
=[ Abin ]=8-Apr-04 16:28
member=[ Abin ]=8-Apr-04 16:28 
GeneralRe: VC6 Compatibility Pin
Lucian Barbulescu9-Apr-04 4:45
memberLucian Barbulescu9-Apr-04 4:45 

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.171114.1 | Last Updated 7 Apr 2004
Article Copyright 2004 by Lucian Barbulescu
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid