Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Chapter 4
Full Power Menus, Toolbars, and Status Bars

In this chapter, we explore the power we can add to our programs with menus, toolbars, and status bars. Anyone familiar with Windows knows about these items, and in this chapter, we see how to make use of them.

We dig deeper than introductory books would. For example, we work through standard menu handling—such as menu accelerators, shortcut keys, checkable menu items, and more—but we also see how to change menus on the fly, how to support popup menus anywhere in a program’s window, and how to support bitmap menu items. The last chapter gave us a start with menus, so we take it from there.

In the same way, we work through standard toolbar handling, such as adding new buttons, setting up status bar prompts, and more. However, we also see how to add new indicators to the status bar (the small indented boxes at lower right) and make them active, how to add a dropdown combo box to a toolbar (something no Visual C++ design tool lets you do), and other new toolbar topics.

There’s a lot coming up in this chapter, so let’s start at once by working with menus and adding them to our programs.

Checkable Menu Items

Our first menu example shows how to add or remove check marks from menus. We begin by setting up a new menu in our program, Choices, with an item in it captioned “Check Me.”

      ------------------------------------------------------
     |File  Edit  View  Choices  Draw  Help|
     |------------------|   --------- ----------------------|
     |                |  Check Me  |                        |
     |                 -------------                        |
     |                                                      |
     |                                                      |
      ------------------------------------------------------

When the user selects that item, we place a check mark in front of the item in the menu that is visible when the menu is next opened.

      ------------------------------------------------------
     |File  Edit  View  Choices  Draw  Help|
     |------------------|   --------- ----------------------|
     |                  |v Check Me   |                     |
     |                   -------------                      |
     |                                                      |
     |                                                      |
     |                                                      |
     |                                                      |
     |                                                      |
     |                                                      |
      ------------------------------------------------------

If the user selects the item again, we remove it, and so on, toggling the check mark under user control. This is a useful skill to have: You can list various options to users in a menu and let them toggle those options as they like. For example, a Toolbar menu item would, when checked, indicate that the toolbar is visible. In fact, AppWizard programs already support that item in the View menu.

We start by creating a new SDI program named Menus. Open the Visual C++ Menu Editor by double-clicking the menu resource ID IDR_MANIFRAME in ResourceView’s Menu folder. The Menu Editor appears in Figure 4.1.

As we did in the last chapter, create a new menu, this time naming it Choices to indicate that we’re examining the menu choices available to us. We can also add the menu Draw that we work with later in this chapter. This menu supports a bitmapped menu item, as shown in Figure 4.1. Now give the first menu item in the Choices menu the caption “Check Me” by typing the caption into the dotted border that appears around the first item in the menu.


Setting Default Check Marks

To make the Check Me item appear checked when it first appears, you can click the Checked box in the item’s property page. To open its property page, just double-click the item in the Menu Editor.


Menu Shortcuts

We can make life a little easier for the user by adding menu shortcuts to our menus. Menu shortcuts are the underlined letters that appear in menus and menu items; the user can select those menus or items by pressing the Alt key and the shortcut key. You install a shortcut key simply by placing an ampersand—&—in front of the letter in the caption you want to make into the shortcut. For example, changing the menu caption to &Choices makes the C the shortcut key for this menu; changing the menu item caption to Check &Me makes the M the shortcut key for that item.


Figure 4.1  Using the Menu Editor.

To add the code that toggles this item’s check mark, right-click the Check Me menu item and open ClassWizard, as shown in Figure 4.2.

Using ClassWizard, connect functions to both the COMMAND and UPDATE_COMMAND_UI messages in ClassWizard, making sure you add these functions to the CMenusView class. This creates two new functions, OnChoicesCheckMe() and OnUpdateChoicesCheckMe().

void CMenusView::OnChoicesCheckme()
{
    // TODO: Add your command handler code here

}

void CMenusView::OnUpdateChoicesCheckme(CCmdUI* pCmdUI)
{
    // TODO: Add your command update UI handler code here

}

To keep track of the checked or unchecked state of our check mark menu item, we set up a new variable, m_checked:

class CMenusView : public CView
{
protected: // create from serialization only
    
    CMenusView();
    DECLARE_DYNCREATE(CMenusView)
    int m_checked;                              ⇐
     .
     .
     .

Now set that variable to false in CMenusView’s constructor.

CMenusView::CMenusView()
{


Figure 4.2  Using ClassWizard.

m_checked = 0;                                 ⇐

}

The Menus program calls OnUpdateChoicesCheckme() before displaying the check mark item, so we set or remove the check mark in that function. We are passed a pointer to a CCmdUI object in that function; use that object’s SetCheck() function to set or remove the check according to the m_checked variable.

void CMenusView::OnUpdateChoicesCheckme(CCmdUI* pCmdUI)
{
    pCmdUI->SetCheck(m_checked);                      ⇐

}

When the user selects the Check Me menu item, we want to toggle the check mark, which really means toggling the state of m_checked.

void CMenusView::OnChoicesCheckme()
{
    m_checked = !m_checked;                     ⇐

}

Now our program supports a checkable menu item, as shown in Figure 4.3. As you select the Check Me item in the Choices menu, the check mark appears and disappears in front of that item. Our first example is a success.

Let’s work with submenus.

Using Submenus

In our next example, we support submenus. These menus are also called popup menus in Visual C++, but we call them submenus to differentiate them from the free-floating popup menus we develop later in this chapter.

In this case, we add a new menu item to the Choices menu in the Menus program: Submenus. When the user selects this item, a new submenu appears with three menu items in it.

      ------------------------------------------------------
     |File  Edit  View  Choices  Draw  Help                   |
     |------------------|   --------- ----------------------  |
     |                |  Check Me   |                |
     |                |  Submenus > |---------       |
     |                   -------------| Item 1 |     |
     |                                | Item 2 |     |
     |                                | Item 3 |     |
     |                                 --------      |
     |                                               |
     |                                               |
     |                                               |
      ------------------------------------------------------


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.