EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+

#Previous question #Index of VC++/WIN32/MFC Questions #Next question
Menu items are vital part when dealing with a big functional application. Application can have large numbers of sections and categories. Menu resources makes it possible to link different sections together. User can navigate different subsections ans can access different dialogs. Menu items are static resources. they can be constructed by resource editor. Insert new menu resource and give a name of the menu. Now add sub items and add IDs.


LoadMenu() loads menu resource from executable and returns the handle to the menu.




Name of the menu resource.

Return value

Handle to a newly loaded menu (HMENU).

Menu Resource

Menu resource can be added using Insert -> Resource -> Menu in VC++ Editor. We inserted a menu resource named as IDR_MENU1 and then added this resource in project.

Add menu items, Insert -> Resource -> Menu in VC++ Editor

We added a top menu named as "File". We added additional sub menu items under this menu item. We need to add unique identities for each menu items.

Menu Demo output

LoadMenu AppendMenu Demo Application screen shot

Menu Demo code

We can load a menu resource and set it to a main window in three ways-

  • Load Menu and supply handle to CreateWindow() as an argument,
  • Load Menu and set this menu to the window using SetMenu
  • Load Menu and set this menu to the window during window creation (WM_CREATE)
Note: Our example has all three options. We do not require all three. Thus use any one while testing this demo.

#include <windows.h>
#include "resource.h"
HMENU hMainMenu;
                         UINT message, 
                         WPARAM wParam,
                         LPARAM lParam);
10  int APIENTRY WinMain(HINSTANCE hInstance,
11                       HINSTANCE hPrevInstance,
12                       LPSTR     lpCmdLine,
13                       int       nCmdShow)
14  {
15    WNDCLASS wc;
16    MSG msg;
17    HWND hWnd;
18    HMENU hMenu;
20    ZeroMemory(&wc, sizeof(WNDCLASS));
22    wc.style           = CS_HREDRAW | CS_VREDRAW;
23    wc.lpfnWndProc     = (WNDPROC)WndProc;
24    wc.hInstance       = hInstance;
25    wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
26    wc.hbrBackground   = (HBRUSH)(COLOR_BACKGROUND);
27    wc.lpszClassName   = (LPCTSTR)"MyMenuClass";
30    RegisterClass(&wc);
31    hMainMenu = LoadMenu(MAKEINTRESOURCE(IDR_MENU1));
32    hWnd = CreateWindow("MyMenuClass", "LoadMenu", WS_OVERLAPPEDWINDOW,
33        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, hMainMenu, hInstance, NULL);
34    SetMenu(hWnd, hMainMenu);
35    ShowWindow(hWnd, nCmdShow);
36    UpdateWindow(hWnd);
38    while (GetMessage(&msg, NULL, 0, 0)) 
39    {
40      DispatchMessage(&msg);
41    }
42    DestroyMenu(hMainMenu);
43    return 0;
44  }
49  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
50  {
52    switch (message) 
53    {
54      case WM_CREATE:
55      {
56        /* Load our dynamic menu */
57        hMainMenu = LoadMenu(MAKEINTRESOURCE(IDR_MENU1));
58        SetMenu(hWnd, hMainMenu);
59        break;
60      }
61      case WM_COMMAND:
62      {
63        DefWindowProc(hWnd, message, wParam, lParam);
64        break;
65      }
66      case WM_DESTROY:
67      {
68        /* Free menu handles */
69        DestroyMenu(hMainMenu);
70        PostQuitMessage(0);
71        break;
72      }
73      default:
74        return DefWindowProc(hWnd, message, wParam, lParam);
75     }
76     return 0;
77  }

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Tue 29 Apr/2014 16:15:05(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.


Your message goes here:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.