Toolbar Button Events and Disabling in Visual C++ 7

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

More by Author

Must Read