String and Menu Resource - Computer Programming - Lecture Notes, Study notes of Computer Engineering and Programming

String and Menu Resource, Menu Handles, Menus, State of Menu Items, Menu Resource Definition Statement, Loading Menu, Specify default class Menu, Specify Menu in CreateWindow. As you can see in this file, how descriptive above mentioned points are explained in this lecture of computer programming. VU is one of best university for computer science in our country.

Typology: Study notes

2011/2012

Uploaded on 11/06/2012

ahsen
ahsen 🇵🇰

4.6

(88)

84 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 18
18.1 MENUS 2
18.1.1 MENU BAR AND MENUS 2
18.1.1.1 SHORT CUT MENUS 3
18.1.1.2 THE WINDOW MENU 3
18.1.2 MENU HANDLES 4
18.1.3 STATE OF MENU ITEMS 4
18.2 MENU RESOURCE DEFINITION STATEMENT 4
18.3 LOADING MENU 5
18.4 SPECIFY DEFAULT CLASS MENU 5
18.5 SPECIFY MENU IN CREATEWINDOW 6
18.6 EXAMPLE APPLICATION 6
18.6.1 RESOURCE DEFINITION STRINGS 6
18.6.2 RESOURCE DEFINITION ICON 6
18.6.3 APPLICATION MENUS 6
18.6.4 APPLICATION WINDOW CLASS 7
18.6.5 CREATEWINDOW 7
18.6.6 WINDOW PROCEDURE 7
18.6.7 KEYBOARD ACCELERATOR 8
18.6.8 MESSAGE LOOP 9
SUMMARY 9
EXERCISES 10
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download String and Menu Resource - Computer Programming - Lecture Notes and more Study notes Computer Engineering and Programming in PDF only on Docsity!

  • Chapter
  • 18.1 M ENUS
  • 18.1.1 M ENU BAR AND M ENUS
  • 18.1.1.1 S HORT CUT M ENUS
  • 18.1.1.2 THE W INDOW M ENU
  • 18.1.2 M ENU HANDLES
  • 18.1.3 S TATE OF MENU ITEMS
  • 18.2 M ENU RESOURCE DEFINITION S TATEMENT
  • 18.3 LOADING M ENU
  • 18.4 S PECIFY DEFAULT CLASS M ENU
  • 18.5 S PECIFY M ENU IN CREATE WINDOW
  • 18.6 EXAMPLE APPLICATION
  • 18.6.1 RESOURCE DEFINITION STRINGS
  • 18.6.2 RESOURCE DEFINITION ICON
  • 18.6.3 APPLICATION M ENUS
  • 18.6.4 APPLICATION W INDOW CLASS
  • 18.6.5 CREATE W INDOW
  • 18.6.6 W INDOW P ROCEDURE
  • 18.6.7 KEYBOARD ACCELERATOR
  • 18.6.8 M ESSAGE L OOP
  • S UMMARY
  • EXERCISES

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.

An application can use the GetSystemMenu function to create a copy of the default window menu to modify. Any window that does not use the GetSystemMenu function to make its own copy of the window menu receives the standard window menu.

18.1.2 Menu Handles

The system generates a unique handle for each menu. A menu handle is a value of the HMENU type. An application must specify a menu handle in many of the menu functions. You receive a handle to a menu bar when you create the menu or load a menu resource.

To retrieve a handle to the menu bar for a menu that has been created or loaded, use the GetMenu function. To retrieve a handle to the submenu associated with a menu item, use the GetSubMenu or GetMenuItemInfo function. To retrieve a handle to a window menu, use the GetSystemMenu function.

18.1.3 State of Menu Items

Following are the states of Menu items:

  • Checked (MF_CHECKED)
  • Unchecked (MF_UNCHECKED)
  • Enabled (MF_ENABLED)
  • Disabled (MF_DISABLED)
  • Grayed (MF_GRAYED)
  • Separator (MF_SEPARATOR)
  • Hilight (MF_HILIGHT)

18.2 Menu Resource Definition Statement

IDR_MY_MENU MENU DISCARDABLE

BEGIN

POPUP "&Tools“ BEGIN MENUITEM "Write &Text", ID_TOOLS_WRITE_TEXT, GRAYED MENUITEM SEPARATOR POPUP "&Draw" BEGIN MENUITEM "&Rectangle", ID_TOOLS_DRAW_RECTANGLE MENUITEM "&Circle", ID_TOOLS_DRAW_CIRCLE, CHECKED MENUITEM "&Ellipse", ID_TOOLS_DRAW_ELLIPSE END MENUITEM SEPARATOR MENUITEM "&Erase All", ID_TOOLS_ERASE_ALL, INACTIVE END

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; }

END

18.6.4 Application Window Class

#define BUFFER_SIZE 128

TCHAR windowClassName[BUFFER_SIZE]; LoadString(hInstance, IDS_CLASS_NAME, windowClassName, BUFFER_SIZE); wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MAIN_ICON)); wc.lpszMenuName = MAKEINTRESOURCE(IDR_FIRST_MENU); wc.lpszClassName = windowClassName

18.6.5 CreateWindow

#define BUFFER_SIZE 128 TCHAR windowName[BUFFER_SIZE]; … … … LoadString(hInstance, IDS_APP_NAME, windowName, BUFFER_SIZE); hWnd = CreateWindow(windowClassName, windowName, ...

18.6.6 Window Procedure

static int count; static BOOL bTimerStarted;

...... case WM_CREATE: count=0; bTimerStarted=FALSE

case WM_COMMAND: switch( LOWORD(wParam) ) { case ID_TIMER_START: SetTimer(hWnd, ID_TIMER, 1000, NULL); bTimerStarted=TRUE; hOurMenu = GetMenu(hWnd); EnableMenuItem(hOurMenu, ID_TIMER_START, MF_BYCOMMAND | MF_GRAYED); EnableMenuItem(hOurMenu, ID_TIMER_STOP, MF_BYCOMMAND | MF_ENABLED); DrawMenuBar(hWnd);

Case ID_TIMER_STOP:

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

Exercises

  1. Show a popup menu whenever the mouse right button is up inside the client area. The pop-up menu should contain at least three items.
  2. Using the mouse messages draw a line which starts when a mouse left button is down and end when the mouse left button is up. During the mouse pressed state if ESC key is pressed the popup menu should be displayed which include menu item Exit. If user press on the exit button process must be cancelled. Use the PeekMessage function and filter the mouse messages only, for this you will have to create another mouse loop that will be created when the mouse left button is down and ends when mouse left button is up.