menus menus is a feature which is common to almost every windows applications. it is the one of the...

20
Menus Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around. Windows does most of the work involved in menu handling, including displaying the menu bar, displaying a drop-down menu when an item on a menu bar is clicked and notifying the application when a menu item is selected. There are basically 3 types of menus in a

Upload: rosamond-shepherd

Post on 18-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

Creating a Menu  There are three ways to create a menu. The easiest way is to create a menu template in your application’s ‘rc’ file using the resource editor.  Once this menu is created it can be loaded into your application’s window by spacifying the resource ID of the menu while creating the window.  Instead of specifying the resource ID while creating the window we may first create the window and then load the menu using CMenu::LoadMenu() function.

TRANSCRIPT

Page 1: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

Menus Menus is a feature which is common to almost every windows

applications. It is the one of the most common user interface

elements around.

Windows does most of the work involved in menu handling,

including displaying the menu bar, displaying a drop-down

menu when an item on a menu bar is clicked and notifying the

application when a menu item is selected.

There are basically 3 types of menus in a windows application:

a system menu, a popup menu and a drop-down menu.

Page 2: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

Contd…. Most applications have a ‘system menu’ containing

commands for restoring, moving, size, minimizing,

maximizing and closing the window.

Another type of the menu is the ‘popup menu’ which can

be popped up anywhere on the screen by clicking the right

mouse button.

The third type of the menu is a ‘drop-down’ menu that

appears as a submenu for the menu items in the menu bar

at the top of the window.

Page 3: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

Creating a Menu There are three ways to create a menu. The easiest way is

to create a menu template in your application’s ‘rc’ file using the resource editor.

Once this menu is created it can be loaded into your application’s window by spacifying the resource ID of the menu while creating the window.

Instead of specifying the resource ID while creating the window we may first create the window and then load the menu using CMenu::LoadMenu() function.

Page 4: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

Contd…. Sometimes it is not possible to determine the menu’s contents

until run-time.

In such cases menus can be created programmatically using

functions like CreateMenu(), InsertMenu(), appendMenu(),

etc., all of which are members of the MFC CMenu class. The

functionality of creating and handling a menu has been

implemented in this class.

The third and more difficult method of creating a menu is to fill

a series of data structure defining the menu’s contents and then

create the menu using CMenu::LoadMenuIndirect() function.

Page 5: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

MENU-ALTERING FUNCTIONS

CreateMenu() :-   Creates a new menu, ready to add items. CreatePopupMenu():-   Creates a new popup menu, ready to

receive items. SetMenu()  :- Attaches menu to a window; often used with LoadMenu():- to  switch between several menus used by the

program. AppendMenu() :-Adds a new menu item or popup to the end

of a menu. InsertMenu():-  Inserts a new menu item or popup into a

menu/popup menu. DeleteMenu():- Removes a menu item from a menu or popup

menu.

Page 6: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

Contd… DestroyMenu() :-Deletes an entire menu, removing it from

memory; only needed if the menu was loaded but not attached

to a window.

DrawMenuBar() :-Draws the menu bar (in main menu area

below window caption),  making any changes visible.

LoadMenu() :- Loads a menu from the program's resource

data, ready tos be attached to a Window with SetMenu().

EnableMenuItem():- is used to enable or disable a specified

menu item in  the menu:  

EnableMenuItem(hmenu, idEnableItem, ActionFlag);

Page 7: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

Basic sequence

1. Use CreateMenu() to create a new, empty menu

with which to start;returns a handle to the new

menu.  

2. Use AppendMenu() and/or InsertMenu() to add

menu items as needed.  

3. Use SetMenu() to attach the menu to a window, so

it can be used.

Page 8: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

Creating a menuSwitch(message){case: WM_CREATE:int hm;Hm=CreateMenu();AppendMenu(hm, MF_STRING|MF_ENABLED))}SetMenu(hwnd, hm);

Page 9: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

Write a program to load a menu in a window

#include<afxwin.h>#include "resource.h"class myframe:public CFrameWnd{public: myframe()

{Create(0,"LoadMenu",WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));

}};

Page 10: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

class myapp:public CWinApp{public:

int InitInstance(){ myframe *p;

p=new myframe;p->ShowWindow(3);m_pMainWnd=p;return 1;

}}; myapp a;

Page 11: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

Creating a popup menus Building a popup menu is similar like creating the menu.

We just use the CreatePopupMenu() function instead CreateMenu().

We can organizing the items of the popup menu into the logical groups. This can be done by specifying the MF_SEPARATOR flag in an AppendMenu() call.

Having filled out our popup menu, we can display it and obtain user selections from it by calling the TrackPopupMenu() function.

Page 12: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

Basic sequence If the main menu contains popup menus, the popups are

created separately, and then attached to the menu as follows:  

1. Use CreatePopupMenu() to create a new, empty popup menu. Returns a handle to the popup menu.  

2. Use AppendMenu() or InsertMenu() to add menu items to popup as needed.  

3. Use AppendMenu() or InsertMenu() to add the finished popup menu to the main menu.

Page 13: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

Creating the popup menu#include"resource.h"#include"afxwin.h"#include"windows.h"

HWND hwnd;long WindowProc(HWND hwnd, UINT message, WPARAM wp,

LPARAM lp);int _stdcall WinMain(HINSTANCE h, HINSTANCE hp, char *r,

int i){

MSG msg; WNDCLASS wc;s

Page 14: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

if(!hp) {

wc.style=CS_HREDDRAW|CS_VREDDRAW|CS_DBLCLKS;

wc.lpfnWndProc=WindowProc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hInstance=h;

wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

wc.hCursor=NULL; wc.lpszClassName="my"; RegisterClass(&wc);

}

Page 15: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

hwndMainWindow=CreateWindow("my","hello",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,NULL,NULL,h,NULL);

ShowWindow(hwndMainWindow,3); UpdateWindow(hwndMainWindow); while(GetMessage(&msg,NULL,0,0))

{ TranslateMessage(&msg);DispatchMessage(&msg);

} return 0; }

Page 16: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

long WindowProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)

{static LPSTR lpszDinnerItem[]={

"soup", "salad","vegitable", "wine" };

#define DINNER_ITEM_COUNT(sizeof(lpszDinnerItem)/sizeof(LPSTR))#define STATIC_COUNT 10

static HMENU hmenu;static HWND hwndbutton;static HWND hwndstatic(STATIC_COUNT);static int index=0;int i;

Page 17: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

switch(message){case WM_CREATE:

hmenu=CreatePopupMenu();for(i=0;i<DINNER_ITEM_COUNT;i++)

{AppendMenu(hmenu,MF_STRING|MF_ENABLE,I,lpszDinnerItems[i]);

}#define BUTTON_ID 100hwndbutton=CreateWindow("BUTTON","dinnermenu",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, hwnd,(HMENU)BUTTON_ID,h,0);

Page 18: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

for(i=0;i<STATIC_COUNT;i++){

hwndstatic[i]=CreateWindow("STATIC","static menu",WS_CHILD|WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,hwnd,(HMENU)i,h,NULL);

randomize();return 0;

Page 19: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

case WM_COMMAND:

if(wparam==BUTTON_ID)TrackPopupMenu(hmenu,0,random(GetSystemMetrics(SM_CXSCREEN)),random(GetSystemMetrics(SM_CYSCREEN)),0,hwnd,NULL);

}case WM_DESTROY:

PostQuitMessage(0);return 0;

}return DefWindowProc(hwnd,message,wp,lp);

}

Page 20: Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows

Menu Messages