重载CMFCRibbonButton

来源:互联网 发布:淘宝10元麦克风 编辑:程序博客网 时间:2024/05/21 15:38

由 Oliver Nie?lein 在 2012/5/24 0:49 发送
Hello,

To be perfect the DrawImage-Method should look like this:

virtual void DrawImage (CDC* pDC, RibbonImageType type, CRect rectImage)
{
    ASSERT_VALID (this);
    ASSERT_VALID (pDC);

    if (type != RibbonImageSmall || m_hIconSmall == NULL)
    {
        return;
    }

    CSize sizeIcon = GetImageSize(type);

    if (m_bIsDisabled)
    {
        CMFCToolBarImages icon;

        icon.AddIcon(m_hIconSmall, m_bAlphaBlendIcon);

        CAfxDrawState ds;
        icon.PrepareDrawImage(ds, sizeIcon);
        icon.Draw(pDC, rectImage.left, rectImage.top, 0, FALSE, TRUE);
        icon.EndDrawImage(ds);
    }
    else
    {
        ::DrawIconEx (pDC->GetSafeHdc (), rectImage.left, rectImage.top, m_hIconSmall, sizeIcon.cx, sizeIcon.cy, 0, NULL, DI_NORMAL);
    }
};

to support bluring out disabled icons...

Greetings from Germany,

Oliver Nie?lein
由 Microsoft 在 2011/1/17 14:23 发送
Hello,

Thanks for the report. As we have mentioned before, we consider this by design. Using an icon with a 16x16 image and without a 32x32 image is a rare case and we don't support it by design. Adding support at this point could cause compatibility issues with other MFC applications.

However, if for some reason you need to use an icon instead of a standard ribbon image list, there is a way to derive a button as described below:

class CIconButton : public CMFCRibbonButton
{
public:
CIconButton (UINT nID, LPCTSTR lpszText, HICON hIcon) :
CMFCRibbonButton(nID, lpszText, NULL, FALSE, hIcon)
{
}

virtual CSize GetImageSize (RibbonImageType type) const
{
if (type == RibbonImageLarge || m_hIconSmall == NULL)
{
return CSize(0, 0);
}

CSize sizeIcon(16, 16);

if (afxGlobalData.GetRibbonImageScale () != 1.)
{
sizeIcon.cx = (int) (.5 + afxGlobalData.GetRibbonImageScale () * sizeIcon.cx);
sizeIcon.cy = (int) (.5 + afxGlobalData.GetRibbonImageScale () * sizeIcon.cy);
}

return sizeIcon;
}

virtual void DrawImage (CDC* pDC, RibbonImageType type, CRect rectImage)
{
ASSERT_VALID (this);
ASSERT_VALID (pDC);

if (type != RibbonImageSmall || m_hIconSmall == NULL)
{
return;
}

CSize sizeIcon = GetImageSize(type);
::DrawIconEx (pDC->GetSafeHdc (), rectImage.left, rectImage.top, m_hIconSmall, sizeIcon.cx, sizeIcon.cy, 0, NULL, DI_NORMAL);
}
};

Then use this class instead of CMFCRibbonButton in your application:

//Create buttons
CIconButton* pButtonA = new CIconButton(ID_APP_ABOUT, L"A", hIconA);
CIconButton* pButtonB = new CIconButton(ID_APP_ABOUT, L"B", hIconB);
CIconButton* pButtonC = new CIconButton(ID_APP_ABOUT, L"B", hIconC);

I hope this helps in your scenario.

Pat Brenner
Visual C++ Libraries Development

原创粉丝点击