2

項目名の横にアイコンを描画するように 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);
}

もちろん問題は、32ビットのアルファ透明度/半透明度を維持しながら、透明から透明TImageList1にコピーすることです。TBitmap現在、結果の白い背景でそれを取得しTBitmapます。

明確にするために、画像をロードする前にwithに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);
        }
}
4

1 に答える 1

2

イメージリスト自体が透明度情報を尊重して描画する方法を知っているため、イメージリストから元のビットマップを取得しようとする必要はありません。そのためにそのDrawメソッドを使用できます。

それ以外の場合、ここでの回答は、AlphaFormat呼び出す前に「afIgnored」に設定するとGetBitmap透明性が維持されることを示唆しています。

于 2015-02-08T16:01:56.233 に答える