转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
Menu Basics         ★★★★

Menu Basics

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2608 更新时间:2009/4/23 18:58:10

Menu Basics

Let''''s start by defining a few terms. The menu bar that appears at the top of a window is an application''''s top-level menu, and the commands in it are called top-level menu items. The menu that appears when a top-level menu item is clicked is a drop-down menu, and items in that menu are referred to as menu items. Menu items are identified by integer values called menu item IDs or command IDs. Windows also supports popup menus that look like drop-down menus but can be popped up anywhere on the screen. The context menu that appears when you right-click an object in the Windows shell is an example of a popup menu. Drop-down menus are actually popup menus that are submenus of an application''''s top-level menu.

Most top-level windows also feature a system menu containing commands for restoring, moving, sizing, minimizing, maximizing, and closing the window. Windows provides this menu, which you display by clicking the left mouse button on the small icon in the window''''s title bar, clicking the right mouse button in the body of the title bar, or pressing Alt-Spacebar.

MFC encapsulates menus and the actions that can be performed on them in the CMenu class. CMenu contains one public data member—an HMENU named m_hMenu that holds the handle of the corresponding menu—and several member functions that provide object-oriented wrappers around functions in the Windows API. CMenu::TrackPopupMenu, for example, displays a context menu, and CMenu::EnableMenuItem enables or disables a menu item. CMenu also contains a pair of virtual functions named DrawItem and MeasureItem that you can override if you want to create stylized menu items containing bitmaps and other graphical user interface elements.

You can create a menu in an MFC application in three ways:

  • You can create a menu programmatically, piecing it together using CreateMenu, InsertMenu, and other CMenu functions.

  • You can initialize a series of data structures defining the menu''''s contents and create the menu with CMenu::LoadMenuIndirect.

  • You can create a menu resource and load the resulting menu into the application at run time.

The third method is far and away the most common because it allows you to define a menu off line using a resource editor or, if you''''d prefer, a simple text editor. We''''ll focus on this method in the first half of the chapter.

Creating a Menu

The easiest way to create a menu is to add a menu template to your application''''s resource file. A resource file is a scriptlike text file that defines an application''''s resources; by convention, it is assigned the file name extension .rc and hence is often referred to as an RC file. A resource is a binary object such as a menu or an icon. Windows supports several types of resources, including (but not limited to) menus, icons, bitmaps, and strings. The resource compiler Rc.exe, which is provided with the Windows Software Development Kit (SDK) and is also part of Microsoft Visual C++, compiles the statements in an RC file and links the resulting resources into the application''''s EXE file. Every resource is identified by a string or an integer ID such as "MyMenu" (string) or IDR_MYMENU (integer). Integer resource IDs are given human-readable names such as IDR_MYMENU by means of #define statements in a header file. Once a resource is compiled and linked into an EXE, it can be loaded with a simple function call.

