Toolbar Creation Question In Visual C++ 5.0

Posted on 1997-12-07
Last Modified: 2013-12-14
I am trying to create a toolbar under MSVC 5.0.  Below I have included some code that I am using.  It creates the toolbar and displays it, but the buttons will not work because there is no command handlers for them.  I am not sure how to attach command handlers to these buttons.  I will describe the process I used to develop this toolbar.

Step 1: I went to resources and inserted a toolbar resouce.  I then created three buttons.  The Buttons are push buttons.

Step 2: In DMainFrame.h I declared CToolBar *d_pToolbar2;
Step 3: In DMainFrame.cpp I added the following code to DMainFrame::OnCreate function.

      //Initialize Toolbar
      d_pToolbar2 = new CToolBar();
      d_pToolbar2->Create(this,WS_CHILD |CBRS_TOP,0x9100);
        //Loads ToolBar From Resouces
        //Displays ToolBar In Application
Step 4: Build Program and execute.  The program will display the toolbar, but I need to attach the buttons to some kind of command handler.

Here are my Questions

Q1:  I would like the buttons to be represented as ID_TESTBUTTON_ONE, ID_TESTBUTTON_TWO, ID_TESTBUTTON_THREE.  Where do I set these properties for that toolbar?

Q2:  In This book I have it shows creating some code like the following

static UINT BASED_CODE buttons[] =
    //list toolbar buttons as IDs

I added this code, and added the buttons with the IDs I want, but to be expected it said the buttons were undeclared identifiers?

Q3: In this line of code that does the 0x9100 do / stand for in this call?

d_pToolbar2->Create(this,WS_CHILD |CBRS_TOP,0x9100)

Q4:  What do I do next?  Am I on the right track.  How can I finish this off.  

Thank you for your time..
Question by:larockd
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 1175138
There two way to assign command IDs to toolbar buttons. The first and the simplest one is when you create toolbar in resource editor choose(or type) command id in command IDs combo box. You can follow this way if you know command IDs at compiler time and these IDs will not change at runtime. Toolbar sends WM_COMMAND messages with corresponding id to toolbar owner(which is usually frame window) amd in turn MFC message map mechanism allows you to add handler to any class. If you don't know command ID at compiler time you need to add it to each button by calling CToolBarCtrl function SetCmdID or by sending TB_SETCMDID message to the toolbar window. If you use SDK you you must specify command id for each button when you fill buttons structures array

Author Comment

ID: 1175139
In Visual C++ 5.0, when you create a toolbar and use the "Toolbar Editor" there is no Combobox to define an actual button.  What you have to do is go to menu item under the view menu "Resource Symbols."  Then you have to select the button and VC names the buttons like ID_BUTTON_32xxx then you have to select change, then view use.  Then you can change the ID for it.

Can you please answer Questions 2,3 above for me also.  Thank you.  I appreciate your help...

Accepted Solution

galkin earned 50 total points
ID: 1175140
I am sorry for not exact answer. There is ID combobox where you can choose ID for each toolbar button. If you don't want to associate toolbar buttons with any menu items do the following. Create dummy meny using resource editor, add menu items you need(ID_TESTBUTTON_ONE, ID_TESTBUTTON_TWO etc.) then go back to the tool bar editor, now you can find these IDs in ID combo box.
If you create toolbat first it is recommended that you use default toolbar ID(it is default parameter - )
Then you need to load toolbar from resources by calling CToolBar::LoadToolBar and passing resource ID of the toolbar you created using resource editor. Then you can specify toolbar style if you have not done yet by CToolBar::SetBarStyle. If you want your toolbar to be dockable you can call       m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY)
CFrameWnd::EnableDocking(CBRS_ALIGN_ANY) and CFrameWnd::DockControlBar(&m_wndToolBar)


Author Comment

ID: 1175141

Q2: In This book I have it shows creating some code like the following

                  static UINT BASED_CODE buttons[] =
                      //list toolbar buttons as IDs

Q3: In this line of code that does the 0x9100 do / stand for in this call?

 d_pToolbar2->Create(this,WS_CHILD |CBRS_TOP,0x9100)

Expert Comment

ID: 1175142
If you are using VC++ 5.0 AppWizard generates you code inside CFrameWnd::OnCreate exactly what I wrote. Old VC version when toolbar resource didn't exist can create slightly different code, using bitmap resource as toolbar bitmap and button structures as buttons information. So in this case you must use CToolBar::LoadBitmap to load bitmap from resources and CToolBar::SetButtons to set buttons information. Concerning create you have already mentioned you should use AFX_IDW_TOOLBAR as id. This is neccessary to automaticaly reposition your toolbar when resizing parent frame.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c++ syntax question 9 63
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 304
C++ mouse_event mouse look 7 128
Winapi.Windows.hpp problem 7 34
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question