Menus-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: Menu, String, Resource, New, File, Open, Source, Main, Command, Bar, Message, Drop, Down

Typology: Study notes

2011/2012

Uploaded on 08/07/2012

anishay
anishay 🇮🇳

4.2

(25)

118 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
String and Menu Resource 2
18.1 Menus
A menu is a list of items that specify options or groups of options (a submenu) for an
application. Clicking a menu item opens a submenu or causes the application to carry out
a command.
18.1.1 Menu bar and Menus
A menu is arranged in a hierarchy. At the top level of the hierarchy is the menu bar;
which contains a list of menus, which in turn can contain submenus. A menu bar is
sometimes called a top-level menu, and the menus and submenus are also known as pop-
up menus.
A menu item can either carry out a command or open a submenu. An item that carries out
a command is called a command item or a command.
An item on the menu bar almost always opens a menu. Menu bars rarely contain
command items. A menu opened from the menu bar drops down from the menu bar and
is sometimes called a drop-down menu. When a drop-down menu is displayed, it is
attached to the menu bar. A menu item on the menu bar that opens a drop-down menu is
also called a menu name.
The menu names on a menu bar represent the main categories of commands that an
application provides. Selecting a menu name from the menu bar typically opens a menu
whose menu items correspond to the commands in a category. For example, a menu bar
might contain a File menu name that, when clicked by the user, activates a menu with
menu items such as New, Open, and Save. To get information about a menu bar, call
GetMenuBarInfo.
Only an overlapped or pop-up window can contain a menu bar; a child window cannot
contain one. If the window has a title bar, the system positions the menu bar just below it.
A menu bar is always visible. A submenu is not visible, however, until the user selects a
menu item that activates it. For more information about overlapped and pop-up windows,
see Window Types.
Each menu must have an owner window. The system sends messages to a menu's owner
window when the user selects the menu or chooses an item from the menu.
docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Menus-Windows Programming-Lecture Notes and more Study notes Windows Programming in PDF only on Docsity!

18.1 Menus

A menu is a list of items that specify options or groups of options (a submenu) for an application. Clicking a menu item opens a submenu or causes the application to carry out a command.

18.1.1 Menu bar and Menus

A menu is arranged in a hierarchy. At the top level of the hierarchy is the menu bar ; which contains a list of menus , which in turn can contain submenus. A menu bar is sometimes called a top-level menu , and the menus and submenus are also known as pop- up menus.

A menu item can either carry out a command or open a submenu. An item that carries out a command is called a command item or a command.

An item on the menu bar almost always opens a menu. Menu bars rarely contain command items. A menu opened from the menu bar drops down from the menu bar and is sometimes called a drop-down menu. When a drop-down menu is displayed, it is attached to the menu bar. A menu item on the menu bar that opens a drop-down menu is also called a menu name.

The menu names on a menu bar represent the main categories of commands that an application provides. Selecting a menu name from the menu bar typically opens a menu whose menu items correspond to the commands in a category. For example, a menu bar might contain a File menu name that, when clicked by the user, activates a menu with menu items such as New , Open , and Save. To get information about a menu bar, call GetMenuBarInfo.

Only an overlapped or pop-up window can contain a menu bar; a child window cannot contain one. If the window has a title bar, the system positions the menu bar just below it. A menu bar is always visible. A submenu is not visible, however, until the user selects a menu item that activates it. For more information about overlapped and pop-up windows, see Window Types.

Each menu must have an owner window. The system sends messages to a menu's owner window when the user selects the menu or chooses an item from the menu.

Figure 1 Menu in Visual Studio Editor

18.1.1.1 Short cut Menus

The system also provides shortcut menus. A shortcut menu is not attached to the menu bar; it can appear anywhere on the screen. An application typically associates a shortcut menu with a portion of a window, such as the client area, or with a specific object, such as an icon. For this reason, these menus are also called context menus.

A shortcut menu remains hidden until the user activates it, typically by right-clicking a selection, a toolbar, or a taskbar button. The menu is usually displayed at the position of the caret or mouse cursor.

18.1.1.2 The Window Menu

The Window menu (also known as the System menu or Control menu) is a pop-up menu defined and managed almost exclusively by the operating system. The user can open the window menu by clicking the application icon on the title bar or by right- clicking anywhere on the title bar.

The Window menu provides a standard set of menu items that the user can choose to change a window's size or position, or close the application. Items on the window menu can be added, deleted, and modified, but most applications just use the standard set of menu items. An overlapped, pop-up, or child window can have a window menu. It is uncommon for an overlapped or pop-up window not to include a window menu.

When the user chooses a command from the Window menu, the system sends a WM_SYSCOMMAND message to the menu's owner window. In most applications, the window procedure does not process messages from the window menu. Instead, it simply passes the messages to the DefWindowProc function for system-default processing of the message. If an application adds a command to the window menu, the window procedure must process the command.

