3
votes

I am trying to add an image to a column on a TDbgrid that takes transparency into account. When drawing the image from a TImageList on the canvas in the DBGridDrawColumnCell procedure, I need the background of the image (the same color as the pixel in the lower left corner) to take on transparency. I want this transparency area to show the highlight color or non-highlight color, especially when themes are used, such as Aero. I have been able to accomplish this in older versions of Windows with color values of clHighlight or clWindow as the background color. But with Aero themes, it always paints a box behind the non-transparent part of the image instead of the gradient blue highlight color that Aero uses. How can I accomplish this?

I believe I am supposed to use alpha channel but I'm not sure how to do this from a TImageList to a canvas. I believe the cell is painted completely with the actual highlight color before I start drawing on the canvas in the cell. I just want to draw the non-transparency part of the image and leave the background.

1
If Delphi still does not support PNG (with alpha-channel ability) images, there is good library PNGComponentsAbelisto
@Abelisto I tried your advice and loaded PNGComponents. It has its own lmageList and I would get the same result with no transparent background. I also found that Delphi 2009 + has a PNGImage library that can be added to the project if using png images. I could not get it to work with this library either. Finally, I was able to solve my problem. See answer below...Ron Swingler

1 Answers

1
votes

I was able to finally determine how to display images on a dbgrid with transparency even if themes, such as Aero is used.

I used a regular TImageList and loaded the images that I needed to display on the dbgrid. In my case there were two and they were in icon (ico) format. Instead of transferring the image to a bitmap and then drawing it to the dbgrid canvas as most old code recommends, I simply used the following simple code in the DBGridDrawColumnCell procedure:

if DataCol=0 then
begin
     if (MApptsConflict.Value='<none>') then
         ImageIndex := 0
     else
         ImageIndex := 1;

     ImageList.Draw(TDBGrid(Sender).Canvas,Rect.Left+2,Rect.Top+2,ImageIndex,True);
end; 

This will draw directly to the dbgrid canvas from the TImageList which will give the desired transparency.

UPDATE: I tried it with bmp's loaded in the Timagelist and it worked too.