





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 9
This page cannot be seen from the preview
Don't miss anything!






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.
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
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.
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.
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.
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; }
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.
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.
#include “resource.h”
STRINGTABLE DISCARDABLE BEGIN IDS_APP_NAME "Virtual University" IDS_CLASS_NAME "MyWindowClass" END
Icon file name is VU.ICO
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;
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
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); } }
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.