📄 ch10.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Teach Yourself Visual C++® 5 in 24 Hours -- Hour 10 -- Menus</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
<FONT COLOR="#000077">Teach Yourself Visual C++® 5 in 24 Hours</FONT></H1>
</CENTER>
<CENTER>
<P><A HREF="../ch09/ch09.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch11/ch11.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<CENTER>
<H1><FONT COLOR="#000077">- Hour 10 -<BR>
Menus</FONT></H1>
</CENTER>
<P>Menus are an essential part of most Windows programs. With the exception of some
simple dialog box-based applications, all Windows programs offer some type of menu.</P>
<P>In this hour, you will learn
<UL>
<LI>How menus are used in Windows programs<BR>
<BR>
<LI>How the MFC <TT>CMenu</TT> class is used to support menus<BR>
<BR>
<LI>How to add accelerators to your menu items
</UL>
<P>In this hour you will also modify a menu created by AppWizard and create a floating
context menu.
<H2><FONT COLOR="#000077"><B>What Is a Menu?</B></FONT></H2>
<P>A <I>menu</I> is a list of command messages that can be selected and sent to a
window.</P>
<P>To the user, a menu item is a string that indicates a task that can be performed
by the application. Each menu item also has an ID that is used to identify the item
when routing window messages. This ID is also used when modifying attributes for
the menu item.</P>
<P>Menus are usually attached to a window, although many applications support floating
pop-up menus that can appear anywhere on the desktop. Later in this hour, you create
a floating pop-up menu that is displayed when the right mouse button is pressed.
These menus are often used to provide context-sensitive help and offer different
menu choices depending on the window that creates the menu.</P>
<P>In order to make Windows programs easier to use, most programs follow a common
set of guidelines regarding the appearance of their menus. For example, menu items
leading to dialog boxes that require additional user input are usually marked with
an ellipsis (...).</P>
<P>Another user interface requirement is a mnemonic, or underlined letter for each
menu item. When this letter is pressed, the appropriate menu item is selected. This
letter is usually the first letter of the menu item; however, in some cases another
letter is used. For example, the Exit menu item found in the File menu uses X as
its mnemonic. You must be careful not to duplicate letters used for mnemonics; if
you do, the menu won't work properly.</P>
<P>Menus are sometimes <I>nested</I>, which means that one menu item is actually
a submenu with a series of additional menu items. A menu item that leads to a nested
submenu has a right arrow to indicate that more selections are available. You can
see an example of a nested menu structure in the menus used by the Windows 95 Start
button, as shown in Figure 10.1.</P>
<P><A NAME="01"></A><A HREF="01.htm"><B>Figure 10.1.</B></A> <BR>
<I>The nested menu structure used by the Start button.</I>
<H3><FONT COLOR="#000077"><B>Command Message Routing</B></FONT></H3>
<P>Before you learn about creating and modifying menus, look at how menu messages
are handled by Windows programs in general and MFC programs in particular.</P>
<P>A menu is always associated with a particular window. In most MFC programs, it
is associated with the main frame window, which also contains the application's toolbar
and status bar. When a menu item is selected, a <TT>WM_COMMAND</TT> message is sent
to the main frame window; this message includes the ID of the menu item. The MFC
framework and your application convert this message into a function call, as described
in Hour 8, "Messages and Event-Driven Programming."</P>
<P>In an MFC application, many windows can receive a menu selection message. In general,
any window that is derived from the <TT>CCmdTarget</TT> class is plugged into the
MFC framework's message loop. When a menu item is selected, the message is offered
to all the command target objects in your application, in the following order:
<DL>
<DD>1. The <TT>CMainFrame</TT> object<BR>
<BR>
2. The main MDI frame window<BR>
<BR>
3. The active child frame of an MDI frame window<BR>
<BR>
4. The view that is associated with the MDI child frame<BR>
<BR>
5. The document object associated with the active view<BR>
<BR>
6. The document template associated with the document object<BR>
<BR>
7. The <TT>CWinApp</TT> object
</DL>
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>This list might
seem like a large number of steps to take, but it's actually not very complicated
in practice. Usually, a menu item is handled by one type of object: a view or main
frame. Menu messages are rarely handled directly by the document template or child
frame objects.
<HR>
</BLOCKQUOTE>
<P>Figure 10.2 shows a simplified map of how commands are routed in an MFC application.</P>
<P>In most cases, you can use ClassWizard to configure the message maps required
to route menu selection messages to their proper destinations.
<H3><FONT COLOR="#000077"><B>MFC Support for Menus</B></FONT></H3>
<P>You can create menus dynamically or as static resources that are added to your
program. The MFC class library provides a <TT>CMenu</TT> class that simplifies handling
menus and is used for the examples in this hour.</P>
<P><A NAME="02"></A><A HREF="02.htm"><B>Figure 10.2.</B></A> <I><BR>
Menu command routing in an MFC application.</I></P>
<P>AppWizard generates a menu resource for programs that it creates. This menu resource
can be edited to add extra menu items for your application, or you can create new
menu resources for your application.</P>
<P>For the examples used in this hour, create a sample SDI application called Menu.
This program is used to demonstrate how menu resources are created and modified.
<H2><FONT COLOR="#000077"><B>Adding New Menu Items</B></FONT></H2>
<P>One of the easiest tasks to perform with a menu is adding a new menu item. In
order to use a new menu item, you must do two things:
<UL>
<LI>Modify the menu resource to include the new menu item.
<LI>Add a message-handling function using ClassWizard.
</UL>
<P>These steps are explained in the next two sections.
<H3><FONT COLOR="#000077"><B>Opening the Menu Resource</B></FONT></H3>
<P>To display the current menu resources, select the ResourceView tab in the project
workspace window. Expand the resource tree to show the different resource types defined
for the current project; one of the folders is labeled Menu.</P>
<P>Open the Menu folder to display the resources defined for the project's menus.
Every multiple-document application created by AppWizard has two menu resources.
MDI applications use an <TT>IDR_MAINFRAME</TT> menu when no views are active. They
also have an additional menu item used when a view is active. The name of this menu
resource is based on the application name, such as <TT>IDR_<I>xxx</I>TYPE</TT>, where
<I><TT>xxx</TT></I> is replaced by the program's name. For example, <TT>IDR_FOOTYPE</TT>
is the second menu resource created for a program named Foo.</P>
<P>SDI applications, such as the Menu example, have a single menu created by AppWizard
named <TT>IDR_MAINFRAME</TT>. This is the menu displayed by default for single-document
applications. Every AppWizard program begins with the same menu; supplying any modifications
that are required for your application is up to you.
<H3><FONT COLOR="#000077"><B>Editing the Menu Resource</B></FONT></H3>
<P>Open the menu resource by double-clicking the menu resource icon. The menu is
displayed in the resource editor ready for editing. When the menu is initially loaded
into the editor, only the top-level menu bar is displayed. Clicking any top-level
menu item displays the pop-up menu associated with that item, as shown in Figure
10.3.</P>
<P><A NAME="03"></A><A HREF="03.htm"><B>Figure 10.3.</B></A> <I><BR>
Using the Developer Studio resource editor to edit a menu resource.</I></P>
<P>The last item of every menu is an empty box. This box is used to add new menu
items to the menu resource. All menu items are initially added to the end of a menu
resource and then moved to their proper position. To add a new menu item, follow
these steps:
<DL>
<DD>1. Double-click the empty box on the File menu to display the Menu Properties
dialog box.<BR>
<BR>
2. To add a menu item, provide a menu ID and caption for the new menu item. By convention,
menu IDs begin with <TT>ID_</TT> and then you include the name of the top-level menu.
For this example, enter <TT>ID_FILE_HELLO</TT> as the menu ID and &Hello as the
menu caption.<BR>
<BR>
3. Optionally, you can provide a prompt that is displayed in the status bar when
the new menu item is highlighted.<BR>
<BR>
4. Click anywhere outside the Properties dialog box to return to the editor.
</DL>
<P>After you've added the new menu item you can move it to a new position by dragging
it with the mouse. Changing the menu position does not change any of its attributes.
<H3><FONT COLOR="#000077"><B>Menu Item Properties</B></FONT></H3>
<P>Several optional properties can be applied to a menu item via the Properties dialog
box:
<UL>
<LI><I>ID</I> is used for the menu's resource ID. This ID is sent as part of the
<TT>WM_COMMAND</TT> message to your application and is used by ClassWizard to identify
the menu item.<BR>
<BR>
<LI><I>Caption</I> is the name used to identify the menu item. The mnemonic letter
is preceded by an ampersand (<TT>&</TT>) and is used to select the item without
using the mouse.<BR>
<BR>
<LI><I>Separator</I> is used to indicate that this menu item is a separator, or horizontal
line that divides logical sections of a menu. This check box is usually cleared.<BR>
<BR>
<LI><I>Checked</I> is used to indicate that the menu item should display a check
mark to indicate the menu item is selected. This check box is usually cleared.<BR>
<BR>
<LI><I>Pop-up</I> is used to indicate that this menu item is the top level of a pop-up
or submenu. This option is usually cleared except on the top-level menu bar.<BR>
<BR>
<LI><I>Grayed</I> indicates that this menu item is grayed. This check box is usually
cleared.<BR>
<BR>
<LI><I>Inactive</I> indicates that this menu item is inactive. This check box is
usually cleared.<BR>
<BR>
<LI><I>Help</I> places the menu item to the far right side of the menu bar. This
option is rarely used, even for the Help menu.<BR>
<BR>
<LI><I>Break</I> is used to split the menu at this menu item. The default choice
is none and is used in almost all cases.<BR>
<BR>
<LI><I>Prompt</I> is used to specify the text that will be displayed in the status
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -