📄 ch06.htm
字号:
specify the caption as E&xit.
<P></P>
<DT>
<DD><B>5. </B>Add a function to the button using the Class Wizard. Change the
code in this function to call OnOK. Remember, the OnOK function causes the
application to close.
<P></P></DD></DL>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> If you don't remember how to add the OnOK function,
review the section "Closing the Application" on Day 2, "Using Controls in Your
Application," for an example.
<HR>
</BLOCKQUOTE>
<H3><A name=Heading8></A>Adding and Customizing a Menu</H3>
<P>Now that you have the basic application built, it's time to start creating a
menu for the application. To create a menu, you will first add a menu resource
to your project. When you add the resource, Visual C++ automatically invokes the
Menu Designer, which allows you to customize the menu. The following steps show
you how to add and customize a menu:</P>
<P>
<DL>
<DT>
<DD><B>1. </B>Select the Resource View tab in the workspace pane.
<P></P>
<DT>
<DD><B>2. </B>Select the project resources folder at the top of the tree; in
your example, this is Menus.
<P></P>
<DT>
<DD><B>3. </B>Right-click the mouse to bring up a pop-up menu.
<P></P>
<DT>
<DD><B>4. </B>Select Insert from the pop-up menu.
<P></P>
<DT>
<DD><B>5. </B>In the Insert Resource dialog that opens, select Menu from the
list of available resources, as in Figure 6.1. Click the New button.
<P></P></DD></DL>
<P><A href="javascript:popUp('06fig01.gif')"><B>FIGURE 6.1.</B></A><B>
</B><I>The Insert Resource dialog.</I></P>
<P><I></I>
<DL>
<DT><I></I>
<DD><B>6. </B>The Menu Designer opens in the editing area of Developer Studio.
The first menu spot is highlighted, as shown in Figure 6.2.
<P></P></DD></DL>
<P><A href="javascript:popUp('06fig02a.gif')"><B>FIGURE 6.2.</B></A><B>
</B><I>An empty menu.</I></P>
<P>At this point, you have created the menu resource and you are ready to
customize it by adding menu items. To add a menu item, follow these steps:</P>
<P>
<DL>
<DT>
<DD><B>1. </B>Right-click the mouse on the highlighted area and select
Properties from the pop-up menu.
<P></P>
<DT>
<DD><B>2. </B>Enter the menu item's Caption. For this example, enter
<B>&File</B> and close the Properties dialog.
<P></P></DD></DL>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> You are in the menu Properties dialog to specify the
text that the user will see on the menu bar while the application is running.
Because the Pop-up check box is checked (by default on any menu items on the
top-level menu bar), this menu element doesn't trigger any application
functionality and thus doesn't need to have an object ID assigned to it.
<HR>
</BLOCKQUOTE>
<DL>
<DT><B></B>
<DD><B>3. </B>The first drop-down menu location is highlighted. To add this
menu item, right-click the mouse again on the highlighted area and select
Properties from the pop-up menu.
<P></P>
<DT>
<DD><B>4. </B>Enter an ID and caption for the menu item. For this example,
enter <B>IDM_FILE_HELLO</B> for the ID and <B>&Hello</B> for the Caption.
Close the dialog.
<P></P></DD></DL>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> This time in the menu Properties dialog, you not only
specify the text that the user will see when the menu is opened from the menu
bar, but you also specify the object ID that will be used in the event message
handler to determine what function receives each of the menu events.
<HR>
</BLOCKQUOTE>
<P>At this point you have created a menu with a single menu item. You can
continue to add menu items by repeating steps 3 and 4 of the preceding list for
each of the highlighted areas. You can also add separators onto the menu. A
<I>separator</I> is a dividing line that runs across the menu to separate two
functional areas of menu selections. To add a separator, perform the following
steps:</P>
<P><A href="javascript:popUp('06fig02.gif')"><B>FIGURE 6.3.</B></A><B>
</B><I>Specifying a menu separator.</I></P>
<P><I></I>
<DL>
<DT><I></I>
<DD><B>1. </B>Select the highlighted area where you want the separator to be
placed. In the example you created, the second drop-down menu location should
be highlighted. Open the properties dialog as you did in step 3 in the
preceding list. To add a separator, simply select the Separator option, as
shown in Figure 6.3, and close the dialog.
<P></P></DD></DL>
<P>To complete your sample program, follow the same steps I just described to
add an Exit item to your File menu and a second menu called Help with one menu
item called About. The following steps, which resemble the preceding list of
steps, walk you through adding these additional items:</P>
<P>
<DL>
<DT>
<DD><B>1. </B>Open the properties dialog for the third drop-down location and
specify the ID as IDM_FILE_EXIT and the caption as E&xit. Close the
dialog.
<P></P>
<DT>
<DD><B>2. </B>Select the second top-level menu location and open the
properties dialog. Specify the caption as &Help and close the dialog.
<P></P>
<DT>
<DD><B>3. </B>Open the properties dialog for the first drop-down location on
the second top-level menu. Specify the ID as ID_HELP_ABOUT and the caption as
&About. Close the dialog.
<P></P></DD></DL>
<P>At this point, your menu is created; however, it is not attached to your
application.</P>
<P>
<H3><A name=Heading9></A>Attaching the Menu to Your Dialog Window</H3>
<P>You now have a menu that you can use in your application. If you compile and
run your application at this point, however, the menu doesn't appear. You still
need to attach the menu to your dialog window. You can attach a menu by
following these steps:</P>
<P>
<DL>
<DT>
<DD><B>1. </B>Open the dialog painter by double-clicking the primary
application dialog in the Dialog folder in the Workspace pane. For this
example, double-click on IDD_MENUS_DIALOG.
<P></P>
<DT>
<DD><B>2. </B>Select the entire dialog window, making sure that no controls
are selected, and open the dialog's properties dialog. (What you are doing is
opening the properties for the dialog window itself, not for any of the
controls that might be on the window.)
<P></P>
<DT>
<DD><B>3. </B>Select the menu you have designed from the Menu drop-down list
box, as shown in Figure 6.4.
<P></P></DD></DL>
<P><A href="javascript:popUp('06fig03.gif')"><B>FIGURE 6.4.</B></A><B>
</B><I>Attaching the menu to the dialog window.</I></P>
<P>If you compile and run your application, you find that the menu is attached
to the application dialog, as shown in Figure 6.5. You can select menu entries
as you do with any other Windows application--with one small difference. At this
point, when you select one of the menu entries, nothing happens. You still need
to attach functionality to your menu.</P>
<P><A href="javascript:popUp('06fig04.gif')"><B>FIGURE 6.5.</B></A><B>
</B><I>The menu is now part of the application dialog.</I></P>
<P><I></I>
<H3><A name=Heading10></A>Attaching Functionality to Menu Entries</H3>
<P>Now that you have a menu as part of your application, it sure would be nice
if it actually did something. Well, before your menu can do anything, you have
to tell it what to do, just like everything else in your Visual C++
applications. To attach some functionality to your menu, follow these steps:</P>
<P><A href="javascript:popUp('06fig05.gif')"><B>FIGURE 6.6.</B></A><B>
</B><I>The menu is now part of the application.</I></P>
<P><I></I>
<DL>
<DT><I></I>
<DD><B>1. </B>Open the Menu Designer to your menu.
<P></P>
<DT>
<DD><B>2. </B>Open the Class Wizard from the View menu.
<P></P>
<DT>
<DD><B>3. </B>The Adding a Class dialog is displayed for you, just as it was
yesterday when you added a second dialog. Leave the dialog selection on Select
an Existing Class and click OK (see Figure 6.6).
<P></P>
<DT>
<DD>Yesterday, when you were adding a second dialog window to your
application, you needed to create a new C++ class for that window. For today's
menu, you want to attach it to the existing C++ class for the dialog window to
which the menu is attached.
<P></P>
<DT>
<DD><B>4. </B>Choose the C++ class of the primary dialog window from the list
of available classes in the Select Class dialog. For this example, select
CMenusDlg, as shown in Figure 6.7. This tells Visual C++ that all the
functionality that you will call from the various menu entries is part of the
same dialog class of the window that it's attached to.
<P></P></DD></DL>
<P><A href="javascript:popUp('06fig06.gif')"><B>FIGURE 6.7.</B></A><B>
</B><I>The Select Class dialog.</I></P>
<P>For the menu elements that you want to use to trigger new functions in your
application, you can add event-handler functions through the Class Wizard, just
as you can with controls that you place on the dialog window.</P>
<P>For this example, add a function for the IDM_FILE_HELLO object (the Hello
menu) on the COMMAND event message. Name the function OnHello and add the code
in Listing 6.1 to the function.</P>
<P>
<H4>LISTING 6.1. THE ONHELLO FUNCTION.</H4><PRE> 1: void CMenusDlg::OnHello()
2: {
3: // TODO: Add your command handler code here
4:
5: ///////////////////////
6: // MY CODE STARTS HERE
7: ///////////////////////
8:
9: // Display a message for the user
10: MessageBox("Hello there", "Hello");
11:
12: ///////////////////////
13: // MY CODE ENDS HERE
14: ///////////////////////
</PRE><PRE>15: }</PRE>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> The COMMAND event message is the message that is passed
to the application window when a menu entry is selected. Placing a function on
this event message has the same effect as placing a function on the menu entry
selection.
<HR>
</BLOCKQUOTE>
<P>You can call existing event handlers from menu elements by adding the
existing function to the menu COMMAND event. You can do this by adding a
function to the menu object ID and then specifying the existing function name
instead of accepting the suggested function name.</P>
<P>To reuse the OnExit function for the Exit menu element, reopen the Menu
Designer and then reopen the Class Wizard. When the Class Wizard is displayed,
add a function for the IDM_FILE_EXIT object on the COMMAND event message. Do not
accept the default function name presented to you by the Class Wizard. Enter the
function name <B>OnExit</B>. This automatically attaches the existing OnExit
function that you created with your Exit button earlier.</P>
<P>To round out your example's functionality, add a function to the
ID_HELP_ABOUT object on the COMMAND event message. Edit the function as in
Listing 6.2.</P>
<P>
<H4>LISTING 6.2. THE ONHELPABOUT FUNCTION.</H4><PRE> 1: void CMenusDlg::OnHelpAbout()
2: {
3: // TODO: Add your command handler code here
4:
5: ///////////////////////
6: // MY CODE STARTS HERE
7: ///////////////////////
8:
9: // Declare an instance of the About window
10: CAboutDlg dlgAbout;
11:
12: // Show the About window
13: dlgAbout.DoModal();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -