如何在所有者绘制的MFC CMenu派生菜单中显示一个复选框?

时间:2017-07-24 15:55:54

标签: c++ visual-c++ mfc ownerdrawn cmenu

我有一个继承自MFC CMenu类的菜单类:MyMenu : public CMenu。 MyMenu是从资源文件加载而不是更改为所有者绘制的菜单(使用CodeGuru中的代码示例,该代码示例适用于菜单项的样式)。现在我要激活菜单项左侧的复选框。

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?

我希望出现复选框,但事实并非如此。 首先,我尝试使用来自MSDN的代码示例设置带有SetMenuItemBitmaps(...)的复选框位图:

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

这没有成功。然后我尝试使用基于a page on MSDN about the MENUITEMINFO struct的SetMenuItemInfo调用来设置MENUITEMINFO:

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);

最后,为了确保菜单项实际上可以更改,我添加了一行

subm->ModifyMenu(ID_COPY_ITEM, MF_BYCOMMAND, ID_COPY_ITEM, reinterpret_cast<LPCTSTR>(&menuProperties));

...,这将导致使用指向menuProperties的lpDrawItemStruct-&gt; itemData调用DrawItem:它运行良好。

我的所有者绘制的菜单中仍然没有复选框。我错过了什么?如何向所有者绘制的MFC PopupMenu添加复选框?

1 个答案:

答案 0 :(得分:0)

如果您的菜单是所有者抽奖,您应该执行类似

的操作
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();

DrawItem方法上。

不要忘记在MeasureItem上为可检查的菜单项提供一点保证金:

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() );