Working with toolbars in Visual C++

W

The buttons on the toolbar correspond to commands, just like the items in a menu. In general, the toolbar buttons are duplicated for menu commands (shortcuts). The toolbar can be added with the AppWizard.

AppWizard creates the toolbar for the most used commands in File, Edit and Help. It is our responsibility to modify the toolbar as we wish.

Each button on the toolbar has an ID that identifies it. Based on this ID we will attach a button command.

Deleting the buttons on the toolbar

Next we will exemplify based on an application, called Tool, with a Document/View and MDI architecture. AppWizard provides a docking toolbar (the toolbar is not fixed, can be moved anywhere in the client area, looks like a floating palette) by default for this application. This functionality is provided by AppWizard and MFC.

To delete a button from the toolbar, you must do the following:

1. Select the Resource View page (tab)
2. We expand Tool Resources to see the application resources
3. Select Toolbar
4. Double-click IDR_MAINFRAME to edit it.
5. Deleting a button is done by dragging the respective button from this list (click on the button, hold the left mouse button pressed and pull the button outside the toolbar and then release the button).

Adding buttons to Toolbar

The addition is done in two steps:
1. draw the button icon by selecting the white button in the toolbar; drawing is done using the tools provided by the programming environment.
2. an order is attached to the new button added; Properties are selected for this new button and we will indicate the Id of this button (ID_CIRCLE in this case).

Description and ToolTip

Next we will define the description of the button and the ToolTip. The description appears in the status bar, and the ToolTip appears when the hover over the button for a second or two. We will enter these two character strings in the Prompt edit box: first the string for the button description and then separated by the newline (\n) string for the ToolTip.

Add button command

There are two situations:
1. the button in the toolbar has a corresponding menu item, ie selecting the button or menu item has the same effect;
2. the toolbar button does not have a corresponding menu item.

In the first case we will choose the same ID for the button as for the menu command. It is assumed that for the menu commands I wrote the code.

In the second case, we will proceed as follows:

1. Select the button for which we want to create the message handling and then open ClassWizard.
2. From the property sheet that appears, with the ID of the button selected, select in the Class Name box, the class to which we will add the function, usually the view class (derived from CView or other classes in this hierarchy).
3. Double-click COMMAND in the Messages box.
4. We accept the name of the function proposed by MFC and then edit the code.

Note: If no command for a button in the toolbar is defined then MFC will make it disabled during execution. The same is true for a menu command.

Recent Posts

Archives

Categories