|
|
|
|
Lesson#18
|
String and Menu Resource
|
|
|
|
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
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
popup
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.
String and Menu Resource 3
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
rightclicking
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.
String and Menu Resource 4
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
String and Menu Resource 5
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 loworder
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;
}
String and Menu Resource 6
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
String and Menu Resource 7
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:
String and Menu Resource 8
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
String and Menu Resource 9
"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.
String and Menu Resource 10
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. |
|
|
|