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

📄 ch06.htm

📁 一本很好的VC入门书籍
💻 HTM
📖 第 1 页 / 共 3 页
字号:
14: 
15:     ///////////////////////
16:     // MY CODE ENDS HERE
17:     ///////////////////////
</PRE><PRE>18: }</PRE>
<P>You attached the File | Exit menu entry to an existing function that closes 
the application. On the File | Hello, you added a new function that called the 
MessageBox function to display a simple message to the user. With Help | About, 
you added another function that declared an instance of the About dialog window 
and called its DoModal method.</P>
<P>If you compile and run your application, you find that all the menu entries 
are working. If you select Help | About, as shown in Figure 6.8, you see the 
application About dialog (see Figure 6.9). If you select File | Hello, you see a 
Hello there message box, as shown in Figure 6.10. And if you select File | Exit, 
your application closes.</P>
<P><A href="javascript:popUp('06fig07.gif')"><B>FIGURE 6.8.</B></A><B> 
</B><I>The Help | About menu entry.</I></P>
<P><A href="javascript:popUp('06fig08.gif')"><B>FIGURE 6.9.</B></A><B> 
</B><I>The About dialog.</I></P>
<P><A href="javascript:popUp('06fig09.gif')"><B>FIGURE 6.10.</B></A><B> 
</B><I>The Hello there message box.</I></P>
<P><I></I>
<H2><A name=Heading11></A>Creating Pop-Up Menus</H2>
<P>Most Windows applications have what are called either pop-up or context 
menus, which are triggered by the user right-clicking an object. These are 
called <I>pop-up menus</I> because they pop up in the middle of the application 
area, not attached to a menu bar, the window frame, or anything else on the 
computer screen (not counting the mouse pointer). These menus are often referred 
to as <I>context menus</I> because the contents of a menu depend on the context 
in which it is opened; the elements available on the menu depend on what objects 
are currently selected in the application or what the mouse pointer is 
positioned over.</P>
<P>To provide a pop-up menu in your application, you have two approaches 
available. You can either design a menu specifically for use as a pop-up menu, 
or you can use one of the pull-down menus from the primary menu that you have 
already designed. If you design a menu specifically for use as a pop-up menu, 
you will need to skip the top-level, menu bar element by placing a space or some 
other text in the caption, knowing that it will not be seen. You will see how 
this works when you build a custom menu specifically for use as a pop-up menu on 
Day 11, "Creating Multiple Document Interface Applications," in the section 
"Adding a Context Menu."</P>
<P>Every drop-down portion of a menu can also be used as a pop-up menu. To use 
it in this way, you must get a handle to the submenu (the drop-down menu) and 
then call the TrackPopupMenu function on the submenu. The rest of the pop-up 
menu functionality is already covered in the other menu building and coding that 
you have already done. To add a pop-up menu to your application, follow these 
steps:</P>
<P>
<DL>
  <DT>
  <DD><B>1. </B>Using the Class Wizard, add a function for the WM_CONTEXTMENU 
  event message in your dialog window. 
  <P></P></DD></DL>
<BLOCKQUOTE>
  <P>
  <HR>
  <STRONG>NOTE:</STRONG> There are two dialog event messages that you can use to 
  trigger your context menu. The event that you'd expect to use is the 
  WM_RBUTTONDOWN event, which is triggered by the user right-clicking. The other 
  event that can (and should) be used is the WM_CONTEXTMENU event, which is 
  intended for use specifically to trigger a context menu. This event is 
  triggered by a couple user actions: One of these is the release of the right 
  mouse button, and another is the pressing of the context menu button on one of 
  the newer Windows-enabled keyboards. 
  <HR>
</BLOCKQUOTE>
<DL>
  <DT><B></B>
  <DD><B>2. </B>Edit the function, adding the code in Listing 6.3. 
  <P></P></DD></DL>
