📄 ch06.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0044)http://netghost.narod.ru/vcpp6/ch06/ch06.htm -->
<HTML><HEAD><TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Ch 6 -- Creating Menus for Your Application</TITLE>
<META content="text/html; charset=windows-1251" http-equiv=Content-Type>
<SCRIPT language=JavaScript>
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '</head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<LINK href="/includes/stylesheets/ebooks.css" rel=stylesheet>
<META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff text=#000000>
<H1 align=center><IMG align=bottom border=0 height=66
src="Teach Yourself Visual C++ 6 in 21 Days -- Ch 6 -- Creating Menus for Your Application.files/sams.gif"
width=171><BR>Teach Yourself Visual C++ 6 in 21 Days</H1>
<CENTER>
<P><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>
<HR>
</CENTER>
<H1 align=center>- 6 -<BR>Creating Menus for Your Application</H1>
<H1></H1>
<UL>
<LI><A href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading1">Menus</A>
<UL>
<LI><A href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading2">Menu
Styles</A>
<LI><A href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading3">Keyboard
Shortcut-Enabling Menus</A>
<LI><A href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading4">Menu
Standards and Conventions</A> </LI></UL>
<LI><A href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading5">Designing
Menus</A>
<LI><A href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading6">Creating a
Menu</A>
<UL>
<LI><A href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading7">Creating
the Application</A>
<LI><A href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading8">Adding
and Customizing a Menu</A>
<LI><A
href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading9">Attaching the
Menu to Your Dialog Window</A>
<LI><A
href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading10">Attaching
Functionality to Menu Entries</A> </LI></UL>
<LI><A href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading11">Creating
Pop-Up Menus</A>
<LI><A href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading12">Creating
a Menu with Accelerators</A>
<LI><A
href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading13">Summary</A>
<LI><A
href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading14">Q&A</A>
<LI><A
href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading15">Workshop</A>
<UL>
<LI><A
href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading16">Quiz</A>
<LI><A
href="http://netghost.narod.ru/vcpp6/ch06/ch06.htm#Heading17">Exercises</A>
</LI></UL></LI></UL>
<P>
<HR SIZE=4>
<BR>Most Windows applications use pull-down menus to provide the user a number
of functions without having to provide buttons on the window. This enables you
to provide your users a large amount of functionality while preserving most of
your valuable screen real estate for other stuff.
<P></P>
<P>Today you will learn</P>
<P>
<UL>
<LI>How to create menus for your Visual C++ application
<P></P>
<LI>How to attach a menu to your application's main dialog window
<P></P>
<LI>How to call application functions from a menu
<P></P>
<LI>How to create a pop-up menu that can be triggered with the right mouse
button
<P></P>
<LI>How to set up accelerator keys for keyboard shortcuts to menus </LI></UL>
<H2><A name=Heading1></A>Menus</H2>
<P>Back when the first computer terminals were introduced and users began using
computer software, even on large mainframe systems software developers found the
need to provide the users with some sort of menu of the functions that the
computer could perform. These early menus were crude by today's standards and
were difficult to use and navigate. Menus have progressed since then; they've
become standardized in how they are used and easy to learn.</P>
<P>The software designers that first came up with the idea of a graphical user
interface (GUI) planned to make computer systems and applications easier to
learn by making everything behave in a consistent manner. Menus used for
selecting application functionality were one part of the GUI design that could
be more easily learned if they all worked the same. As a result, a number of
standard menu styles were developed.</P>
<P>
<H3><A name=Heading2></A>Menu Styles</H3>
<P>The first menu styles that were standardized are the pull-down and cascading
menus. These are the menus with the categories all listed in a row across the
top of the application window. If you select one of the categories, a menu drops
down below the category, with a number of menu entries that can be selected to
trigger various functions in the application.</P>
<P>A variation on this menu style is the <I>cascading menu</I>, which has
another submenu that opens to the right of a menu entry. This submenu is similar
to the pull-down menu, with a number of entries that trigger application
functions. The menu designers placed no limit on how many cascading menus can be
strung together, but it quickly became clear to most developers that more than
two cascading levels is a little unwieldy.</P>
<P>Eventually, a third style of menu was developed, called a <I>pop-up</I> or
<I>context menu</I>--a menu that pops up in the middle of the application area,
floating freely above the application work area. This is also called a context
menu because the specific menu that pops up is dependent on the selected object
or workspace area where the cursor or mouse pointer is.</P>
<P>
<H3><A name=Heading3></A>Keyboard Shortcut-Enabling Menus</H3>
<P>When users began working with keyboard-intensive applications, such as word
processors, it was discovered that taking your hands off the keyboard to use the
mouse to make menu selections dramatically reduced productivity. Software
designers decided that they needed to add keyboard shortcuts for the various
menu entries (especially the most frequently used menu options). For this
reason, keyboard shortcuts (accelerators) and hotkeys were added.</P>
<P><I>Hotkeys</I> are letters that are underlined in each menu entry. If you
press the Alt key with the underlined letter, you can select the menu entry that
contains the underlined letter. This is a means of navigating application menus
without taking your hands off the keyboard.</P>
<P>For more advanced users, application designers added <I>keyboard
shortcuts</I>, or <I>accelerators</I>. An accelerator is a single key
combination that you can press to trigger an application function instead of
having to navigate through the application menus. This allows advanced users to
avoid the overhead of using menus for the most common application functions. To
enable users to learn what accelerators are available in an application, the key
combination is placed on the menu entry that it can be used to replace,
positioned at the right edge of the menu window.</P>
<P>
<H3><A name=Heading4></A>Menu Standards and Conventions</H3>
<P>Although there are no standards in how menus are designed, there are a number
of conventions for how they are designed and organized. All these conventions
are available in <I>Windows Interface Guidelines for Software Design</I>,
published by Microsoft for use by Windows software developers. The purpose of
this publication is to facilitate the development of consistent application
behaviors, which will help accomplish one of the primary goals behind the
development of GUI systems. The conventions are as follows:</P>
<P>
<UL>
<LI>Use single-word menu categories across the top menu bar. A two-word
category can easily be mistaken for two one-word categories.
<P></P>
<LI>The File menu is located as the first menu on the left. It contains all
file-oriented functions (such as New, Open, Save, Print, and so on), as well
as the Exit function. The Exit option is located at the bottom of the menu,
separated from the rest of the menu entries by a border.
<P></P>
<LI>The Edit menu is next to the File menu. The Edit menu contains all editing
functions such as Copy, Cut, Paste, Undo, Redo, and so on.
<P></P>
<LI>The View menu contains menu entries that control and affect the appearance
of the application work area.
<P></P>
<LI>The Window menu is used in Multiple Document Interface (MDI) style
applications. This has functions for controlling the child windows, selecting
the current window, and altering the layout. This menu is the next-to-last
menu from the right end of the menu bar.
<P></P>
<LI>The Help menu is the final menu on the right end of the menu bar. It
contains menu entries that provide instruction or documentation on the
application. If the application has any copyrighted or corporate information
that needs to be available for viewing, this should be located as the final
entry on this menu, labeled About<I> <application name></I>. </LI></UL>
<H2><A name=Heading5></A>Designing Menus</H2>
<P>Menus are defined as a resource in Visual C++ applications. Because they are
a resource, you can design menus in the Visual C++ editor through the Resource
View tab on the workspace pane. When you first create a dialog-style
application, there won't be a menu folder in the resource tree, but you can
change that.</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> Various aspects of Windows applications are considered
to be resources, including window layouts, menus, toolbars, images, text
strings, accelerators, and so on. All these features are organized in what is
known as a <I>resource file</I>, which is used by the Visual C++ compiler to
create these objects from their definitions. The resource file is a text file
with an .rc filename extension and contains a textual description of all the
various objects, including IDs, captions, dimensions, and so on.<BR>Some
resources, such as images and sounds, cannot be described in text, but have to
be stored in a binary format. These resources are stored in individual files,
with the filenames and locations included in the resource file.
<HR>
</BLOCKQUOTE>
<H2><A name=Heading6></A>Creating a Menu</H2>
<P>Creating a menu is not difficult. You will follow several steps:</P>
<P>
<DL>
<DT>
<DD><B>1. </B>Create the application that will house the menu.
<P></P>
<DT>
<DD><B>2. </B>Add a menu resource to your project.
<P></P>
<DT>
<DD><B>3. </B>Customize the menu resource to include the menu items for your
application.
<P></P>
<DT>
<DD><B>4. </B>Add functionality to your menu by connecting routines to your
menu items.
<P></P></DD></DL>
<H3><A name=Heading7></A>Creating the Application</H3>
<P>For the example in this chapter, you will create a simple dialog-style
application that contains a single button and a menu. To create your
application, do the following:</P>
<P>
<DL>
<DT>
<DD><B>1. </B>Create a new MFC AppWizard application, naming the project
<B>Menus</B>.
<P></P>
<DT>
<DD><B>2. </B>Select the default AppWizard settings on all screens. For the
dialog title, enter <B>Menus</B>.
<P></P>
<DT>
<DD><B>3. </B>When the AppWizard has generated your application shell, delete
all the controls from the dialog.
<P></P>
<DT>
<DD><B>4. </B>Add a single button to the dialog. Name the button IDC_EXIT, and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -