由于我对菜单栏使用了ownerdraw,因此按键访问不再起作用。没有ownerdraw时,Alt+F适用于项目“&File”,而使用ownerdraw时,什么也不会发生。这里缺少什么?当我处理 WM_MENUSELECT 时,wParam 没有提供任何信息。
创建菜单:
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处理:
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;
}
根据 MSDN 文档:
自绘菜单以外的菜单可以通过在菜单字符串中的字符旁边插入下划线来指定菜单助记符。这允许用户通过按 ALT 并按菜单助记符来选择菜单。然而,在自绘菜单中,您不能以这种方式指定菜单助记符。 相反,您的应用程序必须处理
消息以提供所有者绘制的菜单和菜单助记符。WM_MENUCHAR