从ComboBox DrawItem事件中的TImageList复制透明(32位Alpha)位图

编码器12345

我正在自定义OnDrawItem事件以在项目名称旁边绘制图标。到目前为止,这是我的事件OnDrawItem的代码:

void __fastcall Form1::ComboBox1DrawItem(TWinControl *Control, int Index,
                                         TRect &Rect, TOwnerDrawState State)
{
TComboBox* CB = static_cast<TComboBox*>(Control);
CB->Canvas->FillRect(Rect);

boost::scoped_ptr<Graphics::TBitmap> bitmap(new Graphics::TBitmap());
bitmap->PixelFormat = pf32bit;
bitmap->AlphaFormat = afPremultiplied;

ImageList1->GetBitmap(Index, bitmap.get());

bitmap->AlphaFormat = afPremultiplied;

if (bitmap->Canvas->Handle)
    {
    // structure for alpha blending
    BLENDFUNCTION bf;
    bf.BlendOp              = AC_SRC_OVER;
    bf.BlendFlags           = 0;
    bf.SourceConstantAlpha  = 0xFF;         // 0x00 (transparent) through 0xFF (opaque)
    bf.AlphaFormat          = AC_SRC_ALPHA; // Use bitmap alpha

    ::AlphaBlend(CB->Canvas->Handle,    // handle to destination DC
             Rect.Left + 2,             // x-coord of upper-left corner
             Rect.Top,                  // y-coord of upper-left corner
             bitmap->Width,             // destination width
             bitmap->Height,            // destination height
             bitmap->Canvas->Handle,    // handle to source DC
             0,                         // x-coord of upper-left corner
             0,                         // y-coord of upper-left corner
             bitmap->Width,             // source width
             bitmap->Height,            // source height
             bf                         // alpha-blending function
            );
    }

    Rect = Bounds(Rect.Left + 20 + 2, Rect.Top, Rect.Right - Rect.Left, Rect.Bottom - Rect.Top);

    DrawTextW(CB->Canvas->Handle, CB->Items->Strings[Index].c_str(), -1, &Rect, DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
}

当然,问题在于要获得透明TImageList1的副本以复制到透明的TBitmap保留32位alpha透明度/半透明的状态。目前,我将结果显示为白色背景TBitmap

只是要清楚一点,在将图像加载TImageList ColorDepth之前将其设置为cd32bitDrawingStyle = dsTransparent并且图像上的图像是透明的,在那里没有问题。

解决这个问题的诀窍是什么?

更新和我的最终解决方案

根据此处的回复,这是我将来为将来可能需要的其他人使用的最终工作代码。当然,这只是一个模板代码,您可能想根据自己的需要进一步自定义。

void __fastcall TForm1::ComboBox1DrawItem(TWinControl *Control, int Index, TRect &Rect, TOwnerDrawState State)
{
if (Index >= 0)
        {
        TComboBox* CB     = static_cast<TComboBox*>(Control);
        CB->Canvas->FillRect(Rect);
        // Note - ImageList1 already has DrawingStyle set to dsTransparent          
        ImageList1->Draw(CB->Canvas, Rect.Left + 2, Rect.Top, 0);
        Rect = Bounds(Rect.Left + ImageList1->Width + 2 + 2, Rect.Top, Rect.Right - Rect.Left - ImageList1->Width - 2, Rect.Bottom - Rect.Top);
        DrawTextW(CB->Canvas->Handle, CB->Items->Strings[Index].c_str(), -1, &Rect, DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
        }
}
塞塔克·阿奎兹(Sertac Akyuz)

您无需尝试从图像列表中获取原始位图,因为图像列表本身知道如何绘制荣誉透明度信息。您可以使用它的Draw方法。

否则,这里的答案表明AlphaFormat在调用之前设置为“ afIgnored”GetBitmap应保留透明度。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

UWP ComboBox中的描述

来自分类Dev

ComboBox中的花括号

来自分类Dev

ComboBox 中的 SelectedItem 绑定

来自分类Dev

从 ComboBox 中获取价值?

来自分类Dev

ComboBox SelectedIndexChanged事件未触发

来自分类Dev

Excel VBA Combobox OnExit事件

来自分类Dev

ComboBox的事件comboBox_TextChanged并不总是触发

来自分类Dev

在动态ComboBox中更改ComboBox项的文本样式

来自分类Dev

在动态ComboBox中更改ComboBox项的文本样式

来自分类Dev

在ComboBox中取消选择文本

来自分类Dev

WCF-ComboBox中的列表

来自分类Dev

在ComboBox中未显示SelectedItem

来自分类Dev

覆盖ComboBox中的选定文本

来自分类Dev

禁止在StyledItemDelegate中编辑ComboBox

来自分类Dev

在ComboBox中列出FTP内容

来自分类Dev

在DataGridView中添加ComboBox列

来自分类Dev

表格中的ExtJS 5 Combobox

来自分类Dev

在Dojo中动态填充ComboBox

来自分类Dev

ComboBox中的默认选定值

来自分类Dev

在datagridview中更新ComboBox列

来自分类Dev

从枚举填充 Datagridview 中的 ComboBox

来自分类Dev

在ComboBox的SelectionChanged事件上更新DataGrid的ItemsSource

来自分类Dev

jQuery UI Combobox清除选择事件

来自分类Dev

ComboBox上的SelectionChanged事件动态创建

来自分类Dev

DataGridViewComboBoxColumn-访问ComboBox附加单击事件

来自分类Dev

JavaFX ComboBox setItems触发onAction事件

来自分类Dev

WPF - 如何为 ComboBox 触发 MouseLeftButtonUp 事件?

来自分类Dev

在Silverlight中的ComboBox中绘制线条

来自分类Dev

我如何防止comboBox中的项目在Javafx中复制?