Click here to Skip to main content
Email Password   helpLost your password?

Sample Image - toolbardialog.jpg

Introduction

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.

    Example:

    LONG CTDDlg::ToolbarButtonCommand(UINT uButtonID)
    {
        CString msg;
        msg.Format("Button with ID %d was pressed in toolbar",uButtonID);
        AfxMessageBox(msg);
        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:

    PositionToolbar(TS_FLOATING);
  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:

    ON_UPDATE_COMMAND_UI_RANGE(ID_BUTTON32771,ID_BUTTON32775,OnUpdateBtn)
    
    void CTDDlg::OnUpdateBtn(CCmdUI* pCmd)
    {
          if(pCmd->m_nID == ID_BUTTON32771)
                pCmd->Enable(FALSE);
    }
  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

Version 1.0.1 - 2004 April 8

Version 1.0.2 - 2004 April 9

Acknowledgments

Usage

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Questionhi,very nice
codeangel
19:35 13 Sep '07  
thank you,
Smilefor us provide this nice toolbar class.

df

GeneralUPDATE_COMMAND_UI problem in a Single Document application
Bumbala
5:29 13 Nov '06  
I am developing a single document application (already has a CFrameWnd). I am trying to use the CToolbarDialog class in one of the dialogs and UPDATE_COMMAND_UI does not work in this case. Any ideas?
Thanks.
GeneralTrue Colour Toolbars - How To:
Dangta
16:30 18 Sep '06  
Hey guys, thanks to this excellent class i can now have toolbars in my dialogs, i've searched around on how to make true colour toolbars, and i found an article at code guru: http://www.codeguru.com/cpp/controls/imagelist/openfaq/article.php/c9045/

I've sucessfully implemented that into this class and it works fantastic, if you want true colour toolbars with this go use that! It's as simple as replacing CToolBar with CTCToolBar and then creating and importing your new toolbar bitmaps for disabled, normal and hover. After adding the bitmaps just add an include to "Resource.h" and then in CSmartToolBar::Initialize add a call to LoadTCToolBar after LoadToolBar and there you go. Big Grin
GeneralWorks Great!
ajselvig
7:41 8 Feb '06  
I don't know why Microsoft didn't put this functionality into the regular CDialog class, but this is exactly what I was looking for and works great. Thanks.

Andy Selvig
Systems/Aerodynamics Engineer
Goodrich Sensor Systems

-- modified at 12:41 Wednesday 8th February, 2006
GeneralNice work, but how to disable the docking function
taxtdod
19:56 4 Oct '05  
I only want it to stay on the top...
GeneralRe: Nice work, but how to disable the docking function
tim635
1:27 6 Oct '05  
That seems quite straightforward.
I think you must be able to do this yourself. Give it another try.
If you still have questions, post them.

greets.

GeneralDouble Click on Toolbar
Morteza Ghasemi
4:40 29 Jun '05  
When i Double Click on Toolbar it's Hide. Why?Confused and how can i repair it?

GeneralRe: Double Click on Toolbar
Lucian Barbulescu
23:06 29 Jun '05  
The toolbar reacts to LBUTTONDOWN and LBUTTONUP messages. On LBUTTONDOWN the floating toolbar is hidden and a rectangle is display (this is for moving the toolbar). On LBUTTONUP the toolbar is displayed in the new position. On double click the following messages are created: WM_LBUTTONDOWN, M_LBUTTONUP, WM_LBUTTONDBLCLK, WM_LBUTTONUP. So, at this moment, it is quite impossible to remove the double click behaviour. If you find a way, please let me know so I could make the required modifications

-----------------------------------------
Lucian - Florentin Barbulescu
Software Developer
C.S. A.E.I.C. S.A.

"Trust is a weakness" - Uplink Hacker Elite

GeneralRe: Double Click on Toolbar
psj699
16:16 4 Oct '05  
I guess that the toolbar normally acts in the following case.
At first, attached toolbar moves other place and then attaches On top, or On bottom ...
After that, when Double click the toolbar, toolbar isn't hidden.
I guess initial setting values are different.
I wish this is gonna help you find a way.
Good Luck~~Smile


-- modified at 2:22 Wednesday 5th October, 2005
Generalnice article, 1 more question :)
mynh79
4:12 16 Aug '04  
Can I use this class for a toolbar that has more than one "row", like emoticon bar in Yahoo Messenger ?
Thankx in advance Smile
GeneralFail to ???
KW-Rix
22:43 10 Aug '04  
Hello, It's me again .
When I was trying to call another Dialog window ,
It failed !!
Please kindly tell me how to fix this problem ??
Thanks !!
Confused


KW-Rix
GeneralRe: Fail to ???
KW-Rix
22:54 10 Aug '04  
The situation is I was bound a MENU on the Dialog,
And from which call another dialog.OMG

KW-Rix
GeneralRe: Fail to ???
Lucian Barbulescu
22:52 11 Aug '04  
I've created a dialog dinamicaly, but I didn't had any problem. Can you tell me exactly wich was the error (if you can also post some code it will help me figure out how to fix the problem)

-----------------------------------------
Lucian - Florentin Barbulescu
Software Developer
Xtreme Software
www.xts.ro

"Trust is a weakness" - Uplink Hacker Elite

GeneralGreat ,But Questions ??
KW-Rix
23:34 9 Aug '04  
First, I'd like to say that your work is so great, thank you.
but when I set bottom text on the toolbar class,
It seems doesn't work !! As below :
*************************************************************
CString xbtn_Str;
xbtn_Str.LoadString(IDS_STRBTN0);
m_pToolbar->SetButtonText(0, xbtn_Str);
*************************************************************
And then I tried to do this :
m_pToolbar->SetSizes(CSize(50,50),CSize(32,32));
At result I just saw a little head of the bottom text on it.
Again I tried to set the toolbar class like this :
m_pToolbar->SetHeight(50);
Still can not see the bottom text.
Can you tell me how to fix this problem , thank you !!
^_^         Confused

KW-Rix
GeneralRe: Great ,But Questions ??
Lucian Barbulescu
0:14 10 Aug '04  
add the button text in CSmartToolbar::Initialise function after LoadToolBar(uTemplateID);

For example:

void CSmartToolbar::Initialize(CWnd* pParent, UINT uTemplateID)
{
DWORD style = WS_CHILD|WS_VISIBLE|CBRS_FLYBY|CBRS_GRIPPER|CBRS_SIZE_DYNAMIC|CBRS_TOOLTIPS;

if(m_bState & TS_FIXED)
style|=(m_bState&TS_FIXED);
else
style|= TS_TOP;
CreateEx(pParent,TBSTYLE_FLAT,style);


LoadToolBar(uTemplateID);

CString xbtn_Str;
xbtn_Str.LoadString(IDS_STRBTN0);
SetButtonText(0, xbtn_Str);

CRect rctButton;
GetItemRect(0,&rctButton);
SetSizes(CSize(rctButton.Width(),rctButton.Height()),CSize(16,15));

}



-----------------------------------------
Lucian - Florentin Barbulescu
Software Developer
Xtreme Software
www.xts.ro

"Trust is a weakness" - Uplink Hacker Elite

GeneralRe: Great ,But Questions ??
KW-Rix
21:46 10 Aug '04  
I'm Done !! Thank You Very Much !! ^_^Big Grin

KW-Rix
GeneralHow I Add more than 1 toolbar
cungdaus
22:23 16 Jun '04  
Because Toolbar is loaded into construction Dialog, so I do know, How I can Load more than one Toolbar
answer me as soon as possiple
thanks very much.
GeneralRe: How I Add more than 1 toolbar
Lucian Barbulescu
22:45 16 Jun '04  
As the class is written at this moment, you can not. I don't think that I will add support for this in the future, but you are free to modify the code to fitt your requirements.

-----------------------------------------
Lucian - Florentin Barbulescu
Software Developer
Xtreme Software
www.xts.ro

"Trust is a weakness" - Uplink Hacker Elite

GeneralAnother way...
Anna-Jayne Metcalfe
0:48 13 Apr '04  
Another way to do this is to create a frame window as a child of the dialog. That then gives you access to all of the docking and command routing capabilities of CFrameWnd.

That's how this page[^] was implemented, for example.

Both approaches are equally valid of course...which is more appropriate depends on the requirements of the UI.



Anna Rose

Homepage | Tears and Laughter

"Be yourself - not what others think you should be"
- Marcia Graesch

"Anna's just a sexy-looking lesbian tart"
- A friend, trying to wind me up. It didn't work.

Trouble with resource IDs? Try the Resource ID Organiser Visual C++ Add-In

GeneralOh, and UPDATE_COMMAND_UI
=[ Abin ]=
0:30 9 Apr '04  
How to make the toolbar response to UPDATE_COMMAND_UI event? Thanks.
GeneralRe: Oh, and UPDATE_COMMAND_UI
Lucian Barbulescu
4:46 9 Apr '04  
Well, version 1.0.1 doesn't support this. I've added support to UPDATE_UI in version 1.0.2

-----------------------------------------
Lucian - Florentin Barbulescu
Software Developer
Xtreme Software
www.xts.ro

"Trust is a weakness" - Uplink Hacker Elite

GeneralRe: Oh, and UPDATE_COMMAND_UI
=[ Abin ]=
6:06 9 Apr '04  
Excellent! Thanks a lot, you've got my 5.
GeneralVC6 Compatibility
=[ Abin ]=
16:28 8 Apr '04  
The code cannot be compiled in VC6 simply because there is no such member function called SetPoint in class CPoint(VC6), really easy to fix, of course.Big GrinBig GrinBig Grin
GeneralRe: VC6 Compatibility
Lucian Barbulescu
4:45 9 Apr '04  
Fixed in version 1.0.2

-----------------------------------------
Lucian - Florentin Barbulescu
Software Developer
Xtreme Software
www.xts.ro

"Trust is a weakness" - Uplink Hacker Elite

GeneralSomething is wrong ...
ReorX
22:49 7 Apr '04  
... if the toolbar is attached to the right or the left: you can't drag it anymore from there with the handle. Only way to retrieve it is by double-clicking and dragging it afterwards somewhere else. Probably easy to fix.



The Saviour of the World is a Penguin and Linus Torvalds is his Prophet.


Last Updated 7 Apr 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010