none
Ugly face of owner-drawn menu items RRS feed

  • 问题

  • Hi, all,

    Through help of Helen Zhao and msdn, Now I could define a owner-drawn menu item, but its face looks ugly and some other elements are missing. Look the following pictures:

    First, the width of owner-drawn menu item is much shorter than the original one's. Second, the width of the check bitmap is also shorter, and the vertical line between the check bitmap and 'Exit' is missing. 

    All I want is just to make 'Exit' bold with other elements such as the menu item's width, height and layout unchanged

    Any ideas will be appreciated.

    Here is handlers of WM_MEASUREITEM and WM_DRAWITEM and create font function

    VOID WINAPI measureItem(HWND hwnd, LPMEASUREITEMSTRUCT lpmis) { HDC hdc = GetDC(hwnd); HFONT hfntOld = (HFONT)SelectObject(hdc, boldFont); SIZE size; if (!GetTextExtentPoint32(hdc, menuItemLabelString, length, &size)) { MessageBox(hwnd, L"Failed to get label string length", L"Failure", MB_OK); return; } lpmis->itemWidth = size.cx; lpmis->itemHeight = size.cy; SelectObject(hdc, hfntOld); ReleaseDC(hwnd, hdc); } VOID WINAPI drawItem(HWND hwnd, LPDRAWITEMSTRUCT lpdis) { COLORREF clrPrevText, clrPrevBkgnd; HFONT hfntPrev; int x, y; // Set the appropriate foreground and background colors. if (lpdis->itemState & ODS_SELECTED) { clrPrevText = SetTextColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT)); clrPrevBkgnd = SetBkColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHT)); } else { clrPrevText = SetTextColor(lpdis->hDC, GetSysColor(COLOR_MENUTEXT)); clrPrevBkgnd = SetBkColor(lpdis->hDC, GetSysColor(COLOR_MENU)); } // Determine where to draw and leave space for a check mark. x = lpdis->rcItem.left; y = lpdis->rcItem.top; x += GetSystemMetrics(SM_CXMENUCHECK); // Select the font and draw the text. hfntPrev = (HFONT)SelectObject(lpdis->hDC, boldFont); ExtTextOut(lpdis->hDC, x, y, ETO_OPAQUE, &lpdis->rcItem, menuItemLabelString, length, NULL); // Restore the original font and colors. SelectObject(lpdis->hDC, hfntPrev); SetTextColor(lpdis->hDC, clrPrevText); SetBkColor(lpdis->hDC, clrPrevBkgnd); }

     

    HFONT createMenuItemFont(UINT) 

        NONCLIENTMETRICS ncm;
        ncm.cbSize = sizeof(NONCLIENTMETRICS);
        SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0);

        ncm.lfMenuFont.lfWeight = FW_BOLD;
        return CreateFontIndirect(&ncm.lfMenuFont); 

    Note:It seems that setting one of the menu items default could make the item label BOLD, but no more than one

    default menu items is allowed.


    Cheers,

    Hu Jingfei


    潜心研究医学图像


    2012年2月29日 12:04

答案

全部回复