Search

CMFCRibbonButton constructor with HICON by Guglielmo Calligaro

Closed
as By Design Help for as By Design

3
0
Sign in
to vote
Type: Bug
ID: 487869
Opened: 9/3/2009 6:56:48 AM
Access Restriction: Public
0
Workaround(s)
1
User(s) can reproduce this bug
CMFCRibbonButton constructor with HICON doesn't work properly because even though my icons are only 16x16 the button appear as 32x32.
Details (expand)
Product Language
English

Version

Visual Studio 2008 SP1
Operating System
Windows XP Professional
Operating System Language
Italian
Steps to Reproduce
Create a new MFC Project and add those lines to the InitializeRibbon function in CMainFrame

//Add MyCategory to CMainFrame Ribbon Bar
CMFCRibbonCategory* pMyCategory =
m_wndRibbonBar.AddCategory(L"MyCategory", 0, 0);

//Add MyPanel to MyCategory
CMFCRibbonPanel* pMyPanel = pMyCategory->AddPanel(L"MyPanel");

//Load icons from resource (they are 16x16)
HICON hIconA = (HICON)::LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_TESTA), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON),
HICON hIconB = (HICON)::LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_TESTB), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON),
HICON hIconC = (HICON)::LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_TESTC), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON),

//Create buttons
CMFCRibbonButton* pButtonA = new CMFCRibbonButton(ID_TESTA, L"A",
hIconA);
CMFCRibbonButton* pButtonB = new CMFCRibbonButton(ID_TESTB, L"B",
hIconB);
CMFCRibbonButton* pButtonC = new CMFCRibbonButton(ID_TESTC, L"B",
hIconC);

//Add buttons to panel
pMyPanel->Add(pButtonA);
pMyPanel->Add(pButtonB);
pMyPanel->Add(pButtonC);
Actual Results
The buttons are shown in the panel as they are 32x32 so they are added always alongside and they cover the whole height of the panel, in this way

A B C
Expected Results
The buttons must be added one below the other, in this way

A
B
C
TAP Code (if applicable)
 
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey. [Details]

 

File Attachments
0 attachments
Sign in to post a comment.
Posted by TinyWolf on 4/1/2010 at 2:59 AM
Use this

class CMFCRibbonButton2 : public CMFCRibbonButton
{
protected:
    bool m_bSmallMode;

public:
    CMFCRibbonButton2(void) : m_bSmallMode(false)
    {}
    CMFCRibbonButton2(UINT nID, LPCTSTR lpszText, int nSmallImageIndex=-1, int nLargeImageIndex=-1, BOOL bAlwaysShowDescription=FALSE)
        : m_bSmallMode(false), CMFCRibbonButton(nID, lpszText, nSmallImageIndex, nLargeImageIndex, bAlwaysShowDescription)
    {}
    CMFCRibbonButton2(UINT nID, LPCTSTR lpszText, HICON hIcon, BOOL bAlwaysShowDescription=FALSE, HICON hIconSmall=NULL, BOOL bAutoDestroyIcon=FALSE, BOOL bAlphaBlendIcon=FALSE)
        : m_bSmallMode(false), CMFCRibbonButton(nID, lpszText, hIcon, bAlwaysShowDescription, hIconSmall, bAutoDestroyIcon, bAlphaBlendIcon)
    {}

public:
    void SetSmallMode(void)
    { m_bSmallMode = true; }
    void SetLargeMode(void)
    { m_bSmallMode = false; }

public:
    virtual BOOL HasLargeMode() const
    {
        if (m_bSmallMode)
            return FALSE;
        return __super::HasLargeMode();
    }
};


// Create button and call this
pBtn2->SetSmallMode();
Posted by Microsoft on 9/22/2009 at 9:45 AM
Hello,

Thanks for the report. We have investigated and found that this is a design bug: initially, it was assumed that each icon-based ribbon button MUST have a large icon. However, the possible fix may cause a backward-compatibility problems so we are not going to fix this, at least for Visual Studio 2010.

However, I can propose the following simple workaround - it does work:

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

Use this class instead of CMFCRibbonButton:

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

Please give this a try. Hope this helps to solve the issue.

Pat Brenner
Visual C++ Libraries Development

Posted by Guglielmo Calligaro on 9/9/2009 at 11:45 PM
With the code you suggested the icon disappear so this will not fix the problem.
I've tried this before to post this as a bug.
I've seen MFC code and It cannot work without a correction.
Posted by Microsoft on 9/9/2009 at 3:48 PM
Hello,

Thanks for the report. We have investigated and found that this is by design, because you are passing a small icons to the CMFCButton constructor as the large icon parameter.

If you replace this code:

//Create buttons
CMFCRibbonButton* pButtonA = new CMFCRibbonButton(ID_TESTA, L"A", hIconA);
CMFCRibbonButton* pButtonB = new CMFCRibbonButton(ID_TESTB, L"B", hIconB);
CMFCRibbonButton* pButtonC = new CMFCRibbonButton(ID_TESTC, L"C", hIconC);

with this code instead:

//Create buttons
CMFCRibbonButton* pButtonA = new CMFCRibbonButton(ID_TESTA, L"A", NULL, FALSE, hIconA);
CMFCRibbonButton* pButtonB = new CMFCRibbonButton(ID_TESTB, L"B", NULL, FALSE, hIconB);
CMFCRibbonButton* pButtonC = new CMFCRibbonButton(ID_TESTC, L"C", NULL, FALSE, hIconC);

then the icons will be treated as 16x16 images and the buttons will line up as you expect.

Pat Brenner
Visual C++ Libraries Development
Posted by Microsoft on 9/4/2009 at 1:54 AM
Thank you for your feedback, We are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.