Menus - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Menus

Description:

Menu is a text or graphical list of commands from which an user can easily make a selection ... Windows operating system does menu management. Creating a Menu ... – PowerPoint PPT presentation

Number of Views:94
Avg rating:3.0/5.0
Slides: 24
Provided by: yax7
Category:
Tags: make | menus

less

Transcript and Presenter's Notes

Title: Menus


1
Menus
  • Jim Fawcett
  • CSE 791 Advanced Windows Programming
  • Summer 2002

2
Topics
  • Menu Definition and Classification
  • Creating and Loading a Menu
  • Responding to Menu Commands
  • Updating Items in a Menu
  • Keyboard Accelerators
  • Creating Menu Programmatically

3
Menus Definition and Classification
  • Menu is a text or graphical list of commands from
    which an user can easily make a selection
  • Menu classification
  • system menu
  • top-level menu
  • drop-down menu
  • popup menu
  • cascading menu
  • Windows operating system does menu management

4
Creating a Menu
  • Three ways to create in an MFC Application
  • Create a menu resource file and load the
    resulting menu into the application at run time.
  • Create a menu programmatically using CreateMenu,
    InsertMenu, and other CMenu functions.
  • Initialize a series of data structures defining
    menus contents and create the menu with
  • MenuLoadMenuIndirect.

5
Menu Resources
  • You can build a menu in the Resource View of the
    IDE.
  • A resource file is a scriptlike text file that
    defines an applications resources.
  • A resource is a binary object, Windows supports
    several types of resources
  • A compiler, Rc.exe, compiles the resource file
    and links the resulting resources into the
    applications executable file.

6
SDI Application Built with App Wizard
7
Menus in Resource View
8
Hooking to an Event Handler
9
Creating Event Handler
10
Menu in Action
11
Menu Resource Codes
  • IDR_MAINFRAME MENU PRELOAD DISCARDABLE
  • BEGIN
  • POPUP "File"
  • BEGIN
  • MENUITEM "Save\tCtrlS",
    ID_FILE_SAVE
  • MENUITEM "Save As...",
    ID_FILE_SAVE_AS
  • MENUITEM SEPARATOR
  • MENUITEM "Recent File",
    ID_FILE_MRU_FILE1,GRAYED
  • MENUITEM "Exit", ID_APP_EXIT
  • END
  • POPUP "Edit"
  • BEGIN
  • MENUITEM "Undo\tCtrlZ", ID_EDIT_
    UNDO
  • MENUITEM SEPARATOR
  • MENUITEM "Cut\tCtrlX", ID_EDIT_C
    UT
  • END
  • END

12
Ways to Load a Menu
  • Pass the menus resource ID to CFrameWndCreate
  • Create (NULL, _T ("My Application"),
  • WS_OVERLAPPEDWINDOW, rectDefault, NULL,
  • MAKEINTRESOURCE (IDR_MAINFRAME))
  • Pass the menus resource ID to FrameWndLoadFrame
  • LoadFrame (IDR_MAINFRAME,
  • WS_OVERLAPPEDWINDOW, NULL, NULL)
  • Load a top-level menu and attach it to a window
  • CMenu menu
  • menu.LoadMenu (IDR_MAINFRAME)
  • SetMenu (menu)
  • menu.Detach ()

13
Processing WM_Command Message
  • WM_Command Message has a parameter(wParam) whose
    low word is the ID of selected menu item
  • // In header file
  • afx_msg void OnFileExit( )
  • // In implementation file
  • BEGIN_MESSAGE_MAP (App Class Name, Base
    Class Name)
  • ON_COMMAND (ID_FILE_EXIT, OnFileExit)
  • END_MESSAGE_MAP( )
  • void CMainWindowOnFileExit ()
  • PostMessage (WM_CLOSE, 0, 0)

14
Command Range
  • One handler for a group of menu items
  • // In CMainWindow's message map
  • ON_COMMAND (ID_COLOR_RED,
    OnColorRed)ON_COMMAND (ID_COLOR_GREEN,
    OnColorGreen)ON_COMMAND (ID_COLOR_BLUE,
    OnColorBlue )
  • Or
  • // In CMainWindow's message map
  • ON_COMMAND_RANGE (ID_COLOR_RED,
    ID_COLOR_BLUE, OnColor )

