在带有 BS_OWNERDRAW 标志的按钮中显示透明位图

问题描述 投票:0回答:1

在按钮上显示透明位图时遇到问题。新图像只是相互叠加,我在互联网上找不到任何关于此的合理信息。

这是我的代码:

VOID DrawButton(LPDRAWITEMSTRUCT DrawStruct, HBITMAP* BitmapArray)
{
    HBITMAP CurrentBitmap = BitmapArray[BM_UP];

    switch (DrawStruct->itemAction)
    {
    case ODA_SELECT:
        if (DrawStruct->itemState & ODS_SELECTED)
        {
            if (DrawStruct->itemState & ODS_FOCUS)
                CurrentBitmap = BitmapArray[BM_DOWN_FOCUS];
            else
                CurrentBitmap = BitmapArray[BM_DOWN];
        }
        break;
    case ODA_DRAWENTIRE:
        if (DrawStruct->itemState & ODS_DISABLED)
            CurrentBitmap = BitmapArray[BM_DISABLE];
        break;
    case ODA_FOCUS:
        if (DrawStruct->itemState & ODS_FOCUS)
            CurrentBitmap = BitmapArray[BM_FOCUS];
        break;
    }
    
    HDC CompatibleDC = CreateCompatibleDC(DrawStruct->hDC);
    HBITMAP hOld = (HBITMAP)SelectObject(CompatibleDC, CurrentBitmap);
    SelectObject(CompatibleDC, CurrentBitmap);
    AlphaBlend(DrawStruct->hDC, 0, 0, 50, 24, CompatibleDC, 0, 0, 50, 24, { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA });
    SelectObject(DrawStruct->hDC, hOld);
    DeleteDC(CompatibleDC);
    DeleteObject(CurrentBitmap);
}

c++ winapi button transparency alpha-transparency
1个回答
0
投票

您需要像@JonathanPotter 所说的那样使该区域无效或删除。

有一个 Microsoft 示例 ChooseFont 解释了这一点。

/******************************************************************
*                                                                 *
* ChooseFontDialog::OnFontSizeSelect                              *
*                                                                 *
* Record the new font size and redraw the sample text.            *
*                                                                 *
******************************************************************/

HRESULT ChooseFontDialog::OnFontSizeSelect()
{
    HRESULT hr = S_OK;

    // Signal the sample text window to redraw itself.
    InvalidateRect(GetDlgItem(m_dialog, ID_SAMPLE_BOX), NULL, false);

    return hr;
}
© www.soinside.com 2019 - 2024. All rights reserved.