您的位置:首页 > 编程语言 > Delphi

delphi中如何使得listbox里为txt后缀的行改变颜色(使listbox根据行内容改变颜色)

2014-09-13 16:50 405 查看
想让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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: