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 t开发者_如何学Pythonhe same color.
Can anyone suggest me how to set the colors for each item separately ?
Thanks
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;
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)
精彩评论