在运行时更改工具按钮的图片不再起作用

Changing picture of tool button at runtime does not work anymore

我有一个工具栏,我正在使用以下过程更改其中一个工具按钮中矩形的颜色。 ImageList的ColorDepthcl24BitDrawingStyledsTransparent。该程序运行正常。

procedure TANewMain.BtReplaceHighOnClick(Sender: TObject);
var
  ABitmap: TBitmap;
  ARect: TRect;
begin
  ABitmap := TBitmap.Create;
  try
    ImgList.GetBitmap(1, ABitmap);
    ABitmap.Canvas.Brush.Color := ColorToRGB(clRed); // S04
    ABitmap.Canvas.Pen.Color := ColorToRGB(clBlue);
    ARect := Rect(5, 1, 11, 15);
    ABitmap.Canvas.Rectangle(ARect);
    ImgList.ReplaceMasked(1, ABitmap, clWhite);
  finally
    ABitmap.Free;
  end;
end;

如果我将程序添加到存储库以供重复使用,它就可以正常工作。但是,如果我从头开始一个新程序并使用完全相同的过程,我会得到一个白色按钮。我确保图像列表和工具栏的属性在两个程序中是相同的。有效的程序是前段时间编写的。问题可能与 Windows 更新有关吗?我正在使用 Windows 10 和 Delphi 10。

您的问题有两种解决方案。

1) 禁用应用程序的主题

通过取消勾选 'Project - Options - Application' 中的 'Enable Runtime themes' 复选框来禁用。

缺点是应用程序看起来是为 Windows95 开发的。

2) 更改 ImageList

的以下属性
  • 颜色深度:cdDeviceDependent
  • 绘图样式:dsNormal
  • 图像类型:itMask

结果在 Windows 10 上看起来像这样(关于工具按钮,在 Windows 7 上也是如此):

我修改了您的代码以充当按钮的开关,因此两个按钮具有红色矩形。

数字只是 64 x 64 像素位图,白底黑字。

警告:重复复制-修改-复制的原则可能导致图像质量下降。更好的方法可能是有两个图像列表,一个包含原始图像,另一个包含易于绘制的矩形。

话虽如此,矩形的目的似乎是表示某种 'active' 状态。这也可以通过 Down 属性 按钮来实现。