VCL TImage + TImageList,透明功能不起作用或者我做错了什么?

VCL TImage + TImageList, transparent feature doesn't work or what am I doing wrong?

使用 Borland C++ Builder 2009

我从 W7 Windows 对话框中截屏了三个按钮,并将它们放在 TImageList 中。我会在适当的时候在 TImage 中加载 3 个变体。

Image->Picture->Bitmap = NULL ; // Clear previous state
ImageList->GetBitmap(2, Image->Picture->Bitmap) ;

PS: Image->Transparent = True

在 Windows 7 和 Windows 10 这似乎工作正常。 但我刚刚意识到,只是因为 TImage 所在的 TForm 具有完全相同的背景 color。 (我把背景改成lime后确认不行)

在 Windows XP 上,按钮看起来不太好。由于 XP 的背景颜色似乎略有不同。请注意,它也是 clBtnFace

经验值: . . . Windows 7:

我也尝试过设置TImageList控件的BlendColorDrawingStyle,结合Image->Transparent = truefalse

但我无法让它工作。

我在W7上捕获了Image->Picture->Bitmap->Canvas->Pixels[0][0]的值并把它放入ImageList->BlendColorImageList->DrawingStyle = dsFocusdsSelected)等等,但没有成功。

我也尝试过在 ImageList->GetBitmap(2, Image->Picture->Bitmap) 之后再次显式设置 Image->Transparent = True 甚至尝试

Image->Picture->Bitmap->TransparentColor =
Image->Picture->Bitmap->Canvas->Pixels[0][0]

没有明显的效果。

你的想法?

我在评论中所说的,而不是从屏幕上捕获图像,您可以尝试使用传递 TDLG_EXPANDOBUTTON 部分和有效状态之一的 DrawThemeBackground 方法绘制展开按钮(TDLGEBS_NORMALTDLGEBS_HOVERTDLGEBS_PRESSED 等)。对于 Windows XP,您可以使用 EBP_NORMALGROUPEXPAND 部分和这些状态之一(EBHC_NORMALEBHC_HOTEBHC_PRESSED

检查此示例,它在 TImage 中绘制了 expando 按钮

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Vsstyle.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------


__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Image1->Canvas->Brush->Color = clBtnFace;
  Image1->Canvas->FillRect(Image1->ClientRect);

  OSVERSIONINFO verwin;
  verwin.dwOSVersionInfoSize=sizeof(verwin);
  if (GetVersionEx(&verwin))
  {
    //Check  the Windows version
    if (verwin.dwMajorVersion >= 6) // is Vista at least?
    {
      HTHEME hTheme = OpenThemeData(Handle, VSCLASS_TASKDIALOG);
      if (hTheme)
      {
          SIZE s;
          //get the size of the  TDLG_EXPANDOBUTTON
          if (GetThemePartSize(hTheme, Image1->Canvas->Handle, TDLG_EXPANDOBUTTON, TDLGEBS_NORMAL, NULL, TS_TRUE, &s) == S_OK)
          {
              TRect pRect = Rect(0, 0, s.cx, s.cy);
              DrawThemeBackground(hTheme, Image1->Canvas->Handle, TDLG_EXPANDOBUTTON, TDLGEBS_NORMAL, &pRect, NULL);
           }
      }
    }
    else
    {
      HTHEME hTheme = OpenThemeData(Handle, VSCLASS_EXPLORERBAR);
      if (hTheme)
      {
          SIZE s;
          //get the size of the  EBP_NORMALGROUPEXPAND
          if (GetThemePartSize(hTheme, Image1->Canvas->Handle, EBP_NORMALGROUPEXPAND, EBHC_NORMAL, NULL, TS_TRUE, &s) == S_OK)
          {
              TRect pRect = Rect(0, 0, s.cx, s.cy);
              DrawThemeBackground(hTheme, Image1->Canvas->Handle, EBP_NORMALGROUPEXPAND, EBHC_NORMAL, &pRect, NULL);
           }
      }
    }
  }
}