How to add image with transparency to TDbgrid column in delphi?

I am trying to add an image to a column on a TDbgrid that takes transparency into account. When drawing an image from a TImageList to a canvas in the DBGridDrawColumnCell procedure, I need the background of the image (the same color as the pixel in the lower left corner) in order to get transparency. I want this transparency area to show a highlight color or a non-highlight color, especially when using themes like Aero. I was able to accomplish this on older versions of Windows with the color values ​​clHighlight or clWindow as the background color. But with Aero themes, it always draws the box behind the opaque part of the image, not the blue gradient color that Aero uses. How can i do this?

I believe I should be using the alpha channel, but I'm not sure how to do this from the TImageList to the canvas. I believe the cell is completely colored with the selected color before I start painting to the canvas in the cell. I just want to paint the opaque part of the image and leave the background.

+3


source to share


1 answer


I was finally able to figure out how to display images on the dbgrid with transparency even when using themes like Aero.

I used a regular TImageList and loaded the images that I need to display on the dbgrid. In my case, there were two of them, 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 of the old codes recommend, I just 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 take directly to the canvas dbgrid from the TImageList, which will give the required transparency.

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

+1


source







All Articles