欢迎来到入门教程网!

Delphi

当前位置:主页 > 软件编程 > Delphi >

Delphi实现Listbox中的item根据内容显示不同颜色的方法

来源:本站原创|时间:2020-01-10|栏目:Delphi|点击:

本文简述了Delphi实现Listbox中的item根据内容显示不同颜色的方法,实现步骤如下:

ListBox1 的 Style 属性改为 lbOwnerDrawVariable

在ListBox的OnDrawItem事件裡,根据item的值,改变Canvas属性

示例代码如下:

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin    //字体用原来默认的颜色
 if Odd(index) then   //当items的index为奇数时的颜色
 begin
  listbox1.Canvas.Brush.Color:=clwindow;
  ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]);
 end
 else     //当items的index为偶数时的颜色
 begin
  listbox1.Canvas.Brush.Color:=clinactivecaptiontext;
  ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]);
 end;
 if  odSelected  in  state  then    //当选定时的颜色
 begin
  listbox1.Canvas.Brush.Color:=clhighlight;
  ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]);
 end;
end;

上一篇:Delphi实现获取磁盘空间大小的方法

栏    目:Delphi

下一篇:Delphi2007编译的程序在Win7下图标模糊的解决办法

本文标题:Delphi实现Listbox中的item根据内容显示不同颜色的方法

本文地址:https://www.xiuzhanwang.com/a1/Delphi/8617.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有