ListBox的每一行颜色不同
首先,设置ListBox1的Style属性为lbOwnerDrawVariable
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
ListBox1.Items.AddObject('这是红色的一行', TObject(clRed));
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
ListBox1.Items.AddObject('这是蓝色的一行', TObject(clBlue));
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
ListBox1.Items.AddObject('这是绿色的一行', TObject(clGreen));
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect:
TRect; State: TOwnerDrawState);
begin
with Control as TListBox do
begin
Canvas.FillRect(Rect); // 填充背景
// 从Items.Objects中读取颜色
Canvas.Font.Color := TColor(Items.Objects[Index]);
// 绘制文本
Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, Items[Index]);
end;
end;
浙公网安备 33010602011771号