Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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.

2 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.

3 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.

4 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.

5 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.

6 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);

7 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.

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

9 Write a program to load a menu in a window #include #include "resource.h" class myframe:public CFrameWnd { public: myframe() { Create(0,"LoadMenu",WS_OVERLAPPEDWINDOW, rectDefault,0,MAKEINTRESOURCE(IDR_MENU1)); } };

10 class myapp:public CWinApp { public: int InitInstance() {myframe *p; p=new myframe; p->ShowWindow(3); m_pMainWnd=p; return 1; } }; myapp a;

11 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.

12 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.

13 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

14 if(!hp) { wc.style=CS_HREDDRAW|CS_VREDDRAW|CS_DBLCL KS; 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); }

15 hwndMainWindow=CreateWindow("my","hello",WS_OVERLAP PEDWINDOW,CW_USEDEFAULT,NULL,NULL,h,NULL); ShowWindow(hwndMainWindow,3); UpdateWindow(hwndMainWindow); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; }

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

17 switch(message) { case WM_CREATE: hmenu=CreatePopupMenu(); for(i=0;i<DINNER_ITEM_COUNT;i++) {AppendMenu(hmenu,MF_STRING|MF_ENABLE,I,lpszDinnerItem s[i]); } #define BUTTON_ID 100 hwndbutton=CreateWindow("BUTTON","dinnermenu", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT, hwnd,(HMENU)BUTTON_ID,h,0);

18 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,hw nd,(HMENU)i,h,NULL); randomize(); return 0;

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

20 Menu Messages


Download ppt "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."

Similar presentations


Ads by Google