Drop Down Menus, System Menu-Windows Programming-Lecture Notes, Study notes of Windows Programming

This lecture handout is for Windows Programming course. It was provided by Prof. Jaimini Chinmay at Ambedkar University, Delhi. It includes: Dialogue, Command, Procedure, Color, Function, Drawing, About, Implement, Box, Common

Typology: Study notes

2011/2012

Uploaded on 08/07/2012

anishay
anishay 🇮🇳

4.2

(25)

118 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Menu and Dialogs 2
19.1 Menus
We have discussed Menus in our previous lecture, here we will know more about menus
and their use in Windows Applications.
19.2 Menu Items
Command Items and Items that Open Submenus
When the user chooses a command item, the system sends a command message to the
window that owns the menu. If the command item is on the window menu, the system
sends the WM_SYSCOMMAND message. Otherwise, it sends the WM_COMMAND
message.
Handle is associated with each menu item that opens a submenu. When the user points to
such an item, the system opens the submenu. No command message is sent to the owner
window. However, the system sends a WM_INITMENUPOPUP message to the owner
window before displaying the submenu. You can get a handle to the submenu associated
with an item by using the GetSubMenu or GetMenuItemInfo function.
A menu bar typically contains menu names, but it can also contain command items. A
submenu typically contains command items, but it can also contain items that open nested
submenus. By adding such items to submenus, you can nest menus to any depth. To
provide a visual cue for the user, the system automatically displays a small arrow to the
right of the text of a menu item that opens a submenu.
Menu-Item Identifier
Associated with each menu item is a unique, application-defined integer, called a menu-
item identifier. When the user chooses a command item from a menu, the system sends
the item's identifier to the owner window as part of a WM_COMMAND message. The
window procedure examines the identifier to determine the source of the message, and
processes the message accordingly. In addition, you can specify a menu item using its
identifier when you call menu functions; for example, to enable or disable a menu item.
Menu items that open submenus have identifiers just as command items do. However, the
system does not send a command message when such an item is selected from a menu.
Instead, the system opens the submenu associated with the menu item.
To retrieve the identifier of the menu item at a specified position, use the
GetMenuItemID or GetMenuItemInfo function.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Drop Down Menus, System Menu-Windows Programming-Lecture Notes and more Study notes Windows Programming in PDF only on Docsity!

19.1 Menus

We have discussed Menus in our previous lecture, here we will know more about menus and their use in Windows Applications.

19.2 Menu Items

Command Items and Items that Open Submenus

When the user chooses a command item, the system sends a command message to the window that owns the menu. If the command item is on the window menu, the system sends the WM_SYSCOMMAND message. Otherwise, it sends the WM_COMMAND message.

Handle is associated with each menu item that opens a submenu. When the user points to such an item, the system opens the submenu. No command message is sent to the owner window. However, the system sends a WM_INITMENUPOPUP message to the owner window before displaying the submenu. You can get a handle to the submenu associated with an item by using the GetSubMenu or GetMenuItemInfo function.

A menu bar typically contains menu names, but it can also contain command items. A submenu typically contains command items, but it can also contain items that open nested submenus. By adding such items to submenus, you can nest menus to any depth. To provide a visual cue for the user, the system automatically displays a small arrow to the right of the text of a menu item that opens a submenu.

Menu-Item Identifier

Associated with each menu item is a unique, application-defined integer, called a menu- item identifier. When the user chooses a command item from a menu, the system sends the item's identifier to the owner window as part of a WM_COMMAND message. The window procedure examines the identifier to determine the source of the message, and processes the message accordingly. In addition, you can specify a menu item using its identifier when you call menu functions; for example, to enable or disable a menu item.

Menu items that open submenus have identifiers just as command items do. However, the system does not send a command message when such an item is selected from a menu. Instead, the system opens the submenu associated with the menu item.

To retrieve the identifier of the menu item at a specified position, use the GetMenuItemID or GetMenuItemInfo function.

Menu-Item Position

In addition to having a unique identifier, each menu item in a menu bar or menu has a unique position value. The leftmost item in a menu bar, or the top item in a menu, has position zero. The position value is incremented for subsequent menu items. The system assigns a position value to all items in a menu, including separators. The following illustration shows the position values of items in a menu bar and in a menu.

When calling a menu function that modifies or retrieves information about a specific menu item, you can specify the item using either its identifier or its position. For more information, see Menu Modifications.

Default Menu Items

A submenu can contain one default menu item. When the user opens a submenu by double-clicking, the system sends a command message to the menu's owner window and closes the menu as if the default command item had been chosen. If there is no default command item, the submenu remains open. To retrieve and set the default item for a submenu, use the GetMenuDefaultItem and SetMenuDefaultItem functions.

Selected and Clear Menu Items

A menu item can be either selected or clear. The system displays a bitmap next to selected menu items to indicate their selected state. The system does not display a bitmap next to clear items, unless an application-defined "clear" bitmap is specified. Only menu items in a menu can be selected; items in a menu bar cannot be selected.

Applications typically check or clear a menu item to indicate whether an option is in effect. For example, suppose an application has a toolbar that the user can show or hide by using a Toolbar command on a menu. When the toolbar is hidden, the Toolbar menu item is clear. When the user chooses the command, the application checks the menu item and shows the toolbar.

A check mark attribute controls whether a menu item is selected. You can set a menu item's check mark attribute by using the CheckMenuItem function. You can use the

The EnableMenuItem function enables, grays, or disables a menu item. To determine whether a menu item is enabled, grayed, or disabled, use the GetMenuItemInfo function.

Instead of GetMenuItemInfo , you can also use the GetMenuState function to determine whether a menu item is enabled, grayed, or disabled.

Highlighted Menu Items

The system automatically highlights menu items on menus as the user selects them. However, highlighting can be explicitly added or removed from a menu name on the menu bar by using the HiliteMenuItem function. This function has no effect on menu items on menus. When HiliteMenuItem is used to highlight a menu name, though, the name only appears to be selected. If the user presses the ENTER key, the highlighted item is not chosen. This feature might be useful in, for example, a training application that demonstrates the use of menus.

Owner-Drawn Menu Items

An application can completely control the appearance of a menu item by using an owner- drawn item. Owner-drawn items require an application to take total responsibility for drawing selected (highlighted), selected, and cleared states. For example, if an application provided a font menu, it could draw each menu item by using the corresponding font; the item for Roman would be drawn with roman, the item for Italic would be drawn in italic, and so on. For more information, see Creating Owner-Drawn Menu Items.

Menu Item Separators and Line Breaks

The system provides a special type of menu item, called a separator , which appears as a horizontal line. You can use a separator to divide a menu into groups of related items. A separator cannot be used in a menu bar, and the user cannot select a separator.

When a menu bar contains more menu names than will fit on one line, the system wraps the menu bar by automatically breaking it into two or more lines. You can cause a line break to occur at a specific item on a menu bar by assigning the MFT_MENUBREAK type flag to the item. The system places that item and all subsequent items on a new line.

When a menu contains more items than will fit in one column, the menu will be truncated. You can cause a column break to occur at a specific item in a menu by assigning the MFT_MENUBREAK type flag to the item or using the MENUBREAK option in the MENUITEM statement. The system places that item and all subsequent items in a new column. The MFT_MENUBARBREAK type flag has the same effect, except that a vertical line appears between the new column and the old.

If you use the AppendMenu, InsertMenu, or ModifyMenu functions to assign line breaks, you should assign the type flags MF_MENUBREAK or MF_MENUBARBREAK.

19.3 Drop Down Menus

Drop down menus are submenu. For example you are working with notepad and you are going to make a new file, for this you press on a file menu and menu drops itself down and you select new from that menu, so this menu is called drop down menu. This drop down menu is called submenu.

19.4 Get Sub Menu

The GetSubMenu function retrieves a handle to the drop-down menu or submenu activated by the specified menu item.

HMENU GetSubMenu(

HMENU hMenu , int nPos );

hMenu: Handle to the menu. nPos: Specifies the zero-based relative position in the specified menu of an item that activates a drop-down menu or submenu.

Return Value: If the function succeeds, the return value is a handle to the drop-down menu or submenu activated by the menu item. If the menu item does not activate a drop- down menu or submenu, the return value is NULL.

19.5 Example Application

Here we create an application which will demonstrate menus.

19.5.1 Popup Menu ( Resource File View )

Popup menu is a main menu which may have sub menu.

IDR_MENU_POPUP MENU DISCARDABLE BEGIN POPUP "Popup Menu" BEGIN MENUITEM "&Line", ID_POPUPMENU_LINE MENUITEM "&Circle", ID_POPUPMENU_CIRCLE MENUITEM "&Rectangle", ID_POPUPMENU_RECTANGLE POPUP "&Other" BEGIN MENUITEM "&Polygon", ID_OTHER_POLYGON MENUITEM "&Text Message", ID_OTHER_TEXTMESSAGE END

END

POINTS structure contains SHORT (short) x, and SHORT y

