Creating Owner-Drawn Menu Example

#include <windows.h> #define WINVER 0x0501 #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) { MSG msg; WNDCLASS wc; ZeroMemory(&wc, sizeof(WNDCLASS)); wc.lpszClassName = TEXT("Menu Demonstration"); wc.hInstance = hInstance; wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(NULL, IDC_ARROW); RegisterClass(&wc); CreateWindow( wc.lpszClassName, TEXT("Owner Drawn Menu"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 350, 180, NULL, NULL, hInstance, NULL); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_CREATE: { // Create menu bar HMENU Bar = CreateMenu(); HMENU File = CreateMenu(); HMENU Edit = CreateMenu(); AppendMenu(Bar,MF_POPUP,(UINT)File,TEXT("File")); AppendMenu(Bar,MF_POPUP,(UINT)Edit,TEXT("Edit")); // Create owner drawn menu items AppendMenu(File, MF_OWNERDRAW | MF_STRING, IDM_MENUITEM1, TEXT("Item 1")); AppendMenu(File, MF_OWNERDRAW | MF_STRING, IDM_MENUITEM2, TEXT("Item 2")); AppendMenu(Edit, MF_STRING, IDM_FILE_QUIT, TEXT("Quit")); SetMenu(hwnd,Bar); break; } // Sent when an owner drawn menu is created case WM_MEASUREITEM: { LPMEASUREITEMSTRUCT lpmis; lpmis = (LPMEASUREITEMSTRUCT)lParam; switch(lpmis->itemID) { case IDM_MENUITEM1: lpmis->itemWidth = 120; lpmis->itemHeight = 50; break; case IDM_MENUITEM2: lpmis->itemWidth = 120; lpmis->itemHeight = 70; break; } return TRUE; } // Draw owner drawn menu items case WM_DRAWITEM: { LPDRAWITEMSTRUCT lpdis; RECT rect; HBRUSH bkBrush; HBRUSH oldBrush; lpdis = (LPDRAWITEMSTRUCT)lParam; // Draw background if(lpdis->itemState & ODS_SELECTED) { SetTextColor(lpdis->hDC,RGB(255,255,255)); bkBrush = CreateSolidBrush(RGB(0,0,160)); } else { SetTextColor(lpdis->hDC,RGB(0,0,0)); bkBrush = CreateSolidBrush(RGB(255,255,255)); } FillRect(lpdis->hDC, &lpdis->rcItem, bkBrush); DeleteObject(bkBrush); // Draw border bkBrush = CreateSolidBrush(RGB(255,255,0)); oldBrush = (HBRUSH)SelectObject(lpdis->hDC, bkBrush); Rectangle(lpdis->hDC, lpdis->rcItem.left + 2, lpdis->rcItem.top + 2, lpdis->rcItem.right - 2, lpdis->rcItem.bottom - 2); SelectObject(lpdis->hDC, oldBrush); DeleteObject(bkBrush); // Draw menu text rect = lpdis->rcItem; rect.left += 8; SetBkMode(lpdis->hDC,TRANSPARENT); switch(lpdis->itemID) { case IDM_MENUITEM1: DrawText(lpdis->hDC, TEXT("Item 1"), -1, &rect, DT_SINGLELINE | DT_VCENTER); break; case IDM_MENUITEM2: DrawText(lpdis->hDC, TEXT("Item 2"), -1, &rect, DT_SINGLELINE | DT_VCENTER); break; } return TRUE; } case WM_COMMAND: switch(LOWORD(wParam)) { case IDM_MENUITEM1: MessageBox(hwnd, TEXT("Menu Item 1"), TEXT("Owner Drawn Menu"), MB_OK); break; case IDM_MENUITEM2: MessageBox(hwnd, TEXT("Menu Item 2"), TEXT("Owner Drawn Menu"), MB_OK); break; case IDM_FILE_QUIT: DestroyWindow(hwnd); break; } break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hwnd, uMsg, wParam, lParam); }