Virtual Developer Workshop: Containerized Development with Docker

Environment: VC6, VC7


Since Microsoft got rid of ClassWizard in VC++ 7, quite a lot of jobs have become much more difficult. This article shows how to hook up the normal click events for toolbars in VC7, and how to disable buttons programmatically, depending on context.

First, let us assume we have an SDI application created using AppWizard. It has three toolbar buttons, as shown in this figure:

The buttons are called ID_TBRED, ID_TBGREEN, and ID_TBBLUE respectively.

Creating Event Functions for the Buttons

If we built the program above just by adding in the toolbar buttons, they would by default be disabled, which is not very useful. Now we want to add an event function to each button so they do something when clicked.

First, let's create the functions for each of them. If you are following my SDI example, it is likely you will want to define the functions in your view. Open the header file for your view (double-click the class name in Class View), this is CDisableToolbarView in the example.

Okay; we need to write the declarations for our three functions; these would be as so:

  afx_msg void OnToolbarRedClick();
  afx_msg void OnToolbarGreenClick();
  afx_msg void OnToolbarBlueClick();

That's all we need in our header, so we can close that and open our cpp file.

For each of the three functions declared above, we need to create a function defintition like the one below.

  void CDisableToolbarView::OnToolbarRedClick()
    MessageBox("Red clicked");

We've now finished creating the event functions. As they stand, VC++ doesn't know what events to hook our functions up to, so we need to specify this.

Toward the top of the cpp file you will see a block of code starting with BEGIN_MESSAGE_MAP. Within these lines we can hook the click events up to our functions. Between the BEGIN_MESSAGE_MAP and END_MESSAGE_MAP lines, add the following three lines:


By building the application, you should now see the three buttons enabled, and clicking them should show the respective message box. That's all there is to it! Other events can be hand coded in the same way; it's just a matter of knowing the ID of the resource, and the map entry event (in this case, ON_BN_CLICKED).

Disabling Toolbar Buttons

Sometimes, depending on the context, you may want to disable certain toolbar buttons. This can be achieved by hooking another event (ON_UPDATE_COMMAND_UI) that the framework (MFC) calls to check whether the button should be enabled or disabled.

Just as with creating functions for the click event, we need to create another three for the update event. Below the three function declarations in the view header file, we can add the following lines:

  afx_msg void OnToolbarRedUpdate(CCmdUI *pCmdUI);
  afx_msg void OnToolbarGreenUpdate(CCmdUI *pCmdUI);
  afx_msg void OnToolbarBlueUpdate(CCmdUI *pCmdUI);

And in the cpp file, three functions like the one below for each colour:

void CDisableToolbarView::OnToolbarRedUpdate(CCmdUI *pCmdUI)

You should notice the three functions above use bRedEnabled, bGreenEnabled, and bBlueEnabled. These should be declared as bool variable members of the CDisableToolbarView class (or in the doc, if you app has multiple views). The three update functions are called very frequently by the framework, so the code within them should be as minimal as possible.

Again, this will not do anything until we hook up the event to the functions using the message map. At the top of the cpp file, between the BEGIN_MESSAGE_MAP and END_MESSAGE_MAP lines, add the following three lines:


All you need to do now is to set the bool variables accordingly to enable/disable each button. In the case of the example, I set the default state in the constructor:

    : CFormView(CDisableToolbarView::IDD)

As your program changes contexts, you can set the bool variables whenever you like, and the toolbar buttons will change state almost instantly.


Download demo project - 58 Kb


  • ClassWizard has gone?!!!!

    Posted by Legacy on 01/12/2004 08:00am

    Originally posted by: Duncan Mackay

    Has ClassWizard really gone in Visual C++ 7 (.net)????
    Blimey! I was checking the prices today, and even the upgrade cost $1800! Per user! Think I'll stay with the mature product that is VC6 without all that .net rubbish. Let me qualify that - we write CPU-intensive digital decoding software for the PC platform. .NET is of little interest, and by the sounds of it, you just gave me one more reason to stick with VC6 for the time being. Tried your example and it works fine.

    What I'd really like, though, is a tool bar with a transparent background - none of that nasty gray colour! If I get it working, I'll be sure to post....

    Cheers - Duncan.

    • Mmm, .NET rubbish?

      Posted by Marc G on 08/08/2004 10:03pm

      "we write CPU-intensive digital decoding software for the PC platform" With Visual C++ 7(.net) you can still create standard C++ apps, without using the .NET framework ... and on top of that the new compiler in vc++ 7 has better optimization tricks etc, so your apps might even work faster... and calling .NET rubbish clearly states that you have never taken even a quick look at the .NET framework. It is really powerfull.

  • That was timely !

    Posted by Legacy on 10/29/2002 08:00am

    Originally posted by: Sathish Kumar

    I was just looking for this stuff for the past one week.
    I was using EnableButton of CToolBarCtrl as

    m_Toolbar.GetToolBarCtrl ().EnableButton ( ... )

    and the buttons kept getting enabled once i am through the
    call, ... thanks alot.


  • First build MENU, name ID, and than add Buttons to ToolBar

    Posted by Legacy on 09/02/2002 07:00am

    Originally posted by: mjk

    VC7 is can do it automatic, but the process is like the follow:
    First build MENU, name ID_1....ID_X, and than add Buttons to ToolBar according to ID_1....ID_X.

  • Adding events to toolbar buttons

    Posted by Legacy on 08/13/2002 07:00am

    Originally posted by: Hal

    In class view right click on the class you want to add the handler to and select properties. Click on the lightning bolt (events) and select the button ID from the list. Expand the item to show the command and update_command_ui selections. Click on the event you want to add and then click on the arrow in the drop down box.


  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date