1

I've just installed TMS Components for Delphi and in TAdvSmoothListBox I would like to customize colors for each item.

I am actually using the .ItemAppearance.Fill.Color but it fills all the items with the same color.

Can anyone suggest me how to set the colors for each item separately ?

Thanks

Gwenael
  • 137
  • 1
  • 2
  • 7
  • Try to set the `TAdvSmoothListBox.DefaultDraw := False;` and handle your item changes in the `OnItemBkgDraw` event. I can't help you more because I don't have TMS suite at this time and don't know the event handler. –  Aug 12 '11 at 10:34

2 Answers2

2

The event OnItemBkgDraw is definitely what you need to draw the background yourself.

But if I had to do this the background would never look really nice. So I would let somebody else do the drawing. Fortunately we can use the Fill.Fill method which will generate a nice background which is compatible with the current item appearance and the overall look of the component.

This is your OnItemBkgDraw handler:

uses AdvGDIP;

procedure TForm1.AdvSmoothListBox1ItemBkgDraw(Sender: TObject; Canvas: TCanvas; itemindex: Integer; itemRect: TRect;
  var defaultdraw: Boolean);
var
  g: TGPGraphics;
  ItemAppearance: TAdvSmoothListBoxItemAppearance;
  ir: TGPRectF;
begin
 // Disable default background drawing behavior
 DefaultDraw:= False;

 // Create our own item appearance which will be responsible for drawing the background
 // Note: The class needs an TAdvSmoothListBox owner, but we can't use ourselves as we would trigger an
 //   infinite update cycle - use a dummy list instead (can be created dynamically or
 //   just put it on your form being invisible)
 ItemAppearance:= TAdvSmoothListBoxItemAppearance.Create(DummyOwner);
 try
   // Get the current item appearance which we want to adjust a little
   ItemAppearance.Assign(AdvSmoothListBox1.ItemAppearance);

   // Set nice colors for current item (you can use the itemindex parameter to see which item is currently being painted)
   ItemAppearance.Fill.Color:= Random(High(TColor));
   ItemAppearance.Fill.ColorTo:= Random(High(TColor));

   // Now prepare the classes needed for drawing
   g := TGPGraphics.Create(Canvas.Handle);
   ir := MakeRect(itemrect.Left, itemrect.Top, itemrect.Right - itemrect.Left, itemrect.Bottom - itemrect.Top);
   try
     // And here it paints
     ItemAppearance.Fill.Fill(g, ir);
   finally
     g.Free;
   end;
 finally
   ItemAppearance.Free;
 end;
 // Done
end;
Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
1

I think Daemon_x is right here, i dont think you can do this with the properties/methods of the TAdvSmoothlistbox by default.

You can easily change fonts, images etc, but the background color needs to be done using the OnItemBkgDraw and/or the OnItemDraw events.

(as at version 2.4.0.1)

Simon
  • 9,197
  • 13
  • 72
  • 115
  • I'm sure it's the way how to customize each item, but I can't provide example because I don't know the event handler parameters. I'm pretty sure it will be adjustable by the event parameter. Soonest I will get to my D2009 on Sunday. For those who want to help earlier I'll remind that the TMS Smooth Controls suite is available for free at [Embarcadero](http://cc.embarcadero.com/reg/delphi) for D2009, D2010 and DXE users, but unfortunately without source code. –  Aug 12 '11 at 11:40