3

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.

Ron Swingler
  • 49
  • 1
  • 5
  • If Delphi still does not support PNG (with alpha-channel ability) images, there is good library [PNGComponents](http://cc.embarcadero.com/item/26127) – Abelisto May 15 '15 at 07:42
  • @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 May 18 '15 at 18:59

1 Answers1

1

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.

Ron Swingler
  • 49
  • 1
  • 5
  • Why would you transfer the image to a bitmap first? – Sertac Akyuz May 19 '15 at 00:52
  • @SertacAkyuz because that is the way my code did it before and most old code methods online taught you to do it this way. For example, [at Delphi.About](http://delphi.about.com/library/weekly/aa032205a.htm). Maybe the ImageList.Draw method was not available in earlier versions of Delphi. But it definitely is the easier way to accomplish what I needed. – Ron Swingler May 20 '15 at 16:37