?? ch10.htm
字號:
<TT>void CMenuView::OnBears()</TT><TT>{</TT><TT> m_szMsg = "Bears are hungry";</TT><TT> InvalidateRect( NULL );</TT></FONT></PRE><P><TT>}</TT> Each of the message-handling functions in Listing 10.6 works in a similarway: A message is stored in the <TT>m_szMsg</TT> member variable, and the view rectangleis invalidated. This causes a <TT>WM_PAINT</TT> message to be sent to the MFC framework,which in turn calls the <TT>OnDraw</TT> function to display the message.</P><P>Build the Menu project. Experiment by right-clicking in the main view window.Selecting any menu item from the shortcut menu will cause a message to be displayed,as shown in Figure 10.4.</P><P><A NAME="04"></A><A HREF="04.htm"><B>Figure 10.4.</B></A> <BR><I>Selecting an item from the context menu.</I><H2><FONT COLOR="#000077"><B>Using Keyboard Accelerators</B></FONT></H2><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B><I>Keyboard accelerators</I>are keyboard shortcuts to message-handling functions.</P><P>A keyboard accelerator provides easy access to commonly accessed program functions.Each keyboard accelerator is a sequence of keystrokes that are translated into aWindows <TT>WM_COMMAND</TT> message, just as if a menu item were selected. This messageis routed to a specific command handler.</P><P>AppWizard creates several keyboard accelerators automatically for your SDI andMDI applications. The following are some of the more common accelerators:<UL> <LI>Ctrl+N is used to call the <TT>ID_FILE_NEW</TT> command handler.<BR> <BR> <LI>Ctrl+C is used to call the <TT>ID_EDIT_COPY</TT> command handler.<BR> <BR> <LI>Ctrl+V is used to call the <TT>ID_EDIT_PASTE</TT> command handler.</UL><BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>There is no requirement that a keyboard accelerator must be mapped to a menu item. However, finding an action that is useful as a keyboard accelerator but not useful as a menu item is extremely rare. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>Displaying Keyboard Accelerator Resources</B></FONT></H3><P>Keyboard accelerators are resources and are displayed and edited much like menuresources. To see the keyboard accelerators for the Menu sample project, open the<TT>IDR_MAINFRAME</TT> Accelerator resource folder in the project workspace. Thekeyboard accelerators used by the project will be displayed as shown in Figure 10.5.</P><P><A NAME="05"></A> <A HREF="05.htm"><B>Figure 10.5.</B> </A><I><BR>Displaying the keyboard accelerators associated with the Menu sample project.</I><BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>An MDI program will have at least two accelerator resources. Only one resource identifier is in use at a time; the current accelerator resource has the same identifier as the current menu resource. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>Adding Keyboard Accelerators</B></FONT></H3><P>To create a new keyboard accelerator, bring up the Accel Properties dialog boxby double-clicking the empty line at the bottom of the accelerator list. The AccelProperties dialog box is shown in Figure 10.6.</P><P><A NAME="06"></A><A HREF="06.htm"><B>Figure 10.6.</B> </A><I><BR>Use the Accel Properties dialog box to add new accelerator resources to your project.</I><BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>You can also bring up the Accel Properties dialog box by pressing the Insert key on your keyboard. <HR></BLOCKQUOTE><P>Each keyboard accelerator has several properties:<UL> <LI><I>ID</I> is the <TT>WM_COMMAND</TT> message that will be sent when the accelerator is invoked. This value is usually a menu item identifier.<BR> <BR> <LI><I>Key</I> is the keyboard key that is used to start the accelerator.<BR> <BR> <LI><I>Modifiers</I> is used to indicate whether one or more of the Shift, Control, or Alt keys is pressed as part of the accelerator combination.<BR> <BR> <LI><I>Type</I> is used to specify whether the Key value is a virtual key code or an ASCII value.</UL><P>The simplest way to fill in the Accel Properties dialog box is to click the buttonlabeled Next Key Typed. After clicking this button, the dialog box will use the nextkeystroke combination to fill in the properties for the accelerator.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>Avoid using the ASCII value type for your keyboard accelerators because they behave unpredictably in the presence of Shift and Caps Lock keys. The virtual keycode is much more reliable and is not affected by your keyboard's Shift and Caps Lock states. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>A Keyboard Accelerator Example</B></FONT></H3><P>To illustrate how accelerators are added to Windows applications, you can adda keyboard accelerator to the Menu sample project. The accelerator will perform thesame action as selecting Bears from the pop-up shortcut menu.</P><P>Open the <TT>IDR_MAINFRAME</TT> accelerator resource folder, and add a new acceleratorresource to the Menu project using the values from Table 10.3.<H4><FONT COLOR="#000077">Table 10.3. The new accelerator resource for the Menu project.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>ID</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Key</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Modifiers</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Type</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>ID_BEARS</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>B</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Ctrl</TD> <TD ALIGN="LEFT" VALIGN="TOP">Virtual</TD> </TR></TABLE></P><P>Build the Menu project. Instead of selecting an item from the shortcut menu, trypressing Ctrl+B on your keyboard; you will get the same message as when you selectBears from the shortcut menu.<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2><P>In this hour, you learned about the use of menus in Windows applications. Youlearned about the routing of menu command messages, as well as methods for modifyingand creating menu resources. As an example, you created an application that displaysa floating pop-up menu when the right mouse button is clicked.<H2><FONT COLOR="#000077"><B>Q&A</B></FONT></H2><DL> <DD><B>Q I added a new item to my menu, but it's gray. I've checked the menu attributes to make sure that the menu should be enabled; why is the menu item still gray?</B><BR> <BR> <B>A</B> Make sure that you have provided a message-handling function for the menu item. The MFC framework will not enable a menu item that doesn't have a message handler.<BR> <BR> <B>Q All the menu items with keyboard accelerators that are provided by MFC and AppWizard place the accelerator label to the far right of the menu window. How can I provide that effect for my controls?</B><BR> <BR> <B>A</B> Use the <TT>\t</TT> tab escape sequence between your menu item and the accelerator text. For example, the caption for the Bears menu item would be</DL><BLOCKQUOTE> <PRE><FONT COLOR="#0066FF"><TT>&Bears\tCtrl+B</TT></FONT></PRE></BLOCKQUOTE><PRE><FONT COLOR="#0066FF"><TT></TT></FONT></PRE><H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2><P>The Workshop is designed to help you anticipate possible questions, review whatyou've learned, and begin thinking ahead to putting your knowledge into practice.The answers to the quiz are found in Appendix B, "Quiz Answers."<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3><DL> <DD>1. What MFC class is used to manage menus?<BR> <BR> 2. What message is handled when providing a shortcut menu?<BR> <BR> 3. What visual cue should be provided for a menu that leads to a dialog box that requires further input from the user?<BR> <BR> 4. What is a mnemonic?<BR> <BR> 5. What is a keyboard accelerator?</DL><H3><FONT COLOR="#000077"><B>Exercises</B></FONT></H3><DL> <DD>1. The File|Enable Hello and File|Check Hello menu items are not updated to reflect the current state of the application. Add update command UI handlers for these menu items so that their captions read File|Disable Hello and File|Uncheck Hello when appropriate.<BR> <BR> 2. Add accelerators for the Lions and Tigers shortcut menu items in the Menu project.</DL><CENTER><P><HR><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> <BR><BR><BR><IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"BORDER="0"></P><P>© <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -