?? 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"><IMGSRC="../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 somesimple 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 floatingcontext 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 awindow.</P><P>To the user, a menu item is a string that indicates a task that can be performedby the application. Each menu item also has an ID that is used to identify the itemwhen routing window messages. This ID is also used when modifying attributes forthe menu item.</P><P>Menus are usually attached to a window, although many applications support floatingpop-up menus that can appear anywhere on the desktop. Later in this hour, you createa 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 differentmenu choices depending on the window that creates the menu.</P><P>In order to make Windows programs easier to use, most programs follow a commonset of guidelines regarding the appearance of their menus. For example, menu itemsleading to dialog boxes that require additional user input are usually marked withan ellipsis (...).</P><P>Another user interface requirement is a mnemonic, or underlined letter for eachmenu item. When this letter is pressed, the appropriate menu item is selected. Thisletter is usually the first letter of the menu item; however, in some cases anotherletter is used. For example, the Exit menu item found in the File menu uses X asits mnemonic. You must be careful not to duplicate letters used for mnemonics; ifyou do, the menu won't work properly.</P><P>Menus are sometimes <I>nested</I>, which means that one menu item is actuallya submenu with a series of additional menu items. A menu item that leads to a nestedsubmenu has a right arrow to indicate that more selections are available. You cansee an example of a nested menu structure in the menus used by the Windows 95 Startbutton, 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 messagesare 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, itis associated with the main frame window, which also contains the application's toolbarand status bar. When a menu item is selected, a <TT>WM_COMMAND</TT> message is sentto the main frame window; this message includes the ID of the menu item. The MFCframework and your application convert this message into a function call, as describedin 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 theMFC framework's message loop. When a menu item is selected, the message is offeredto 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 requiredto 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 yourprogram. The MFC class library provides a <TT>CMenu</TT> class that simplifies handlingmenus 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 resourcecan be edited to add extra menu items for your application, or you can create newmenu 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. Inorder 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 projectworkspace window. Expand the resource tree to show the different resource types definedfor 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. Theyalso have an additional menu item used when a view is active. The name of this menuresource 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 AppWizardnamed <TT>IDR_MAINFRAME</TT>. This is the menu displayed by default for single-documentapplications. Every AppWizard program begins with the same menu; supplying any modificationsthat 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 isdisplayed in the resource editor ready for editing. When the menu is initially loadedinto the editor, only the top-level menu bar is displayed. Clicking any top-levelmenu item displays the pop-up menu associated with that item, as shown in Figure10.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 menuitems to the menu resource. All menu items are initially added to the end of a menuresource and then moved to their proper position. To add a new menu item, followthese 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 draggingit 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 dialogbox:<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 + -