typedef struct tagPOINTS { SHORT x; //horizontal short integer SHORT y; //vertical short integer } POINTS;

19.5.4 Main Window Procedure

POINTS pts; POINT pt; … … … … case WM_RBUTTONDOWN:

pts = MAKEPOINTS(lParam); pt.x = pts.x; pt.y = pts.y;

ClientToScreen(hWnd, &pt); //convert the window coordinates to the screen coordinates

result = TrackPopupMenu(hPopupMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON, pt.x, pt.y, 0, hWnd, 0 );

19.5.5 Set Menu Item Information

The SetMenuInfo function sets information for a specified menu.

BOOL SetMenuInfo(

HMENU hmenu , //handle to the menu LPCMENUINFO lpcmi //menu informations );

hmenu: Handle to a menu.

lpcmi: Pointer to a MENUINFO structure for the menu.

Return Value:

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

19.5.6 System Menu

The GetSystemMenu function allows the application to access the window menu (also known as the system menu or the control menu) for copying and modifying.

HMENU GetSystemMenu(

HWND hWnd , //handle to the window BOOL bRevert //action specification );

hWnd: Handle to the window that will own a copy of the window menu.

bRevert: Specifies the action to be taken. If this parameter is FALSE, GetSystemMenu returns a handle to the copy of the window menu currently in use. The copy is initially identical to the window menu, but it can be modified. If this parameter is TRUE, GetSystemMenu resets the window menu back to the default state. The previous window menu, if any, is destroyed.

Return Value: If the bRevert parameter is FALSE, the return value is a handle to a copy of the window menu. If the bRevert parameter is TRUE, the return value is NULL.

Any window that does not use the GetSystemMenu function to make its own copy of the window menu receives the standard window menu.

The window menu initially contains items with various identifier values, such as SC_CLOSE, SC_MOVE, and SC_SIZE.

Menu items on the window menu send WM_SYSCOMMAND messages.

All predefined window menu items have identifier numbers greater than 0xF000. If an application adds commands to the window menu, it should use identifier numbers less than 0xF000.

The system automatically grays items on the standard window menu, depending on the situation. The application can perform its own checking or graying by responding to the WM_INITMENU message that is sent before any menu is displayed.

19.5.7 System Menu Identifiers

The window menu initially contains items with various identifier values, such as Figure labelled as

SC_MOVE Move

format=LOCAL; GetLocalTime(&st); hOurMenu = GetMenu(hWnd); CheckMenuItem(hOurMenu, ID_FORMAT_LOCALTIME, MF_BYCOMMAND | MF_CHECKED); Break;

case WM_COMMAND: switch(LOWORD(wParam)) { case ID_FORMAT_UTC: if(format == UTC) break; format = UTC; hOurMenu = GetMenu(hWnd); result = CheckMenuItem(hOurMenu, ID_FORMAT_UTC, MF_BYCOMMAND | MF_CHECKED); result = CheckMenuItem(hOurMenu, ID_FORMAT_LOCALTIME, MF_BYCOMMAND | MF_UNCHECKED); DrawMenuBar(hWnd); (format == UTC)? GetSystemTime(&st) : GetLocalTime(&st); GetClientRect(hWnd, &rect); InvalidateRect(hWnd, &rect, TRUE); break;

case WM_PAINT: hDC = BeginPaint(hWnd, &ps); wsprintf(msg, "Hour: %2d:%02d:%02d", st.wHour, st.wMinute, st.wSecond); TextOut(hDC, 10, 10, msg, lstrlen(msg)); EndPaint(hWnd, &ps); break;

case WM_TIMER: if(wParam == ID_TIMER) { (format == UTC)? GetSystemTime(&st) : GetLocalTime(&st); GetClientRect(hWnd, &rect); InvalidateRect(hWnd, &rect, TRUE); break; } break;

19.9 Dialogs

Dialogs are important resource in windows. Most of the information in window are displayed in dialog boxes. Simple example of dialog boxes is about dialog box or properties are shown in normally in dialog boxes.

A dialog box is a temporary window an application creates to retrieve user input. An application typically uses dialog boxes to prompt the user for additional information for menu items. A dialog box usually contains one or more controls (child windows) with which the user enters text, chooses options, or directs the action.

Windows also provides predefined dialog boxes that support common menu items such as Open and Print. Applications that use these menu items should use the common dialog boxes to prompt for this user input, regardless of the type of application.

Dialogs are of two types.

  • Modal Dialog Boxes
  • Modeless Dialog Boxes

19.9.1 Modal Dialog Boxes

A modal dialog box should be a pop-up window having a window menu, a title bar, and a thick border; that is, the dialog box template should specify the WS_POPUP, WS_SYSMENU, WS_CAPTION, and DS_MODALFRAME styles. Although an application can designate the WS_VISIBLE style, the system always displays a modal dialog box regardless of whether the dialog box template specifies the WS_VISIBLE style. An application must not create a modal dialog box having the WS_CHILD style. A modal dialog box with this style disables itself, preventing any subsequent input from reaching the application.

An application creates a modal dialog box by using either the DialogBox or DialogBoxIndirect function. DialogBox requires the name or identifier of a resource containing a dialog box template; DialogBoxIndirect requires a handle to a memory object containing a dialog box template. The DialogBoxParam and DialogBoxIndirectParam functions also create modal dialog boxes; they are identical to the previously mentioned functions but pass a specified parameter to the dialog box procedure when the dialog box is created.

When creating the modal dialog box, the system makes it the active window. The dialog box remains active until the dialog box procedure calls the EndDialog function or the system activates a window in another application. Neither the user nor the application can make the owner window active until the modal dialog box is destroyed.

An application creates a modeless dialog box by using the CreateDialog or CreateDialogIndirect function. CreateDialog requires the name or identifier of a resource containing a dialog box template; CreateDialogIndirect requires a handle to a memory object containing a dialog box template. Two other functions, CreateDialogParam and CreateDialogIndirectParam, also create modeless dialog boxes; they pass a specified parameter to the dialog box procedure when the dialog box is created.

CreateDialog and other creation functions return a window handle for the dialog box. The application and the dialog box procedure can use this handle to manage the dialog box. For example, if WS_VISIBLE is not specified in the dialog box template, the application can display the dialog box by passing the window handle to the ShowWindow function.

A modeless dialog box neither disables the owner window nor sends messages to it. When creating the dialog box, the system makes it the active window, but the user or the application can change the active window at any time. If the dialog box does become inactive, it remains above the owner window in the Z order, even if the owner window is active.

The application is responsible for retrieving and dispatching input messages to the dialog box. Most applications use the main message loop for this. To permit the user to move to and select controls by using the keyboard, however, the application must call the IsDialogMessage function. For more information about this function, see Dialog Box Keyboard Interface.

A modeless dialog box cannot return a value to the application as a modal dialog box does, but the dialog box procedure can send information to the owner window by using the SendMessage function.

An application must destroy all modeless dialog boxes before terminating. It can destroy a modeless dialog box by using the DestroyWindow function. In most cases, the dialog box procedure calls DestroyWindow in response to user input, such as clicking the Cancel button. If the user never closes the dialog box in this way, the application must call DestroyWindow.

DestroyWindow invalidates the window handle for the dialog box, so any subsequent calls to functions that use the handle return error values. To prevent errors, the dialog box procedure should notify the owner that the dialog box has been destroyed. Many applications maintain a global variable containing the handle for the dialog box. When the dialog box procedure destroys the dialog box, it also sets the global variable to NULL, indicating that the dialog box is no longer valid.

The dialog box procedure must not call the EndDialog function to destroy a modeless dialog box.

19.9.3 Message Box Function

A message box is a special dialog box that an application can use to display messages and prompt for simple input. A message box typically contains a text message and one or more buttons. An application creates the message box by using the MessageBox or MessageBoxEx function, specifying the text and the number and types of buttons to display. Note that currently there is no difference between how MessageBox and MessageBoxEx work.

Although the message box is a dialog box, the system takes complete control of the creation and management of the message box. This means the application does not provide a dialog box template and dialog box procedure. The system creates its own template based on the text and buttons specified for the message box and supplies its own dialog box procedure.

A message box is a modal dialog box and the system creates it by using the same internal functions that DialogBox uses. If the application specifies an owner window when calling MessageBox or MessageBoxEx , the system disables the owner. An application can also direct the system to disable all top-level windows belonging to the current thread by specifying the MB_TASKMODAL value when creating the dialog box.

The system can send messages to the owner, such as WM_CANCELMODE and WM_ENABLE, just as it does when creating a modal dialog box. The owner window should carry out any actions requested by these messages.

19.9.4 Modal Loop

Modal loop is run by Modal dialogs and process message as does application message loop. That’s why program execution is transfer to modal loop so the modal loop itself gets messages and dispatch message.

19.9.5 Dialog Resource Template

IDD_DIALOG_ABOUT DIALOG DISCARDABLE 0, 0, 265, 124

STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU

CAPTION "About" FONT 8, "MS Sans Serif" BEGIN DEFPUSHBUTTON "OK",IDOK,208,7,50, PUSHBUTTON "Cancel",IDCANCEL,208,24,50, LTEXT "Some copyright text", IDC_STATIC, 67, 27,107, ICON IDI_ICON_VU,IDC_STATIC,17,14,20, END