CS 351 Systems Programming Menus, Dialogs, and Controls - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

CS 351 Systems Programming Menus, Dialogs, and Controls

Description:

All menu items have a state they can be enabled, disabled, grayed out, checked, or unchecked ... Checking and unchecking a menu item. CheckMenuItem (hMenu, ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 14
Provided by: ondrejh
Category:

less

Transcript and Presenter's Notes

Title: CS 351 Systems Programming Menus, Dialogs, and Controls


1
CS 351 Systems ProgrammingMenus, Dialogs, and
Controls
  • Ondrej Hrebicek, Jesse Yurkovich
  • October 23rd, 2002

2
Introduction to Menus
  • Easy to create using Visual Studios Resource
    Editor
  • Each selectable menu item is identified by a
    unique ID
  • Whenever a menu item is activated, a WM_COMMAND
    message is sent to the owner window together with
    the unique ID
  • The main menu, also called the top level menu,
    is located immediately below the caption bar
  • Submenus, or drop-down menus, are invoked by
    clicking on items in the main menu, or within the
    submenus themselves
  • All menu items have a state they can be
    enabled, disabled, grayed out, checked, or
    unchecked

3
Creating a Menu
  • In Visual Studio, click on Insert and select
    Resource
  • Pick Menu in the dialog box that appears

4
Adding Items to a Menu
  • In the Resource Editor, double click on an empty
    box
  • When entering a caption, add an ampersand before
    the letter to be used as an accelerator it will
    become underlined
  • Each leaf menu item also has an ID associated
    with it
  • The ID you pick for your items will be sent to
    your applications window procedure along with
    the WM_COMMAND message

5
Adding Items to a Menu (2)
  • The following code needs to be added to your
    window procedure to capture clicks on the Open
    menu item

6
Integrating Menus with your Application
  • Possibility 1 - The menus name needs to be
    specified in your window class
  • wndclass.lpszMenuName TEXT (MainMenu)
  • Possibility 2 - Use LoadMenu () to obtain a
    handle to your menu
  • hMenu LoadMenu (hInstance, TEXT (MainMenu))
  • hMenu LoadMenu (hInstance, MAKEINTRESOURCE
    (ID_MENU))
  • Which you then pass to CreateWindow () or
    SetMenu ()
  • CreateWindow (, hMenu, )
  • SetMenu (hWnd, hMenu)

7
Menu Operations
  • Obtaining a handle to a windows menu
  • hMenu GetMenu (hWnd)
  • Checking and unchecking a menu item
  • CheckMenuItem (hMenu, iItemID, MF_CHECKED)
  • CheckMenuItem (hMenu, iItemID, MF_UNCHECKED)
  • Enabling, disabling, and graying out a menu item
  • EnableMenuItem (hMenu, iItemID, MF_ENABLED)
  • EnableMenuItem (hMenu, iItemID, MF_DISABLED)
  • EnableMenuItem (hMenu, iItemID, MF_GRAYED)

8
Floating PopUp Menus
  • Can be separate from main menu, or main menus
    submenus
  • In order to display a floating popup menu in
    your client area
  • Capture the WM_RBUTTONUP message
  • Get the x and y coordinates of the mouse clicks
    (stored in the low and high word of lParam,
    respectively)
  • Call TrackPopupMenu (hPopupMenu, uFlags, x, y,
    nReserved, hWnd, prcRect)
  • Zero or more uFlags specify function options
    (alignment, etc)
  • nReserved must always be 0
  • prcRect is ignored set to NULL

9
Creating a Dialog
  • Process is very similar to creating a menu
  • Instead of choosing Menu, choose Dialog and
    select the IDD_FORMVIEW style
  • Adding controls to the Dialog in the Resource
    Editor is a simple matter of dragging and
    dropping
  • Double clicking on a control, as well as the
    dialog itself, will bring up its properties
  • Some of these properties need to be modified so
    your program knows how the work with the dialogs
    and their controls
  • The most important properties for controls will
    be their ID and their caption
  • Dont be afraid to change other properties as
    well many are very effective and often necessary

10
Integrating Dialogs with your Application
  • The process will be much the same as menus with
    one added caveat
  • You must think of Dialogs as a window in and of
    itself - this means all dialogs will get their
    own window procedure to handle their actions
  • Use DialogBox () in order to create a dialog and
    display it
  • DialogBox (hInst, (LPCTSTR) IDD_LISTVIEW, hWnd,
    (DLGPROC) WndProcListViewDlg)
  • IDD_LISTVIEW is the ID you assigned to your
    dialog box in the Resource Editor
  • WndProcListViewDlg is your dialogs window
    procedure
  • Messages are sent to this procedure in the same
    way as to your main window procedure

11
Integrating Dialogs with your Application
  • Inside your dialogs window procedure, you will
    often need to handle WM_INITDIALOG and WM_COMMAND
    messages
  • WM_COMMAND functions the same way it does for
    menus when controls are manipulated inside the
    dialog box, WM_COMMAND messages are sent to the
    window procedure
  • The ID of the control which was manipulated is
    sent with the WM_COMMAND message in the LOWORD of
    lParam
  • When the time comes to destroy the dialog box,
    call
  • EndDialog (hWnd, LOWORD (wParam))

12
More on Controls
  • Examples of window controls
  • Scrollable Listbox
  • Drop down Combo box
  • Progress bar
  • Radio and check buttons
  • All controls, whether they are within a dialog
    or on the main window itself all function the
    same - they all will send a WM_COMMAND message
    whenever theyre manipulated
  • You communicate with a control by sending
    messages to it messages can change a controls
    appearance and return information about it

13
Listbox Control
  • Getting a handle to a Listbox inside a dialog
    where hWnd is the handle to the dialog box
  • hListBox GetDlgItem (hWnd, IDC_LIST_LBOX)
  • Adding an item to a Listbox
  • SendMessage (hListBox, LB_ADDSTRING, 0, (LPARAM)
    string)
  • Deleting an item from a Listbox
  • SendMessage (hListBox, LB_DELETESTRING, 0, 0)
  • Obtaining the position of the item currently
    selected
  • int iSelPos SendMessage (hListBox,
    LB_GETCURSEL, 0, 0)
Write a Comment
User Comments (0)
About PowerShow.com