vel19.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 1,273 行 · 第 1/3 页
HTM
1,273 行
<LI>Press Tab to move the focus to the Name text box, and type <B>mnuFile</B>. The application will refer to the File menu bar item by the name mnuFile as needed. The rest of the Menu Design window's default property settings are fine as set. Your screen should look something like the one in Figure 19.3.<BR>The only shortcut access key available for menu bar commands is the underlined Alt+keystroke that occurs as the result of the Caption property's underlined letter. Don't attempt to select a Ctrl+keystroke from the Shortcut dropdown list box for the menu bar commands. Ctrl+keystroke shortcut access keystroke combinations are available only for pull-down menu commands.<BR>Don't press Enter or click the OK button to close the Menu Design window just yet, because you've got to add the additional menu bar commands before closing the window.<BR><BR></OL><P><B> <A HREF="19vel03.gif">Figure 19.3. The File menu bar command is now added to the menu.</A></B><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>Naming Menu Items: The event procedures within any Visual Basic application's code reference menu items by their menu item names. Preface all menu items, both menu bar as well as pull-down menu items, with the mnu caption prefix so that you can easily distinguish menu commands from variables and from the other controls as you work with the application's code.<BR>Generally, Visual Basic programmers follow the naming standard of naming menu bar items mnu, followed by the name of the item. Therefore, File is named mnuFile, Edit is named mnuEdit, and so on.<BR>As you add additional items to the pull-down menus, preface each of those items with the mnu prefix as well as the name of the menu bar command, and <I>then</I> append the name of the pull-down menu's item. Therefore, the File Exit item will be named mnuFileExit, View Normal will be named mnuViewNormal, and so on. The names then clearly describe the menu items that they represent.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Adding the remaining menu bar commands requires little more than adding the three items to the lower window of the Menu Design window. These steps complete the creation of the MYMENU.MAK's menu bar:<BR><OL><LI>Click the Menu Design window's Next command button to inform Visual Basic that you want to add the next item. The lower window's highlight bar drops down to the next line in preparation for the next menu item.<BR><BR><LI>Type <B>&Edit</B> at the Caption text box and press Tab. Name the second menu bar item mnuEdit. Click the Next command button to prepare the Menu Design window for the next menu bar item.<BR><BR><LI>Type <B>&View</B> and press Tab to move the focus to the Name text box. Type <B>mnuView</B> and select Next to prepare the Menu Design window for the final menu bar item.<BR><BR><LI>Type <B>&Help</B> and press Tab to move the focus to the Name text box. Type <B>mnuHelp</B>. Your screen should look like the one in Figure 19.4.<BR><BR></OL><P><B> <A HREF="19vel04.gif">Figure 19.4. The Menu Design window with all four menu bar items entered.</A></B><BR><P>Close the Menu Design window be pressing Enter or clicking the OK command button. Immediately, Visual Basic displays the new menu bar across the top of the application's Form window.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The Menu Design window provides the tools needed to add a menu bar along with the menu bar's pull-down menus and commands. Adding the menu bar items involves little more than typing the item name and caption. You're now ready to add the individual items to the menu. The next section explains how to complete this unit's File menu bar pull-down menu.<BR><BR><A NAME="E68E144"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Adding Pull-Down Menus</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Each menu bar command opens a pull-down menu that consists of a series of commands, separator bars, and access keystrokes. These pull-down menus are sometimes called <I>submenus</I>. The four arrow key command buttons inside the Menu Design window enable you to indent the pull-down menu commands from their matching menu bar commands, to show which items go with which menu bar commands.<BR><P>Now that you've added the menu bar, you can add the individual items to the pull-down menus. You didn't have to complete the menu bar before completing each pull-down menu. You could have added the File command to the menu bar and then completed the File pull-down menu before adding the View command to the menu bar. The order in which you add menu items doesn't matter at all.<BR><P>The File pull-down menu will contain the following items:<BR><UL><LI>The New command<BR><BR><LI>The Open command with a shortcut access keystroke of Ctrl+O<BR><BR><LI>The Close command<BR><BR><LI>A separator bar<BR><BR><LI>The Exit command<BR><BR></UL><P>After you add these submenu items, you can hook up the menu commands to event procedures that you write, which is explained in the next section.<BR><P>Adding submenu items consists of following steps that are virtually identical to the ones you followed when adding the menu bar items. The difference is that the additional Menu Design window options become more important because you'll apply these options more frequently to the pull-down menu items. Table 19.1 explains the remaining Menu Design window properties.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 19.1. The Menu Design window's remaining property explanations.</B></FONT></CENTER><BR><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Property</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Checked</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Indicates whether a menu item has a check mark next to the item. Generally, you'll add check marks to menu commands that perform on or off actions, such as a View menu that contains a Highlighted command. The check mark appears when you, at design time or through code, set the menu item's Checked property to True. The check mark goes away (indicating that the item is no longer active or selected) when you set the Checked property to False.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>HelpContextID</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>This is a code that matches a help file description if you add help files to your application.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Index</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>If you create a menu control array rather than name individual menu items separately, the Index property specifies the menu item's subscript within the control array.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Shortcut</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>This is a dropdown list of Ctrl+keystroke access keys that you can add to any pull-down menu item.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Window List</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Specifies whether the menu item applies to an advanced application's MDI (Multiple Document Interface) document. The menus that you create for this book don't require the use of MDI features.</FONT></TABLE><P>Perhaps the most important command keys on the Menu Design window when you add pull-down menu items are the four arrow command buttons. The left and right arrow command buttons indicate which items go with which menu bar command. In other words, if four items in the lower window are indented to the right and appear directly beneath the File menu bar item, those four indented items will appear on the File pull-down menu. The left arrow removes the indention and the right arrow adds the indention. The up and down arrow keys move menu items up and down the list of menu items, rearranging the order if you need to do so.<BR><P>The arrow keys make a lot of sense when you see them used. Follow these steps to create the File pull-down menu bar's submenu:<BR><OL><LI>Move the lower window's highlight line to the &Edit menu bar item. Click the Insert command button. You always insert <I>before</I> an item, so to add items to the File menu, you must <I>insert</I> before the Edit menu bar item in the lower window.<BR><BR><LI>Click the right arrow command button. Visual Basic adds ellipsis, showing that the newly inserted item will be indented under the File option.<BR><BR><LI>Move the focus to the Caption prompt and type <B>&New</B>.<BR><BR><LI>Press Tab to move the focus to the Name prompt and type <B>mnuFileNew</B>.<BR><BR><LI>Click Next and then Insert to insert another item beneath the New item. Your Menu Design window should look like the one in Figure 19.5.<BR><BR><P><B> <A HREF="19vel05.gif">Figure 19.5. In the process of adding to the File menu.</A></B><BR><LI>Move the focus to the Caption prompt and type <B>&Open</B>. Press Tab and enter the Name value of mnuFileOpen. Rather than add the next item, click the Shortcut dropdown list and select Ctrl+O from the list. When the user now displays the File pull-down menu, Ctrl+O will appear as the shortcut key next to the File Open menu item.<BR><BR><LI>Click Next and then Insert to make room for the next item. Add the Close caption with the name mnuFileClose. Click Next and then Insert to insert another item beneath the Close item. It's now time to add the separator bar.<BR><BR></OL><P>Separator bars help you break individual pull-down menus into separate sections. Although several commands appear on most Windows applications' File pull-down menus, these commands don't all perform the same kind of functions. Some relate to files, some relate to printing, and the Exit command always appears on the File menu as well. The separator bars help distinguish groups of different items from each other on the pull-down menus.<BR><P>All separator bars have the same Caption property that is nothing more than a hyphen, -. You must give every separator bar a different name. Usually, the name of the separator bars on the File menu are mnuFileBar1, mnuFileBar2, and so on. You must add the separator bars using the right arrow's command button so that they indent properly beneath their pull-down menus. Follow these steps to add the single separator bar for this MYMENU.MAK's File pull-down menu:<BR><OL><LI>Type <B>-</B> (the hyphen) for the Caption and press Tab.<BR><BR><LI>Type <B>mnuFuleBar1</B> for the Name.<BR><BR></OL><P>There's one more item to add. You know enough to add the Exit command to the File menu. After adding Exit, your Menu Design window should look like the one shown in Figure 19.6.<BR><P><B> <A HREF="19vel06.gif">Figure 19.6. The completed </B><B>F</B><B>ile pull-down menu commands.</A></B><BR><P>As an extra menu feature, add one checkmarked item to the View pull-down menu. Add an indented Highlighted item with the name mnuViewHighlighted. Click the Checked check box. The View Highlighted item will initially be checked when the user selects the View Highlighted from the menu.<BR><P>Now that you've completed the menu (as far as we're taking it here), click the OK command button. When the Menu Design window disappears, you'll see the application's Form window with the menu bar across the top of the screen.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Adding menus to your applications requires only that you master the Menu Design window. Menus are nothing more than advanced controls with property values that you set using the Menu Design window. Most menu items require that you specify a Caption and Name property as well as indent the item properly under its menu bar command. Optionally, a menu item might contain a shortcut access keystroke or a check mark next to the item.<BR><BR><A NAME="E68E145"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Connecting Menus to Event Procedures</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Once you've built your menu, you need to tie each menu command to your application. To respond to menu selections, you need to write Click event procedures that you want Visual Basic to execute when the user selects a menu command.<BR><P>Visual Basic generates a Click event when the user selects a menu command. The name of the menu command, combined with Click, provides the name of the event procedure. Therefore, the File Exit menu item will generate the execution of the event procedure named mnuFileExit_Click().
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?