Rudi
Rudi

Reputation: 305

Changing picture of tool button at runtime does not work anymore

I have a tool bar and I am using the following procedure to change the color of a rectangle in one of the tool buttons. The ColorDepth of the ImageList is cl24Bit and the DrawingStyle is dsTransparent. The procedure works fine.

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;

If I add the program to the repository for reuse it works fine. However, if I start a new program from scratch and use the exact same procedure, I get a white button. I made sure that the properties for the image list and the tool bar are the same in both programs. The program that works was written some time ago. Could the problem have anything to do with Windows updates? I am using Windows 10 and Delphi 10.

Upvotes: 1

Views: 850

Answers (1)

Tom Brunberg
Tom Brunberg

Reputation: 21033

There are two solutions to your problem.

1) Disabling themeing of your application

Disable by unticking the 'Enable Runtime themes' checkbox in 'Project - Options - Application'.

The downside of this is that the application looks as developed for Windows 95.

2) Change following properties of the ImageList

  • ColorDepth: cdDeviceDependent
  • DrawingStyle: dsNormal
  • ImageType: itMask

The result looks like this on Windows 10 (and with respect to the toolbuttons, the same also on Windows 7):

enter image description here

I modified your code to act as a toggle for the buttons, therefore two buttons have the red rectangle.

The numbers are simply 64 x 64 pixel bitmaps with black text on white background.

Caveat: The principle of copying - modifying - copy back repeatedly might lead to reduced quality of the images. A better way could be to have two imagelists, one with the original images and one with the rectangle readily drawn.

Having said that, it appears that the purpose of the rectangle is to indicate some kind of 'active' state. That can be achived also with Down property of the buttons.

Upvotes: 2

Related Questions