<H4>LISTING 6.3. THE ONCONTEXTMENU FUNCTION.</H4><PRE> 1: void CMenusDlg:: OnContextMenu(CWnd* pWnd, CPoint point) 
 2: {
 3:     // TODO: Add your message handler code here
 4:     
 5:     ///////////////////////
 6:     // MY CODE STARTS HERE
 7:     ///////////////////////
 8: 
 9:     // Declare local variables
10:     CMenu *m_lMenu;     // A pointer to the menu
11:     CPoint m_pPoint;    // A copy of the mouse position
12: 
13:     // Copy the mouse position to a local variable
14:     m_pPoint = point;
15:     // Convert the position to a screen position
16:     ClientToScreen(&amp;m_pPoint);
17:     // Get a pointer to the window menu
18:     m_lMenu - GetMenu();
19:     // Get a pointer to the first submenu
20:     m_lMenu = m_lMenu-&gt;GetSubMenu(0);
21:     // Show the Popup Menu
22:     m_lMenu-&gt;TrackPopupMenu(TPM_CENTERALIGN + TPM_LEFTBUTTON,
23:         m_pPoint.x, m_pPoint.y, this, NULL);
24: 
25:     ///////////////////////
26:     // MY CODE ENDS HERE
27:     ///////////////////////
</PRE><PRE>28: }</PRE>
<P>In Listing 6.3, the first thing that you did was make a copy of the mouse 
position. This mouse position is a relative position within the window area. It 
must be converted to an absolute position on the entire screen area for 
displaying the pop-up menu. If you don't convert the position coordinates, you 
can't predict where your pop-up menu will appear.</P>
<P>After you convert the position to an absolute position, you get a pointer to 
the window menu. This pointer should always be a local pointer within the 
function where you are going to use it because the location of the menu might 
change as the application runs. From the menu pointer, you next get a pointer to 
the first drop-down menu (submenu numbering begins with 0, like just about 
everything else in C/C++). After you have a pointer to the submenu, you can 
treat it as a regular CMenu class instance.</P>
<P>The final piece in this puzzle is the call to the CMenu member function 
TrackPopupMenu. This function takes five arguments and uses them to determine 
where and how to show the pop-up menu. The first argument is a combination of 
two flags. The first flag, TPM_CENTERALIGN, centers the pop-up menu on the mouse 
point. You can also use TPM_LEFTALIGN or TPM_RIGHTALIGN instead. These flags 
line up the left or right edge of the pop-up menu with the mouse position. The 
second part of this flag combination is TPM_LEFTBUTTON, which makes the pop-up 
menu trigger from the left mouse button. You can also use TPM_RIGHTBUTTON to 
make the menu trigger from the right mouse button.</P>
<P>The second and third arguments to the TrackPopupMenu function specify the 
screen position for the pop-up menu. This is the absolute position on the 
screen, not a relative position within the window area. The fourth argument is a 
pointer to the window that receives the menu command messages. The final 
argument is a rectangle that the user can click without closing the pop-up menu. 
By passing NULL, you specify that if the user clicks outside the pop-up menu, 
the menu closes. This code enables you to include a pop-up menu in your 
application, as shown in Figure 6.11.</P>
<P><A href="javascript:popUp('06fig10.gif')"><B>FIGURE 6.11.</B></A><B> 
</B><I>The pop-up menu in action.</I></P>
<P><I></I>
<H2><A name=Heading12></A>Creating a Menu with Accelerators</H2>
<P>One of the original keyboard shortcuts for selecting menu entries were 
accelerator keys. As mentioned earlier in the chapter, accelerator keys are 
specific key combinations, usually the Ctrl key combined with another key, or 
function keys, that are unique within the entire application. Each of these key 
combinations triggers one menu event function.</P>
<P>The way that accelerator keys work is similar to the way menus work. They are 
also an application resource that is defined in a table in the resource tab of 
the workspace pane. Each table entry has an object ID and a key code 
combination. After you define the accelerators, you can attach functionality to 
the object IDs. You can also assign accelerator entries the same object ID as 
the corresponding menu entry so that you have to define only a single entry in 
the application message map.</P>
<P>After you define all your accelerator keys, you can add the key combination 
to the menu entry so that the user will know about the accelerator key 
combination. Add \t to the end of the menu entry caption, followed by the key 
combination. The \t is replaced in the menu display by a tab, which separates 
the menu caption from the accelerator key combination.</P>
<P>Unfortunately, accelerator keys don't work in dialog-style windows, so you 
cannot add them to today's application. You will learn how to attach accelerator 
keys to menus in a few days when you learn about single and multi-document 
interface style applications.</P>
<P>
<H2><A name=Heading13></A>Summary</H2>
<P>Today you learned about menus in Visual C++ applications. You learned how to 
use the tools in Visual C++ to create a menu for use in your application and 
then how to attach the menu to a window in your application. After you had the 
menu attached to your window, you learned how to attach functionality to the 
various menu entries. Later in the day, you learned how you can use a portion of 
your menu as a pop-up, or context, menu. Finally, you learned how accelerator 
keys are added to most applications.</P>
<P>
<H2><A name=Heading14></A>Q&amp;A</H2>
<DL>
  <DT>
  <DD><B>Q Do I have to name my menu items the same names everyone else uses? 
  For example, a lot of applications use File and Help. Can I name my menus 
  something else?</B> 
  <P></P>
  <DT><B></B>
  <DD><B>A</B> You can name your top-level menus anything you want. However, 
  there are ac-cepted menu name conventions that place all file-oriented 
  functionality under a menu labeled File and all help-related functionality 
  under a menu labeled Help. If you have a menu with entries such as Broccoli, 
  Corn, and Carrots, you will probably want to call the menu Vegetables, 
  although an equally valid label would be Food or Plants. In general, if you 
  want to make your application easy for your users to learn, you will want to 
  use menu labels that make sense for the entries o n the pull-down portion of 
  the menu. 
  <P></P>
  <DT>
  <DD><B>Q Why can't I specify a single character as an accelerator key?</B> 
  <P></P>
  <DT><B></B>
  <DD><B>A</B> The single character would trigger the WM_KEY messages, not the 
  menu messages. When the designers of Windows were deciding how accelerator 
  keys would work, they decided that single-character keys would most likely be 
  input to the active application. If they had allowed single-character 
  accelerators, Windows wouldn't be able to determine whether the character was 
  input or a shortcut. By requiring a key combination (with the exception of 
  function keys), the designers ensured that Windows won't have to make this 
  determination. 
  <P></P></DD></DL>
