自定义TAdvSmoothListBox项目颜色

时间:2011-08-12 09:17:17

标签: delphi listbox components tms

我刚刚为Delphi和TAdvSmoothListBox安装了TMS组件我想为每个项目自定义颜色。

我实际上使用的是.ItemAppearance.Fill.Color,但它填充了所有相同颜色的项目。

有人可以建议我如何分别设置每个项目的颜色吗?

由于

2 个答案:

答案 0 :(得分:2)

事件OnItemBkgDraw绝对是您自己绘制背景所需的。

但如果我必须这样做,背景永远不会很好看。所以我会让其他人做绘图。幸运的是,我们可以使用Fill.Fill方法生成一个漂亮的背景,该背景与当前项目外观和组件的整体外观兼容。

这是您的OnItemBkgDraw处理程序:

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;

答案 1 :(得分:1)

我认为Daemon_x就在这里,我认为你不能默认使用TAdvSmoothlistbox的属性/方法。

您可以轻松更改字体,图片等,但需要使用OnItemBkgDraw和/或OnItemDraw事件来完成背景颜色。

(截至2.4.0.1版)