Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC

Using Toolbar on a Dialog Based Application

Rate me:
Please Sign up or sign in to vote.
2.47/5 (20 votes)
19 Sep 2009CPOL2 min read 62.3K   25   8
This article will put together the information on how to use a toolbar on a Dialog

Introduction

Hi folks. With this article, I will try to put together all the necessary information that is required for getting started with the toolbars.

There are many articles written on this topic. However, I am trying to put together the useful information from various sources.

Coming to the point, my first suggestion is:

If you want to use a toolbar, don't go for a dialog box. But for the applications that are already developed with dialog box and then need a toolbar to be added, this article is useful.

I am using VS 2003, but I don't think the steps will change a lot in other VS versions.

Let's start it from scratch. Step by Step.

  1. Right click on Solution Explorer -> Add -> Resource -> Tool Bar -> New. This will open into Tool Bar Editor. 
  2. You can Create/Paint Buttons here. Make sure you change the default Ids of these buttons to some proper usable ids. (Use Property Explorer for this.) This will create a .bmp image in resources. (Let's say, the Tool Bar is TDR_TOOLBAR1 and the two buttons are IDC_TBUTTON1 and IDC_TBUTTON2.)
  3. Now go to the .h for dialog (You can add a class to a dialog) on which the toolbar needs to be placed. Declare a CToolBar object:
    C++
    CToolBar m_FirstToolBar
  4. Now on the .cpp file of the dialog, in OnInitDialog() event, add following code (you can add OnInitDialog event directly by declaring it in the .h file added and using it in .cpp file):
    C++
    if(!m_FirstToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | 
    	WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | 
    	CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || 
    	!m_FirstToolBar.LoadToolBar(IDR_TOOLBAR1))
    {
       EndDialog(IDCANCEL);
    }
    
    RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,0);
    1. Here you can see the various attributes that can be set for the toolbar in CreateEx(...) like TBSTYLE_FLAT for flat look of toolbar buttons.
    2. In LoadToolBar(...), the Resource created for the toolbar is used. This maps our toolbar object m_FirstToolBar with resource IDR_TOOLBAR1.
    3. RepositionBars() is used to place the toolbar on the dialog.
  5. MoveWindow() can be used to place the toolbar at the desired position.
    C++
    m_FirstToolBar.MoveWindow( 50, 0,100, 100, 1 );

    Now if you have finalized the place where you want to place the tool bar, you can put it there using MoveWindow(...). My suggestion is to put a label in the design of dialog at the place where you want to put your toolbar. Then at run time, get the position of this label and place the toolbar there using MoveWindow(...).

    If you want to place this toolbar on the Top Left and dynamically move all the controls accordingly and increase the dialog dimensions, refer to Tips for dialog based applications.

  6. This is how the buttons of Tool Bar can be made visible/invisible:
    C++
    m_FirstToolBar.GetToolBarCtrl().HideButton(IDC_TBUTTON1, TRUE);

    Notice that we have used the IDC_TBUTTON1 which we declared at the time of creating toolbar resource in Step 2.

  7. Similarly, the following can be used to disable the button:
    C++
    m_FirstToolBar.GetToolBarCtrl().EnableButton(IDC_TBUTTON1,false)
  8. To handle Click events:
    1. Add following line to BEGIN_MESSAGE_MAP():
      C++
      ON_COMMAND(IDC_TBUTTON1,OnToolBarButton1) 
    2. Write the desired code in the .cpp file:
      C++
      FirstToolBarDialog:: OnToolBarButton1()
      {
      
      - - - 
      
            - - - 
      }

That's it friends. This is all I have to share with you for now on this topic. Suggestions/comments are most welcome.

License

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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe bitmap will blink when cursor moves on Pin
Jason.LYJ20-Feb-13 14:56
professionalJason.LYJ20-Feb-13 14:56 
Generalthx Pin
Member 89009011-Mar-12 23:22
Member 89009011-Mar-12 23:22 
GeneralMy vote of 1 Pin
transoft24-Sep-09 12:15
transoft24-Sep-09 12:15 
GeneralThanks Pin
snake.ma7718-Sep-09 16:26
snake.ma7718-Sep-09 16:26 
GeneralThank You Very Much Pin
TechnoRob6-Jun-08 5:26
TechnoRob6-Jun-08 5:26 
Generalthx. you can also add CheckButton() like a HideButton() or as EnableButton() to get a "sticky-button" effect Pin
AHTOXA5-Jan-08 2:02
AHTOXA5-Jan-08 2:02 
GeneralThanks Pin
Kharfax8-Mar-07 8:40
Kharfax8-Mar-07 8:40 
Generalthanks Pin
[ku]31-Dec-06 2:46
[ku]31-Dec-06 2:46 
you help me start pro a toolbars

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.