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