Creating Menus Example

//creating menus
#include <windows.h>
#define IDM_MENUITEM1 1
#define IDM_MENUITEM2 2
#define IDM_FILE_QUIT 3
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("myWindowClass");
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("myWindowClass"), TEXT("Creating Menus"), WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 700, 200, NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_CREATE:
//Create handles for different types of menu
HMENU hMenubar;
HMENU hMenu;
//Create handles for context menu
HMENU hContextMenu;
POINT point;
//create empty menu
hMenubar = CreateMenu();
hMenu = CreateMenu();
//Create top level popup menu
AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hMenu,TEXT("&File"));
//Add menu items to pop up window
AppendMenu(hMenu, MF_STRING, IDM_MENUITEM1, TEXT("Item1"));
AppendMenu(hMenu, MF_STRING, IDM_MENUITEM2, TEXT("Item2"));
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenu, MF_STRING, IDM_FILE_QUIT, TEXT("Quit"));
//Assigns a new menu to the specified window.
SetMenu(hwnd, hMenubar);
break;
//responds to main menu messages
case WM_COMMAND:
switch(LOWORD(wParam)) {
case IDM_MENUITEM1:
MessageBeep(MB_ICONINFORMATION);
break;
case IDM_MENUITEM2:
MessageBeep(MB_ICONWARNING);
break;
case IDM_FILE_QUIT:
SendMessage(hwnd, WM_CLOSE, 0, 0);
break;
}
break;
//The WM_INITMENU message is generated when the user clicks an item on the menu bar.
case WM_INITMENU:
{
HMENU selectedMenuID=(HMENU)wParam;//menu ID of selected menu item
}
break;
// WM_MENUSELECT is Sent to a menu's owner window when the user selects a menu item.
case WM_MENUSELECT:
{
UINT uItem = (UINT) LOWORD(wParam); // menu item or submenu index
UINT muFlags = (UINT) HIWORD(wParam); // menu flags
HMENU hmenu = (HMENU) lParam; // handle of menu clicked
}
// The WM_INITMENUPOPUP is sent when the user points to a menu item that opens a submenu
case WM_INITMENUPOPUP:
{
HMENU hMenuPopup = (HMENU) wParam; // handle of submenu
UINT uPos = (UINT) LOWORD(lParam); // submenu item position
BOOL bSystemMenu = (BOOL) HIWORD(lParam); // window menu flag
}
//responds to right mouse click by creating context menu
case WM_RBUTTONUP:
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
// Create context menu
hContextMenu = CreatePopupMenu();
//converts the client coordinates of a specified point to screen coordinates.
ClientToScreen(hwnd, &point);
//add menu iten to context menu
AppendMenu(hContextMenu, MF_STRING, IDM_MENUITEM1, TEXT("Option1"));
AppendMenu(hContextMenu, MF_STRING, IDM_MENUITEM2, TEXT("Option2"));
AppendMenu(hContextMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hContextMenu, MF_STRING, IDM_FILE_QUIT, TEXT("Quit"));
//Displays popup menu
TrackPopupMenu(hContextMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hwnd, NULL);
DestroyMenu(hContextMenu);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}