将ListBos1.Style 设为 lbOwnerDrawFixed;procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
ListBox1.Canvas.FillRect(Rect);
if Index = ListBox1.Items.Count - 1 then
ListBox1.Canvas.Font.Color := clRed
else
ListBox1.Canvas.Font.Color := clBlack;
if odSelected in State then
ListBox1.Canvas.Font.Color := clWhite;
ListBox1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, ListBox1.Items[Index]);
end;
Rect: TRect; State: TOwnerDrawState);
begin
ListBox1.Canvas.FillRect(Rect);
if Index = ListBox1.Items.Count - 1 then
ListBox1.Canvas.Font.Color := clRed
else
ListBox1.Canvas.Font.Color := clBlack;
if odSelected in State then
ListBox1.Canvas.Font.Color := clWhite;
ListBox1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, ListBox1.Items[Index]);
end;
Rect: TRect; State: TOwnerDrawState);
begin
ListBox1.Canvas.FillRect(Rect);
if Index = ListBox1.Items.Count - 1 then
ListBox1.Canvas.Font.Color := clRed
else
ListBox1.Canvas.Font.Color := clBlack;
if odSelected in State then
ListBox1.Canvas.Font.Color := clWhite;
ListBox1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, ListBox1.Items[Index]);
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
TListBox(Sender).Canvas.FillRect(Rect);
if TListBox(Sender).Items[Index] = Edit1.Text then
TListBox(Sender).Canvas.Font.Color := clRed;
TListBox(Sender).Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, TListBox(Sender).Items[Index]);
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
m_colors:array[0..3] of tcolor;//自己定义一个数组放颜色
begin
m_colors[0]:=clred;
m_colors[1]:=clblue;
m_colors[2]:=clgreen;
m_colors[3]:=clblack;
ListBox1.Canvas.FillRect(Rect);
ListBox1.Canvas.Font.Color := m_colors[index mod 4];
if odSelected in State then
ListBox1.Canvas.Font.Color := clWhite;
ListBox1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, ListBox1.Items[Index]);end;
Rect: TRect; State: TOwnerDrawState);
begin
ListBox1.Canvas.FillRect(Rect);
if 条件1成立then
ListBox1.Canvas.Font.Color := clRed
else if 条件2成立 then
ListBox1.Canvas.Font.Color := clYellow
else if 条件3成立 then
ListBox1.Canvas.Font.Color := clGreen;
{ 其它的以此类推 }
if odSelected in State then
ListBox1.Canvas.Font.Color := clWhite;
ListBox1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, ListBox1.Items[Index]);
end;