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

CreateMenu

CreateMenu() creates a new menu and returns the handle.

Syntax

HMENU WINAPI CreateMenu();

Parameters

None

Return value

Handle to a newly created menu (HMENU).

AppendMenu

Appends a new item to the end of the specified menu bar, drop-down menu, submenu, or shortcut menu. You can use this function to specify the content, appearance, and behavior of the menu item.

Syntax

BOOL WINAPI AppendMenu(
        HMENU hMenu,
        UINT uFlags,
        UINT_PTR uIDNewItem,
        LPCTSTR lpNewItem
);

Parameters

  • hMenu (HMENU) A handle to the menu bar, drop-down menu, submenu, or shortcut menu to be changed.
  • uFlags (UINT) Controls the appearance and behavior of the new menu item. This parameter can be a combination of the following values. Value Meaning MF_BITMAP, MF_CHECKED, MF_DISABLED, MF_ENABLED, MF_GRAYED, MF_MENUBARBREAK, MF_MENUBREAK, MF_OWNERDRAW, MF_POPUP, MF_SEPARATOR, MF_STRING,MF_UNCHECKED
  • uIDNewItem (UINT_PTR) The identifier of the new menu item or, if the uFlags parameter is set to MF_POPUP, a handle to the drop-down menu or submenu.
  • lpNewItem (LPCTSTR) The content of the new menu item. The interpretation of lpNewItem depends on whether the uFlags parameter includes the following values. MF_BITMAP, MF_OWNERDRAW, MF_STRING

Return value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero/FALSE. To get extended error information, call GetLastError.

CreateMenu AppendMenu Demo Application screen shot

#include <windows.h>
#define IDM_FILE_NEW 1000
#define IDM_FILE_OPEN 1001
#define IDM_FILE_CLOSE 1002
#define IDM_FILE_EXIT 1003
#define IDM_HELP_ABOUT 1004
LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT message, 
10                           WPARAM wParam,
11                           LPARAM lParam);
12 
13  int APIENTRY WinMain(HINSTANCE hInstance,
14                       HINSTANCE hPrevInstance,
15                       LPSTR     lpCmdLine,
16                       int       nCmdShow)
17  {
18    WNDCLASS wc;
19    MSG msg;
20    HWND hWnd;
21    HMENU hMenu;
22 
23    ZeroMemory(&wc, sizeof(WNDCLASS));
24 
25    wc.style           = CS_HREDRAW | CS_VREDRAW;
26    wc.lpfnWndProc     = (WNDPROC)WndProc;
27    wc.hInstance       = hInstance;
28    wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
29    wc.hbrBackground   = (HBRUSH)(COLOR_BACKGROUND);
30    wc.lpszClassName   = (LPCTSTR)"MyDynMenuClass";
31 
32 
33    RegisterClass(&wc);
34 
35     hWnd = CreateWindow("MyDynMenuClass", "Dynamic Menu", WS_OVERLAPPEDWINDOW,
36        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
37 
38    ShowWindow(hWnd, nCmdShow);
39    UpdateWindow(hWnd);
40 
41    while (GetMessage(&msg, NULL, 0, 0)) 
42    {
43      DispatchMessage(&msg);
44    }
45    return 0;
46  }
47 
48  HMENU hMainMenu;
49  HMENU hFileMenu;
50  HMENU hHelpMenu;
51 
52  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
53  {
54    int nSel;
55    char * caption_menu[] = { "New", "Open", "Close", "Exit", "About"};
56    char msg[50];
57    switch (message) 
58    {
59      case WM_CREATE:
60      {
61        /* Creating our dynamic menu */
62        hMainMenu = CreateMenu();
63        hFileMenu = CreateMenu();
64        hHelpMenu = CreateMenu();
65 
66        AppendMenu(hFileMenu, MF_BYPOSITION | MF_STRING, IDM_FILE_NEW, "New");
67        AppendMenu(hFileMenu, MF_BYPOSITION | MF_STRING, IDM_FILE_OPEN, "Open");
68        AppendMenu(hFileMenu, MF_BYPOSITION | MF_STRING, IDM_FILE_CLOSE, "Close");
69        AppendMenu(hFileMenu, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
70        AppendMenu(hFileMenu, MF_BYPOSITION | MF_STRING, IDM_FILE_EXIT, "Exit");
71 
72        AppendMenu(hHelpMenu, MF_BYPOSITION | MF_STRING,IDM_HELP_ABOUT, "About");
73        AppendMenu(hMainMenu,MF_POPUP | MF_STRING,(UINT)hFileMenu, "File");
74        AppendMenu(hMainMenu,MF_POPUP | MF_STRING,(UINT)hHelpMenu, "Help");
75        
76        SetMenu(hWnd, hMainMenu);
77        break;
78      }
79      case WM_COMMAND:
80      {
81        if(wParam >= IDM_FILE_NEW && wParam <= IDM_HELP_ABOUT) {
82          sprintf(msg, "Clicked on %s", caption_menu[wParam -IDM_FILE_NEW]);
83          MessageBox(hWnd, msg, "Main Menu", MB_OK);
84        }
85        DefWindowProc(hWnd, message, wParam, lParam);
86        break;
87      }
88      case WM_DESTROY:
89      {
90        /* Free menu handles */
91        DestroyMenu(hHelpMenu);
92        DestroyMenu(hFileMenu);
93        DestroyMenu(hMainMenu);
94        PostQuitMessage(0);
95        break;
96      }
97      default:
98        return DefWindowProc(hWnd, message, wParam, lParam);
99     }
100     return 0;
101  }

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 Mon 28 Apr/2014 03:41:57(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.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  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.