winapikeymenubarownerdrawn

Key access to menu bar with ownerdraw


Since I use ownerdraw for the menu bar, the key access does not work anymore. Without ownerdraw Alt+F works for the item "&File", with ownerdraw nothing happens. What is missing here? When I process WM_MENUSELECT, wParam gives no information.

Menu creation:

HMENU hm;
HMENU winmenu = CreateMenu ();

MENUINFO mi;
mi.cbSize = sizeof (mi);
mi.fMask = MIM_BACKGROUND;
mi.hbrBack = (HBRUSH)CreateSolidBrush (RGB (70, 70, 70));
SetMenuInfo (winmenu, &mi);
UINT barFlags = MF_POPUP|MF_OWNERDRAW;

hm = CreatePopupMenu ();
AppendMenuW (winmenu, barFlags, (UINT)hm, L"&File");
AppendMenuW (hm, MF_STRING, IDM_OPENFILE, L"&Open");
...

SetMenu (hwnd, winmenu);
DrawMenuBar (hwnd);

Ownerdraw processing:

case WM_MEASUREITEM:
{
    LPMEASUREITEMSTRUCT lpmi = (LPMEASUREITEMSTRUCT)lParam;
    HDC hdc = GetDC (hwnd);
    SIZE size;
    GetTextExtentPoint32W (hdc, (PWCHAR)lpmi->itemData, wcslen ((PWCHAR)lpmi->itemData), &size);
    lpmi->itemWidth = size.cx;
    lpmi->itemHeight = size.cy;
    ReleaseDC (hwnd, hdc);
    break;
}
case WM_DRAWITEM:
{
    LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
    if (lpdis->CtlType == ODT_MENU)
    {
        HDC hdc = lpdis->hDC;
        HPEN oldPen = (HPEN) SelectObject(hdc, GetStockObject (DC_PEN));
        HBRUSH oldBrush = (HBRUSH) SelectObject(hdc, GetStockObject (DC_BRUSH));
        SetBkMode (hdc, TRANSPARENT);

        if ((lpdis->itemState & ODS_HOTLIGHT) || (lpdis->itemState & ODS_SELECTED))
        {
            SetDCPenColor (hdc, RGB (70, 20, 20));
            SetDCBrushColor (hdc, RGB (70, 20, 20));
            SetTextColor (hdc, RGB (255, 255, 255));
        }
        else
        {
            SetDCPenColor (hdc, RGB (70, 70, 70));
            SetDCBrushColor (hdc, RGB (70, 70, 70));
            SetTextColor (hdc, RGB (255, 255, 255));
        }
        Rectangle (hdc, lpdis->rcItem.left, lpdis->rcItem.top, lpdis->rcItem.right, lpdis->rcItem.bottom);
        DrawTextW (hdc, (PWCHAR)lpdis->itemData, -1, &lpdis->rcItem,
            ((lpdis->itemState & ODS_NOACCEL) ? DT_HIDEPREFIX : 0)
            |DT_SINGLELINE|DT_CENTER|DT_VCENTER);

        SelectObject (hdc, oldPen);
        SelectObject (hdc, oldBrush);
    }
    break;
}

Solution

  • Per MSDN documentation:

    Owner-Drawn Menus and the WM_MENUCHAR Message

    Menus other than owner-drawn menus can specify a menu mnemonic by inserting an underscore next to a character in the menu string. This allows the user to select the menu by pressing ALT and pressing the menu mnemonic character. In owner-drawn menus, however, you cannot specify a menu mnemonic in this manner. Instead, your application must process the WM_MENUCHAR message to provide owner-drawn menus with menu mnemonics.