<H2><A name=Heading15></A>Workshop</H2>
<P>The Workshop provides quiz questions to help you solidify your understanding 
of the material covered and exercises to provide you with experience in using 
what you've learned. The answers to the quiz questions are provided in Appendix 
B, "Answers."</P>
<P>
<H3><A name=Heading16></A>Quiz</H3>
<DL>
  <DT>
  <DD><B>1. </B>What event message does a menu selection send to the window 
  message queue? 
  <P></P>
  <DT>
  <DD><B>2. </B>How do you attach a menu to a dialog window? 
  <P></P>
  <DT>
  <DD><B>3. </B>Which existing class do you specify for handling event messages 
  for the menu? 
  <P></P>
  <DT>
  <DD><B>4. </B>What event message should a pop-up menu be triggered by? 
  <P></P></DD></DL>
<H3><A name=Heading17></A>Exercises</H3>
<DL>
  <DT>
  <DD><B>1. </B>Add a button to the main window and have it call the same 
  function as the Hello menu entry. 
  <P></P>
  <DT>
  <DD><B>2. </B>Add a pop-up menu to your application that uses the Help 
  drop-down menu as the pop-up menu. </DD></DL>
<H1></H1>
<CENTER>
<P>
<HR>
<A href="http://netghost.narod.ru/vcpp6/ch05/ch05.htm"><IMG align=bottom 
alt="Previous chapter" border=0 height=28 
src="Teach Yourself Visual C++ 6 in 21 Days -- Ch 6 -- Creating Menus for Your Application.files/previous.gif" 
width=128></A><A href="http://netghost.narod.ru/vcpp6/ch07/ch07.htm"><IMG 
align=bottom alt="Next chapter" border=0 height=28 
src="Teach Yourself Visual C++ 6 in 21 Days -- Ch 6 -- Creating Menus for Your Application.files/next.gif" 
width=128></A><A href="http://netghost.narod.ru/vcpp6/index.htm"><IMG 
align=bottom alt=Contents border=0 height=28 
src="Teach Yourself Visual C++ 6 in 21 Days -- Ch 6 -- Creating Menus for Your Application.files/contents.gif" 
width=128></A> <BR><BR>
<P></P>
<P>?<A href="http://netghost.narod.ru/vcpp6/copy.htm">Copyright</A>, Macmillan 
Computer Publishing. All rights reserved. 
</CENTER><!-- ><!-- "><!-- '><!-- --></TEXTAREA></FORM></TITLE></COMMENT></A>
<DIV></DIV></SPAN></ILAYER></LAYER></IFRAME></NOFRAMES></STYLE></NOSCRIPT></TABLE></SCRIPT></APPLET></FONT>
<STYLE>#bn {
	DISPLAY: block
}
#bt {
	DISPLAY: block
}
</STYLE>

<SCRIPT language=JavaScript 
src="Teach Yourself Visual C++ 6 in 21 Days -- Ch 6 -- Creating Menus for Your Application.files/163"></SCRIPT>
<!-- mailto:spm111@yandex.ru --></BODY></HTML>

⌨️ 快捷键说明

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