首页 > 代码库 > CMFCRibbonButton使用HICON作为参数的构造函数BUG

CMFCRibbonButton使用HICON作为参数的构造函数BUG

问题描述

CMFCRibbonButton constructor with HICON doesn‘t work properly because even though my icons are only 16x16 the button appear as 32x32.

使用HICON作为参数构建的RibbonButton即便指定用16X16的小图标,但最后显示在界面上仍是32X32的大图标。


解决方法

写一个新的小图标按钮类,重写GetImageSize和DrawImage方法如下:

class CSmallIconButton : public CMFCRibbonButton
{
public:
CSmallIconButton (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);
}
};
后来发现,MainPannel上的CMFCRibbonMainPanelButton也是如此,使用类似的方法即可。
微软官方原文:
http://connect.microsoft.com/VisualStudio/feedback/details/487869/cmfcribbonbutton-constructor-with-hicon