15
Menu States
  • A menu can be in one of the following states
  • Value Meaning
  • CHECKED The menu item is currently
    checked.
  • DISABLED The menu item is currently
    disabled.
  • GRAYED The menu item is currently grayed
    out.
  • SELECTED The menu item is currently
    selected.

16
Updating Items in a Menu
  • Update a menu using ON_UPDATE_COMMAND_UI macro
  • ON_UPDATE_COMMAND_UI (ID_COLOR_RED,
    OnUpdateColorRed)
  • void CMainWindowOnUpdateColorRed (CCmdUI
    pCmdUI) // Implementation
  • Update Range
  • ON_UPDATE_COMMAND_UI_RANGE (ID_COLOR_BLACK,
  • ID_COLOR_WHITE, OnUpdateColorUI)
  • void CMainWindowOnUpdateColorUI (CCmdUI
    pCmdUI) // Implementation

17
Keyboard Accelerator
  • Keyboard accelerator provides application
    simplicity
  • Create an accelerator table resource
  • IDR_MAINFRAME ACCELERATORS PRELOAD MOVEABLE
    BEGIN
  • "N", ID_FILE_NEW,
    VIRTKEY,CONTROL
  • "Z", ID_EDIT_UNDO,
    VIRTKEY,CONTROL
  • VK_BACK, ID_EDIT_UNDO,
    VIRTKEY,ALT
  • END
  • Like menus, keyboard accelerators must be loaded
    and attached to a window before they'll do
    anything.
  • LoadAccelTable (MAKEINTRESOURCE
    (IDR_MAINFRAME))

18
Prosises Shape Application
  • Use AppWizard and ClassWizard to build an
    application.
  • Shows how to use menus, accelerators and MFCs
    UI update mechanism.
  • Examines application structures and code.

19
Creating Menus Programmatically
  • Use a combination of CMenuCreateMenu,
    CMenuCreatePopupMenu, and CMenuAppendMenu.
  • CMenu menuMain
  • menuMain.CreateMenu () // create an empty
    top-level menu CMenu menuPopupmenuPopup.CreateP
    opupMenu () menuPopup.AppendMenu (MF_STRING,
    ID_FILE_EXIT, "Exit")menuMain.AppendMenu
    (MF_POPUP, (UINT) menuPopup.Detach (), "File")
    SetMenu (menuMain) // to attach menu to a
    windowmenuMain.Detach () // to dissociate
    CMenu object and menu

20
Modifying Menus Programmatically
  • Function Description
  • AppendMenu Adds an item to the end of a menu
  • InsertMenu Inserts an item into a menu at a
    specified location
  • ModifyMenu Changes the command ID, text, or other
    characteristics of a menu item
  • DeleteMenu Deletes a menu item and the submenu
    associated with it, if any
  • RemoveMenu Deletes a menu item

21
Owner-Draw Menusa too brief synopsis
  • Often used to display bitmaps in menu items.
  • Stamp conventional items with the label
    MF_OWNERDRAW using CMenuModifyMenu
  • Add an OnMeasureitem handler for WM_MEASUREITEM
    message
  • Provide an OnDrawItem handler for WM_DRAWITEM
    message

22
Context Menus
  • A context menu is just a submenu that isnt
    attached to a top-level menu.
  • MFC's CMenuTrackPopupMenu function displays a
    context menu.
  • BOOL TrackPopupMenu (UINT nFlags, int x,
    int y, CWnd
  • pWnd, LPCRECT
    lpRect NULL)
  • TrackPopupMenu is typically called in response to
    WM_CONTEXTMENU messages.
  • Uses conventional menu processing to process the
    context menu commands

23
The Color Application
  • Shows how to
  • use AppWizard and ClassWizard to build an
    application
  • use owner-draw menus and context menu
  • analyzes application structures and code
Write a Comment
User Comments (0)
About PowerShow.com