Reputation: 77
I have a menu-class which inherits from the MFC CMenu class: MyMenu : public CMenu
.
MyMenu is loaded from a resource file and than changed to be an owner-drawn menu (using the code example from CodeGuru, which works well for styling of the menu-items). Now I want to activate the checkbox left to the menu-item.
MyMenu menu;
menu.LoadMenu(IDR_MYCONTEXT_MENU);
MyMenu* subm = ef_cast<MyMenu*>(menu.GetSubMenu(0));
if (subm == nullptr) return;
subm->ChangeToOwnerDraw(*subm);
subm->CheckMenuItem(ID_COPY_ITEM, m_ItemCopied ? MF_CHECKED : MF_UNCHECKED);
subm->CheckMenuItem(ID_COPY_ITEM, MF_CHECKED); //Force visibility?
I expected the checkbox to appear, but it didn't. First I tried to set the checkbox bitmaps with SetMenuItemBitmaps(...) using a code example from MSDN:
int commandID = ID_COPY_ITEM;
CBitmap checkedBitmap;
checkedBitmap.Attach(MyMenu::GetMyCheckBitmaps(CHECK));
CBitmap uncheckedBitmap;
uncheckedBitmap.Attach(MyMenu::GetMyCheckBitmaps(UNCHECK));
SetMenuItemBitmaps(*subm, commandID, MF_BYCOMMAND, uncheckedBitmap, checkedBitmap);
subm->SetMenuItemBitmaps(commandID, MF_BYCOMMAND, &uncheckedBitmap, &checkedBitmap); //Same as previous line
That didn't work out. Then I tried to set the MENUITEMINFO using a call to SetMenuItemInfo, based on a page on MSDN about the MENUITEMINFO struct:
MENUITEMINFO mItemInfo{};
mItemInfo.cbSize = sizeof(MENUITEMINFO);
mItemInfo.fMask |= MIIM_CHECKMARKS | MIIM_STATE;
mItemInfo.fState |= MFS_CHECKED | MFS_DEFAULT;
mItemInfo.hbmpChecked = MyMenu::GetMyCheckBitmaps(CHECK);
mItemInfo.hbmpUnchecked = MyMenu::GetMyCheckBitmaps(UNCHECK);
subm->SetMenuItemInfo(commandID, &mItemInfo, FALSE);
Finally, to be sure that the menu-item actually can be changed, I added a line
subm->ModifyMenu(ID_COPY_ITEM, MF_BYCOMMAND, ID_COPY_ITEM, reinterpret_cast<LPCTSTR>(&menuProperties));
..., which will cause the DrawItem to be called with lpDrawItemStruct->itemData pointing to the menuProperties: that works well.
Still there is no checkbox in my owner-drawn menu. What am I missing? How do a add a checkbox to a owner-drawn MFC PopupMenu?
Upvotes: 1
Views: 1006
Reputation: 4335
If your menu is owner draw, you should do something like
LOGFONT lf;
// if font defined by you
if(i_have a_font)
{
lf= your_log_font;
}
else
{
NONCLIENTMETRICS ncm;
ncm.cbSize= sizeof(ncm);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);
lf= ncm.lfMenuFont;
}
CSize sizeImage = CSize(abs(lf.lfHeight), abs(lf.lfHeight))
if ((lpDrawItemStruct->itemState & ODS_CHECKED))
{
CRect rectImage= CRect(lpDrawItemStruct->rcItem);
CPoint ptImage(0,
rectImage.top +(rectImage.Height() - sizeImage.cy) / 2 +((rectImage.Height() - sizeImage.cy) % 2));
//TODO! Need to fix; currently drawing checks on situations it should draw radios!
CMenuImages::SetColor(CMenuImages::ImageBlack, clrText);
MENUITEMINFO mii;
ZeroMemory(&mii, sizeof (mii));
mii.cbSize= sizeof(mii);
mii.fMask= MIIM_TYPE;
::GetMenuItemInfo(this->m_hMenu, lpDrawItemStruct->itemID, MF_BYCOMMAND, &mii);
if(mii.fType & MFT_RADIOCHECK)
CMenuImages::Draw(pDC,CMenuImages::IdRadio, ptImage, CMenuImages::ImageBlack, sizeImage);
else
CMenuImages::Draw(pDC,CMenuImages::IdCheck, ptImage, CMenuImages::ImageBlack, sizeImage);
}
pDC->ReleaseOutputDC();
on your DrawItem
method.
Don't forget to give a little margin on your MeasureItem
for checkable menu-items:
CDC dc;
dc.Attach( GetDC(NULL));
CSize size;
LOGFONT lf;
// if font defined by you
if(i_have a_font)
{
lf= your_log_font;
}
else
{
NONCLIENTMETRICS ncm;
ncm.cbSize= sizeof(ncm);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);
lf= ncm.lfMenuFont;
}
CFont font;
VERIFY(font.CreateFontIndirect(&lf));
CFont* pOldFont= dc.SelectObject(&font);
// text is a CString containing the text of your menu. I made it enter into the function by passing it through the field `itemData` of the parameter `LPMEASUREITEMSTRUCT lpMeasureItemStruct`
CSize size= dc.GetTextExtent(text);
dc.SelectObject(&pOldFont);
// CHOOSE WHAT BEST FITS YOU
size.cx+= (2 * abs(lf.lfHeight)); // Margin for Check Mark at Left and Margin for Popup Arrow at Right
// OR
size.cx+= (2* SM_CXMENUCHECK); // Margin for Check Mark at Left and Margin for Popup Arrow at Right
lpMeasureItemStruct->itemWidth= size.cx; <= HERE IS THE LINE ASSIGNING THE NEW WIDTH INCLUDING THE MARGIN
ReleaseDC( NULL, dc.Detach() );
Upvotes: 0