⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch10.htm

📁 Visual C++ 的学习资料 Visual C++ 的学习资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<TT>void CMenuView::OnBears()</TT>
<TT>{</TT>
<TT>    m_szMsg = &quot;Bears are hungry&quot;;</TT>
<TT>    InvalidateRect( NULL );</TT>
</FONT></PRE>
<P><TT>}</TT> Each of the message-handling functions in Listing 10.6 works in a similar
way: A message is stored in the <TT>m_szMsg</TT> member variable, and the view rectangle
is 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 a
Windows <TT>WM_COMMAND</TT> message, just as if a menu item were selected. This message
is routed to a specific command handler.</P>
<P>AppWizard creates several keyboard accelerators automatically for your SDI and
MDI 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 menu
resources. To see the keyboard accelerators for the Menu sample project, open the
<TT>IDR_MAINFRAME</TT> Accelerator resource folder in the project workspace. The
keyboard 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 box
by double-clicking the empty line at the bottom of the accelerator list. The Accel
Properties 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 button
labeled Next Key Typed. After clicking this button, the dialog box will use the next
keystroke 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 add
a keyboard accelerator to the Menu sample project. The accelerator will perform the
same 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 accelerator
resource 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, try
pressing Ctrl+B on your keyboard; you will get the same message as when you select
Bears 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. You
learned about the routing of menu command messages, as well as methods for modifying
and creating menu resources. As an example, you created an application that displays
a floating pop-up menu when the right mouse button is clicked.
<H2><FONT COLOR="#000077"><B>Q&amp;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>&amp;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 what
you've learned, and begin thinking ahead to putting your knowledge into practice.
The answers to the quiz are found in Appendix B, &quot;Quiz Answers.&quot;
<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"><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> <BR>
<BR>
<BR>
<IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"
BORDER="0"></P>

<P>&#169; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>


</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -