我刚刚为 Delphi 安装了 TMS 组件,在TAdvSmoothListBox中我想为每个项目自定义颜色。
我实际上正在使用 .ItemAppearance.Fill.Color 但它用相同的颜色填充所有项目。
谁能建议我如何分别设置每个项目的颜色?
谢谢
我刚刚为 Delphi 安装了 TMS 组件,在TAdvSmoothListBox中我想为每个项目自定义颜色。
我实际上正在使用 .ItemAppearance.Fill.Color 但它用相同的颜色填充所有项目。
谁能建议我如何分别设置每个项目的颜色?
谢谢
该事件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;
我认为 Daemon_x 就在这里,我认为默认情况下你不能使用 TAdvSmoothlistbox 的属性/方法来做到这一点。
您可以轻松更改字体、图像等,但需要使用OnItemBkgDraw
和/或OnItemDraw
事件来完成背景颜色。
(在版本 2.4.0.1 中)