积极答复者
Ugly face of owner-drawn menu items

问题
-
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
潜心研究医学图像
- 已编辑 Hu Jingfei 2012年3月1日 13:56
答案
-
Please refer:http://download.csdn.net/detail/fhao840911/212003
Here is demo code, I hope that could helpfull for you.Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
- 已标记为答案 Hu Jingfei 2012年3月7日 6:49
全部回复
-
Please refer:http://download.csdn.net/detail/fhao840911/212003
Here is demo code, I hope that could helpfull for you.Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
- 已标记为答案 Hu Jingfei 2012年3月7日 6:49
-
Thanks for your prompt reply. And I run the code you suggested, but still it isn't satisfactory. Here is the result of your recommended code:
But I wish an effect like that:
Only the font of Open are bold, others remain unchanged. This effect is when you right click on the excel files(any other types of files are also suitable), windows will popup a context menu. So, Is there another way to accomplish the target?
Cheers,
Hu Jingfei
潜心研究医学图像
- 已编辑 Hu Jingfei 2012年3月1日 1:44 Repair the grammar errors
-
sir,
how about these samples on CodeProject. these articles have demenstrated sample routines of owner-drawn menus. please have a look.
http://www.codeproject.com/Articles/2354/Owner-Drawn-Menu-with-Icons-Titles-and-Shading
http://www.codeproject.com/Articles/2869/Owner-drawn-menu-with-bitmaps-icons-and-colors
http://www.codeproject.com/Articles/7503/An-examination-of-menus-from-a-beginner-s-point-of
Please mark this reply as answer if it helps you! Thanks for your cooperation! Good Luck to you.