23W
23W

Reputation: 1540

Does CMFCStatusBar::SetPaneIcon supports alpha icon?

My App setup icons for panes in status bar (class CMFCStatusBar). There is only one method for this task - CMFCStatusBar::SetPaneIcon(). But if icon have alpha channel this method loads wrong image (on black background). I looked into the source code and I saw that CMFCStatusBar uses internal HIMAGELISTs for every panes. All this HIMAGELIST creates with flag ILC_MASK | ILC_COLORDDB, not ILC_COLOR32 or ILC_COLOR24.

This is a bug!

So, is there a way to use the icons with alpha channel in CMFCStatusBar panes?

Example:

HICON hIcon = ::LoadImage(hModule, MAKEINTRESOURCE(nIconID), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
m_StatusBar.SetPaneIcon(m_StatusBar.CommandToIndex(ID_INDICATOR_1), hIcon);

Upvotes: 1

Views: 508

Answers (2)

23W
23W

Reputation: 1540

My solution, based in rrirower code.

CMFCStatusBarPaneInfo* CMyStatusBar::GetPane(int nIndex) const {
    if (nIndex == 255 && m_nCount < 255) {
        // Special case for the simple pane
        for (int i = 0; i < m_nCount; i++)
        {
                CMFCStatusBarPaneInfo* pSBP = GetPane(i);
                ENSURE(pSBP != NULL);

            if (pSBP->nStyle & SBPS_STRETCH) {
                return pSBP;
            }
        }
    }

    if (nIndex < 0 || nIndex >= m_nCount) {
        return NULL;
    }

    if (m_pData == NULL) {
        ASSERT(FALSE);
        return NULL;
    }

    return((CMFCStatusBarPaneInfo*)m_pData) + nIndex;
}


void CMyStatusBar::SetPaneIcon(int nIndex, HICON hIcon, BOOL bUpdate /*= TRUE*/)
{
    ASSERT_VALID(this);

    CMFCStatusBarPaneInfo* pSBP = GetPane(nIndex);
    if (pSBP == NULL)
    {
        ASSERT(FALSE);
        return;
    }

    // Disable animation(if exist):
    SetPaneAnimation(nIndex, NULL, 0, FALSE);

    if (hIcon == NULL)
    {
        if (pSBP->hImage != NULL)
        {
            ::ImageList_Destroy(pSBP->hImage);
        }

        pSBP->hImage = NULL;

        if (bUpdate)
        {
            InvalidatePaneContent(nIndex);
        }

        return;
    }

    ICONINFO iconInfo;
    ::GetIconInfo(hIcon, &iconInfo);

    BITMAP bitmap;
    ::GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bitmap);

    ::DeleteObject(iconInfo.hbmColor);
    ::DeleteObject(iconInfo.hbmMask);

    if (pSBP->hImage == NULL)
    {
        pSBP->cxIcon = bitmap.bmWidth;
        pSBP->cyIcon = bitmap.bmHeight;

        pSBP->hImage = ::ImageList_Create(pSBP->cxIcon, pSBP->cyIcon, ILC_COLOR32 | ILC_MASK, 1, 0);
        ::ImageList_AddIcon(pSBP->hImage, hIcon);

        RecalcLayout();
    }
    else
    {
        ASSERT(pSBP->cxIcon == bitmap.bmWidth);
        ASSERT(pSBP->cyIcon == bitmap.bmHeight);

        ::ImageList_ReplaceIcon(pSBP->hImage, 0, hIcon);
    }

    if (bUpdate)
    {
        InvalidatePaneContent(nIndex);
    }
}

Upvotes: 0

rrirower
rrirower

Reputation: 4590

Microsoft developed the CMFC classes (Feature Pack) from the BCG toolkit. If you compare the CMFCStatusBar::SetPaneIcon() method with the BCG method from the same class, you’ll notice a few subtle differences.

BCG defines its method with a flag for alpha blend. The method looks like:

void CBCGPStatusBar::SetPaneIcon (int nIndex, HBITMAP hBmp,
            COLORREF clrTransparent, BOOL bUpdate, BOOL bAlphaBlend/* = FALSE*/)

The CMFC equivalent removes the ‘bAlphaBlend’ flag. Comparing the method code, you’ll see that BCG uses the following code that has been removed from the CMFC version:

DWORD dwFlags = ILC_MASK | ILC_COLORDDB;
if (bAlphaBlend)
{
    dwFlags = ILC_COLOR32;
}

pSBP->hImage = ::ImageList_Create (pSBP->cxIcon, pSBP->cyIcon, dwFlags, 1, 0);

I’m not sure why the CMFC version differs so much (Microsoft may have a valid reason), but, I’ve included the BCG version below. I would study the BCG version and possibly create your own ‘SetPaneIcon’ method from that code (at your own risk).

void CBCGPStatusBar::SetPaneIcon (int nIndex, HBITMAP hBmp,
                                 COLORREF clrTransparent, BOOL bUpdate, BOOL bAlphaBlend/* = FALSE*/)
{
    ASSERT_VALID(this);

    CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(nIndex);
    if (pSBP == NULL)
    {
        ASSERT (FALSE);
        return;
    }

    // Disable animation (if exist):
    SetPaneAnimation (nIndex, NULL, 0, FALSE);

    if (hBmp == NULL)
    {
        if (pSBP->hImage != NULL)
        {
            ::ImageList_Destroy (pSBP->hImage);
        }

        pSBP->hImage = NULL;

        if (bUpdate)
        {
            InvalidatePaneContent (nIndex);
        }

        return;
    }

    BITMAP bitmap;
    ::GetObject (hBmp, sizeof (BITMAP), &bitmap);

    if (pSBP->hImage == NULL)
    {
        pSBP->cxIcon = bitmap.bmWidth;
        pSBP->cyIcon = bitmap.bmHeight;

        DWORD dwFlags = ILC_MASK | ILC_COLORDDB;
        if (bAlphaBlend)
        {
            dwFlags = ILC_COLOR32;
        }

        pSBP->hImage = ::ImageList_Create (pSBP->cxIcon, pSBP->cyIcon, dwFlags, 1, 0);
        RecalcLayout ();
    }
    else
    {
        ASSERT (pSBP->cxIcon == bitmap.bmWidth);
        ASSERT (pSBP->cyIcon == bitmap.bmHeight);

        ::ImageList_Remove (pSBP->hImage, 0);
    }

    //---------------------------------------------------------
    // Because ImageList_AddMasked changes the original bitmap,
    // we need to create a copy:
    //---------------------------------------------------------
    HBITMAP hbmpCopy = (HBITMAP) ::CopyImage (hBmp, IMAGE_BITMAP, 0, 0, 0);

    if (bAlphaBlend)
    {
        ::ImageList_Add (pSBP->hImage, hbmpCopy, NULL);
    }
    else    
    {
        ::ImageList_AddMasked (pSBP->hImage, hbmpCopy, clrTransparent);
    }

    ::DeleteObject (hbmpCopy);

    if (bUpdate)
    {
        InvalidatePaneContent (nIndex);
    }
}

Upvotes: 1

Related Questions