Click here to Skip to main content
6,931,354 members and growing! (21,052 online)
Email Password   helpLost your password?
 
Desktop Development » Toolbars & Docking windows » Toolbars     Intermediate

CToolbarDialog - dialog with floating toolbar

By Lucian Barbulescu

A CDialog derived class that contains a floating toolbar.
VC6, VC7Win2K, MFC, Dev
Posted:6 Apr 2004
Views:88,448
Bookmarked:46 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 5.27 Rating: 4.13 out of 5
1 vote, 5.3%
1
3 votes, 15.8%
2
1 vote, 5.3%
3
1 vote, 5.3%
4
13 votes, 68.4%
5

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

  • 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)

Acknowledgments

  • 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

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.

License

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


Member

Occupation: Web Developer
Location: Romania Romania

Other popular Toolbars & Docking windows articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh)FirstPrevNext
Questionhi,very nice Pinmembercodeangel19:35 13 Sep '07  
GeneralUPDATE_COMMAND_UI problem in a Single Document application PinmemberBumbala5:29 13 Nov '06  
GeneralTrue Colour Toolbars - How To: PinmemberDangta16:30 18 Sep '06  
GeneralWorks Great! Pinmemberajselvig7:41 8 Feb '06  
GeneralNice work, but how to disable the docking function Pinmembertaxtdod19:56 4 Oct '05  
GeneralRe: Nice work, but how to disable the docking function Pinmembertim6351:27 6 Oct '05  
GeneralDouble Click on Toolbar PinmemberMorteza Ghasemi4:40 29 Jun '05  
GeneralRe: Double Click on Toolbar PinmemberLucian Barbulescu23:06 29 Jun '05  
GeneralRe: Double Click on Toolbar Pinmemberpsj69916:16 4 Oct '05  
Generalnice article, 1 more question :) Pinmembermynh794:12 16 Aug '04  
GeneralFail to ??? PinmemberKW-Rix22: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 ??? PinmemberKW-Rix22:54 10 Aug '04  
GeneralRe: Fail to ??? PinmemberLucian Barbulescu22:52 11 Aug '04  
GeneralGreat ,But Questions ?? PinsussKW-Rix23:34 9 Aug '04  
GeneralRe: Great ,But Questions ?? PinmemberLucian Barbulescu0:14 10 Aug '04  
GeneralRe: Great ,But Questions ?? PinmemberKW-Rix21:46 10 Aug '04  
GeneralHow I Add more than 1 toolbar Pinmembercungdaus22:23 16 Jun '04  
GeneralRe: How I Add more than 1 toolbar PinmemberLucian Barbulescu22:45 16 Jun '04  
GeneralAnother way... PinmemberAnna-Jayne Metcalfe0:48 13 Apr '04  
GeneralOh, and UPDATE_COMMAND_UI Pinmember=[ Abin ]=0:30 9 Apr '04  
GeneralRe: Oh, and UPDATE_COMMAND_UI PinmemberLucian Barbulescu4:46 9 Apr '04  
GeneralRe: Oh, and UPDATE_COMMAND_UI Pinmember=[ Abin ]=6:06 9 Apr '04  
GeneralVC6 Compatibility Pinmember=[ Abin ]=16:28 8 Apr '04  
GeneralRe: VC6 Compatibility PinmemberLucian Barbulescu4:45 9 Apr '04  
GeneralSomething is wrong ... PinmemberReorX22:49 7 Apr '04  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 6 Apr 2004
Editor: Smitha Vijayan
Copyright 2004 by Lucian Barbulescu
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project