A menu system is one of the most standard features of any Windows application.
Menus are
Figure 4.7 shows a typical Windows application menu system. As you can see, it consists of many different features.
Figure 4.7:
This menu illustrates the contents of the Visual C++ Express 2005 File menu.
Windows application menu systems
Menus. These are the first, or high-level, menu items that are immediately visible from the menu bar. Examples include File, Edit, and Help.
Menu items. These are additional menu items residing under menus, each of which represents a choice that users can make.
Submenus. These are collections of menu items accessed through a parent menu item. You can identify submenus by the black arrow on the right end of the menu item that provides access to them.
Shortcuts.
These are keyboard
Access keys. These are keyboard keys that, when used in conjunction with the Alt key, activate a menu or menu item.
Separators. These are horizontal lines used to organize menu items into logical groups.
|
|
Most users have enough experience with Microsoft Windows that they expect certain minimal standards from the applications that they use. They have come to expect that all Windows applications work in certain similar ways. Any application that fails to meet these expectations runs the risk of disappointing its target audience, no matter how well designed it might be. One major expectation that most Windows users have is that Windows application menus will perform the same way across applications. For example, users have come to expect to see menu headings such as File, Edit, and Help. The File menu, for instance, is generally expected to be first in the menu's list, and Help is often listed last. Options for opening, closing, printing, exiting, and so on typically are located on the File menu. The Exit command is expected to be the last menu item on the File menu. You should avoid upsetting this traditional expectation unless your application has an overriding need to do so.
|
|
The first step in adding a menu system to a Visual C++ application is to select the MenuStrip control from the Toolbox and add it to your form. Doing so creates an instance of the control to the Component Tray. After this control is a part of your form, you can begin defining menus, as demonstrated in Figure 4.8.
Figure 4.8:
You can use the MenuStrip control to add a menu system to a Visual C++ application.
The following steps
Add the MenuStrip control to your form.
Click on the
Type Here
text that is displayed, and type the
To create an additional menu, click on the Type Here text shown just to the right of the first menu heading and type the name of the menu, as demonstrated in Figure 4.10.
To add a menu item under a particular menu, click on the menu, click on the Type Here text that appears just underneath it, and type in the name of the menu item, as demonstrated in Figure 4.11.
To create a submenu, select the menu item that will provide access to the submenu. Then click on the Type Here text located just to the right of the menu item and type the name of the first menu item in the submenu. Then, to complete the submenu, continue clicking on the Type Here text displayed under each new menu item in the submenu, adding as many menu items as required. (See Figure 4.12.)
Repeat steps 4 through 6 as many times as necessary to assemble your application's menu system.
Figure 4.9:
The first menu heading in the application's menu system is defined by typing its name.
Figure 4.10:
Additional menus can be added to the application's menu system.
Figure 4.11:
Menu items can be added by typing in the boxes under a menu heading.
Figure 4.12:
Entering text automatically enables you to add submenu items to the menu system.
One way to make your menus look more professional is to be consistent in how you spell each element. Always be sure that the first letter in each menu or menu item is a capital letter. Also, add an ellipsis ( … ) to the end of any menu name that, when selected, provides access to another window.
| Trick |
After you've started defining your menu items, you're not stuck with their position. You can select a menu item and quickly rearrange it by using drag-and-drop to move it to a new location. You can also right-click on it and select Delete to remove it. |
| Trick |
If you want, Visual C++ can help you start
|
Just as you have done with other controls, you can give menu items code by double-clicking on them. When you do so, the IDE creates a new click event function for the item and
For example, Figure 4.14 illustrates the standard menu added in the previous example. You can add code by double-clicking on any item. You can, for example, associate the following code with the Exit menu to close the application.
Figure 4.14:
Clicking any menu item allows you to easily add code to it.
private: System::Void exitToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { Close(); }
As shown in the following list, you can do a variety of things to make your menu systems easier and more
Adding shortcut keys
Adding access keys
Adding and removing check marks
Organizing menu items using separator bars
Shortcut keys allow users to access menu items using only the keyboard. This can give your users a boost in productivity. You can use shortcuts to quickly initiate commonly used application commands. Examples of shortcut keys include all the function keys (F1 to F12) and keystroke combinations such as Ctrl+N and Ctrl+O.
| Hint |
Some shortcut keys have become so common that they are now a de facto standard for Windows application commands. Examples include Ctrl+S for saving and Ctrl+X for exiting an application. Make sure that you use these shortcuts when appropriate in your applications. |
Adding a shortcut to a menu item is straightforward, as outlined here:
Select the menu containing the menu item that you want to work with.
Select the appropriate menu item.
Click on the value field that is associated with the Shortcut property (in the Properties window) and select one of the shortcuts displayed in the drop-down list, as demonstrated in Figure 4.15.
Figure 4.15:
You can specify the shortcut you want to assign to a menu or menu item.
After you select the shortcut, it's immediately visible, as demonstrated in Figure 4.16.
Figure 4.16:
You can specify shortcut keys to activate menus and menu items.
Access keys are another method for allowing users to activate specific menu items using their keyboard. To establish an access key, preface one of the
To use an access key, all you have to do is press and hold the Alt key while
The following steps outline the process involved in adding access keys to your Visual C++ menu and menu items.
Select the menu heading or menu item that you want to work with.
Position the cursor in front of the letter in the menu or menu item name that you want to
Add the ampersand ( & ) character in front of the selected letter (for example &File or E&xit ). The results are immediately visible, as demonstrated in Figure 4.17.
Figure 4.17:
This example shows the process of adding access keys to menus and menu items.
Another feature that users might find convenient is the check mark. Check marks are useful when you need to identify the status of menu items that you can toggle on and off. The presence of a check mark indicates that the menu item has been selected or enabled. Similarly, the absence of a check mark indicates that the menu item has been deselected or disabled. For example, later in this chapter when you develop the
Lottery Assistant
game, you use check marks so that the
You can set the initial check mark setting at design time using the following method:
Select the menu item that you want to work with.
Right-click to bring up the item's context menu. Click on the Checked option, as demonstrated in Figure 4.18.
Figure 4.18:
A check mark indicates when a menu item has been selected.
| Trick |
You can also set the check mark value by selecting the appropriate menu item and then setting the Checked property in the Properties window to true . |
Any menu item that can be checked can also be unchecked. When you click a menu item, it is appropriate to toggle the checked status of the item. Therefore, you need to know how to change the checked status of your menu items at runtime, as demonstrated in the following example:
private: System::Void grayToolStripMenuItem_Click(System::Object
^
sender, System::EventArgs^ e) { grayToolStripMenuItem->Checked = true; yellowToolStripMenuItem->Checked = false; whiteToolStripMenuItem->Checked = false; }
Here, the statement marks the Gray menu item as checked whenever the user clicks on it and
private: System::Void yellowToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { grayToolStripMenuItem->Checked = false; yellowToolStripMenuItem->Checked = true; whiteToolStripMenuItem->Checked = false; }
Separator bars divide long lists of menu items and make them easier to read. They are also an
Of course, the separator can
Begin creating your menu items.
When you get to a point where you want to insert a separator bar, click the drop-down arrow next to Type Here.
A list of options appears. Select the Separator option from the list. A bar is added to your menu, separating its contents, as shown in Figure 4.19.
Figure 4.19:
Using separator bars can visually group related menu items.
Based on what your application is doing, you might need to prevent a user from being able to click on a given menu item. For example, later in this chapter, you work on developing the
Lottery Assistant
game. This game's menu system controls it, and it includes Get Numbers and Clear Numbers menu items on its File menu. The game enables and disables access to the Get Numbers menu item based on whether the user has entered all the data required to retrieve lottery
When a menu item is disabled, it appears to the user to be grayed out and doesn't respond when clicked. By default, menu items are enabled. However, you can disable menu items at design time using the following function:
Select the menu or menu item that you want to work with.
Using the Properties window, set the Enabled property for the menu or menu item to true to enable the menu or menu item (the default) or set it to false to disable it. As you can see in Figure 4.20, the Yellow menu item has been disabled.
Figure 4.20:
Menu items can be disabled at design time.
This functionality is not very useful, however, if you don't add program statements to your code that enable your menu items at the appropriate time. Doing so is a simple process, fortunately, as demonstrated next. In the following example, the previously disabled Yellow menu option is enabled when the user selects the White menu option.
private: System::Void whiteToolStripMenuItem_Click(System::Object
^
sender, System::EventArgs^ e) { yellowToolStripMenuItem->Enabled = true; }
| Trap |
If you disable a menu heading or a menu item that provides access to a submenu, all menu items and submenus underneath it become inaccessible. Figure 4.21
|
Although disabling menu items is a frequent technique that applications use to hide options that are not relevant or appropriate to whatever your user is currently doing, sometimes this is not enough. In such cases, you can go a step further by completely hiding and later
Select the menu or menu item that you want to work with.
Using the Properties window, set the Visible property for the menu or menu item to true to enable its display (the default) or set it to false to disable it. The option disappears at runtime. Figure 4.22 shows the result of adding an option named Custom to the Background submenu at design time, which simply disappears when its Visible property is set to false .
Figure 4.22:
Here the Custom submenu item has been made invisible on the Background submenu.
| Trap |
If you hide a menu or a menu item that provides access to a submenu, all menu items and submenus underneath it are hidden from view. |
After you hide a menu or menu item, you need to add program statements to your code that make them visible later on. Doing so is straightforward, as shown in the following example:
private: System::Void loadToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { customToolStripMenuItem->Visible = true; }
As Figure 4.23 demonstrates, the Custom menu item is once again visible. In this example, the code that triggers this change was placed in the Load option, so that when the Load menu item is selected, the Visible setting for the Custom item is set to the value of true .
Figure 4.23:
You can also control user access to menu items by hiding and redisplaying them when appropriate.