想让listbox中后缀名为txt的行变成红色,其他默认黑色,
ListBox1 的 Style 属性改为 lbOwnerDrawVariable
在ListBox的OnDrawItem事件裡,根据item的值,改变Canvas属性
代码如下:procedure TForm1.lst1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
len:Integer;
begin
len:=Length(lst1.Items[Index]);
if Copy(lst1.Items[Index],len-2,len)='txt' then
begin
lst1.Canvas.Font.Color:=clRed;
lst1.Canvas.TextRect(Rect,rect.Left,Rect.Top,lst1.Items[index]);
end
else
begin
lst1.Canvas.Font.Color:=clBlack;
lst1.Canvas.TextRect(Rect,rect.Left,Rect.Top,lst1.Items[index]);
end;
end;