MENUITEM "&About...", ID_ABOUT END

Clicking on menu item sends a WM_COMMAND message to its parent. WM_COMMAND message contains Menu item ID in the low word of WPARAM and handle in LPARAM.

18.3 Loading Menu

The LoadMenu function loads the specified menu resource from the executable (.exe) file associated with an application instance.

HMENU LoadMenu(

HINSTANCE hInstance , //handle to the instance of the / LPCTSTR _lpMenuName / Menu Name */_ );

hInstance: Handle to the module containing the menu resource to be loaded.

lpMenuName: Pointer to a null-terminated string that contains the name of the menu resource. Alternatively, this parameter can consist of the resource identifier in the low- order word and zero in the high-order word. To create this value, use the MAKEINTRESOURCE macro.

Return Value: If the function succeeds, the return value is a handle to the menu resource. If the function fails, the return value is NULL. To get extended error information, call GetLastError.

18.4 Specify default class Menu

You can specify default class menu for windows by assigning Menu name to the lpszMenuName parameter in window class.

wc. lpszMenuName= (LPCTSTR)IDR_MENU1; …………….. …………….. if(!RegisterClass(&wc)) { return 0; }

18.5 Specify Menu in CreateWindow

Menu can be specifying in hMenu parameter of CreateWindow function. hMenu is the handle of the menu so Menu handle must be specify here rather its name. if the handle of the menu is specified then this will override class window menu.

18.6 Example Application

Now we will practically discuss the menus and Timers by making an application. In this application we will display menu which will be enabled and disabled.

18.6.1 Resource Definition strings

#include “resource.h”

STRINGTABLE DISCARDABLE BEGIN IDS_APP_NAME "Virtual University" IDS_CLASS_NAME "MyWindowClass" END

18.6.2 Resource Definition Icon

IDI_MAIN_ICON ICON DISCARDABLE "VU.ICO"

Icon file name is VU.ICO

18.6.3 Application Menus

IDR_FIRST_MENU MENU DISCARDABLE

BEGIN

POPUP “&File" BEGIN MENUITEM "E&xit", ID_FILE_EXIT END

POPUP "&Timer" BEGIN MENUITEM "&Start", ID_TIMER_START MENUITEM "Sto&p", ID_TIMER_STOP, GRAYED END

KillTimer(hWnd, ID_TIMER); bTimerStarted=FALSE; hOurMenu = GetMenu(hWnd); EnableMenuItem(hOurMenu, ID_TIMER_STOP, MF_BYCOMMAND | MF_GRAYED); EnableMenuItem(hOurMenu, ID_TIMER_START, MF_BYCOMMAND | MF_ENABLED); DrawMenuBar(hWnd); break;

case ID_FILE_EXIT: DestroyWindow(hWnd);

case WM_TIMER: switch(wParam) { case ID_TIMER: ++count; count %= 10; GetClientRect(hWnd, &rect); InvalidateRect(hWnd, &rect, TRUE); break; } break; }

TCHAR msg[10];

case WM_PAINT:

hDC = BeginPaint(hWnd, &ps); wsprintf(msg, "Count: %2d", count); TextOut(hDC, 10, 10, msg, lstrlen(msg)); EndPaint(hWnd, &ps);

break;

case WM_DESTROY: if(bTimerStarted) KillTimer(hWnd, ID_TIMER); PostQuitMessage(0); break;

18.6.7 Keyboard Accelerator

IDR_ACCELERATOR ACCELERATORS DISCARDABLE

BEGIN "P", ID_TIMER_STOP, VIRTKEY, CONTROL, NOINVERT

"S", ID_TIMER_START, VIRTKEY, CONTROL, NOINVERT

"X", ID_FILE_EXIT, VIRTKEY, ALT, NOINVERT

END

IDR_FIRST_MENU MENU DISCARDABLE

BEGIN

POPUP "&File" BEGIN MENUITEM "E&xit\tAlt+X", ID_FILE_EXIT END POPUP "&Timer" BEGIN MENUITEM "&Start\tCtrl+S", ID_TIMER_START MENUITEM "Sto&p\tCtrl+P", ID_TIMER_STOP, GRAYED END END

18.6.8 Message Loop

HACCEL hAccelerators; hAccelerators = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR)); while(GetMessage(&msg, NULL, 0, 0) > 0) { if(!TranslateAccelerator(msg.hwnd, hAccelerators, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }

Summary

In this lecture, we studied about the menus resources and their entry in resource definition file. Using menu accelerators you can use short cut keys to operate menus. At the end we discussed an example application which enables or disable the menus. Menus are used by almost every application except some games or other system tools. Using menus we can watch different facilities or action provided by application.