README.md

December 5, 2022 ยท View on GitHub

Making a menu using winapi non-mfc

Description

This code shows how to easily and quickly create a menu on a non-mfc window using winapi. I this code is versatile, and extremely useful as I for one hate resources. This example deals with making a "file --> exit" menu. The methods used in making this apply to all menus.

More Info

Submitted On
ByJay
LevelBeginner
User Rating5.0 (40 globes from 8 users)
CompatibilityC++ (general), Microsoft Visual C++
CategoryControls/ Forms/ Dialogs/ Menus
WorldC / C++
Archive File

Source Code

//define any menu ITEMS u might have, in this case "exit"
#define CM_MENU_FILE_EXIT 2001
//place this in LRESULT CALLBACK WindowFunc
HMENU hMenu, hSubMenu;
//create the menu in the WM_CREATE section of LRESULT CALLBACK WindowFunc
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, CM_MENU_FILE_EXIT, "E&xit");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
SetMenu(hwnd, hMenu);
//note - if creating extra menus dont forget to call hSubmenu = CreatePopupMenu(); again or all your "menu items" will just b added to the old menu
//give the "menu item" an action under WM_COMMAND
switch(LOWORD(wParam)) {
case CM_MENU_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
}
//i hope this has been helpful, feel free to comment