A menu template contains all the information the resource compiler needs to create a menu resource, including the menu''''s resource ID, the names of the menu items, and the IDs of the menu items. The menu template in Figure 4-1 comes from a project created by Visual C++''''s MFC AppWizard. It defines a single menu resource consisting of a top-level menu and four submenus—File, Edit, View, and Help. IDR_MAINFRAME is the menu''''s resource ID. PRELOAD and DISCARDABLE are resource attributes. PRELOAD tells Windows to load the menu resource into memory when the application starts. DISCARDABLE allows Windows to discard the resource if the memory it occupies is needed for other purposes. (If it''''s needed again, a discarded resource can be reloaded from the application''''s EXE file.) PRELOAD and DISCARDABLE are both artifacts of 16-bit Windows and have no impact on either the performance or behavior of 32-bit applications.

Figure 4-1. A menu template generated by the MFC AppWizard.

IDR_MAINFRAME MENU PRELOAD DISCARDABLE
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "&New\tCtrl+N",        ID_FILE_NEW
        MENUITEM "&Open...\tCtrl+O",    ID_FILE_OPEN
        MENUITEM "&Save\tCtrl+S",       ID_FILE_SAVE
        MENUITEM "Save &As...",         ID_FILE_SAVE_AS
        MENUITEM SEPARATOR
        MENUITEM "Recent File",         ID_FILE_MRU_FILE1,GRAYED
        MENUITEM SEPARATOR
        MENUITEM "E&xit",               ID_APP_EXIT
    END
    POPUP "&Edit"
    BEGIN
        MENUITEM "&Undo\tCtrl+Z",       ID_EDIT_UNDO
        MENUITEM SEPARATOR
        MENUITEM "Cu&t\tCtrl+X",        ID_EDIT_CUT
        MENUITEM "&Copy\tCtrl+C",       ID_EDIT_COPY
        MENUITEM "&Paste\tCtrl+V",      ID_EDIT_PASTE
    END
    POPUP "&View"
    BEGIN
        MENUITEM "&Toolbar",            ID_VIEW_TOOLBAR
        MENUITEM "&Status Bar",         ID_VIEW_STATUS_BAR
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About MyApp...",     ID_APP_ABOUT
    END
END

The statements between the opening and closing BEGIN and END statements define the contents of the menu, with POPUP statements defining top-level menu items and the associated submenus. The BEGIN and END statements following POPUP statements bracket MENUITEM statements defining the items in the submenus. The special MENUITEM SEPARATOR statement adds a thin horizontal line to the menu; it''''s used to provide visual separation between groups of menu items. The ampersands in the text of the menu items identify shortcut keys the user can press in combination with the Alt key to display submenus and select items from submenus. In this example, the File-Exit command can be selected by pressing Alt-F and then X. Windows underlines the F in "File" and the x in "Exit" so that they''''re easily identifiable as shortcut keys. If two or more items in the same menu are assigned the same shortcut key, the shortcut cycles among the menu items and no selection is made until the Enter key is pressed.

An ellipsis (...) in the text of a menu item indicates that further input is required after the item is selected. If the user selects Save, the document is saved immediately. But if the user selects Save As, a dialog box is displayed instead. To be consistent with other Windows applications, use an ellipsis for any menu item whose action is deferred until subsequent input is received from the user. If an item in the top-level menu executes a command instead of displaying a submenu, the text of the item should be followed with an exclamation mark, as in

IDR_MAINFRAME MENU PRELOAD DISCARDABLE
BEGIN
    POPUP "&File"
        [...]
    POPUP "&Edit"
        [...]
    POPUP "&View"
        [...]
    POPUP "&Help"
        [...]
    MENUITEM "E&xit!",      ID_APP_EXIT
END

It''''s legal to include MENUITEM statements in top-level menus this way, but these days it''''s considered bad form. And it''''s likely to surprise your users, most of whom are accustomed to seeing top-level menu items display submenus rather than take action themselves.

The ID_ values following the menu item names in the MENUITEM statements are command IDs. Every menu item should be assigned a unique command ID because it is this value that identifies the menu item to your application when the user makes a selection. By convention, IDs are defined with #define statements, and each is given the name ID_ or IDM_ followed by an item name spelled in capital letters. MFC''''s Afxres.h header file defines ID_ values for commonly used commands such as File-New and Edit-Paste. When you write document/view applications, using the predefined IDs automatically connects certain menu items to handling functions the framework provides. In nondocument/view applications, use of the predefined IDs is optional.

Valid values for menu item IDs range from 1 through 0xEFFF, but MFC Technical Note #20 recommends restricting the range to 0x8000 through 0xDFFF. IDs equal to 0xF000 and higher are reserved for Windows—specifically, for items in the system menu. The range 0xE000 to 0xEFFF is reserved for MFC. In practice, it''''s perfectly safe to use values lower than 0x8000, and in fact, restricting item IDs to the range 1 through 0x7FFF sidesteps a nasty bug in Windows 95 that affects owner-draw menu items. This bug is explained—and work-arounds are presented—later in this chapter.

The text following the tab character in some of the menu items (for example, the "Ctrl+O" in "Open…\tCtrl+O") identifies an accelerator. An accelerator is a key or combination of keys that, when pressed, has the same effect as selecting a menu item. Commonly used accelerators include Ctrl-X for Edit-Cut, Ctrl-C for Edit-Copy, and Ctrl-V for Edit-Paste. Text strings denoting accelerator keys are preceded by tab characters for alignment purposes. The default font used in menus is proportionally spaced, so it''''s futile to try to align menu text with spaces.

When you define a menu item with MENUITEM, you also have the option of specifying the item''''s initial state. The GRAYED keyword accompanying the File-Recent Fil

[1] [2] [3] [4]  下一页


[常用软件][设计]Flashation Menu Builder 菜单软件教学  [Delphi程序]Create a menu item into the Delphi menu
[VB.NET程序]WAP Basics  [VB.NET程序]Event-Handling Basics
[VB.NET程序]Internet Basics  [Web开发]利用JavaScript和CSS制作浮动menu
[MySql]linux下的c 编程------控制台下的menu  
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台