怎么修改listview某个格子中的内容的字体颜色?

oldcat1 2003-09-12 01:55:42
将实时采集到的最新数据,改写到listview的某个单元格,当数据超过最大值时,此数据值就以不同的颜色(红色)显示出来,如何实现,请高手指点,急切等待回答!
...全文
290 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
joky1981 2003-09-15
  • 打赏
  • 举报
回复
procedure TForm2.ListView2CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if 超过最大值 then
ListView.Canvas.font.Color:= clWindow;
end;
oldcat1 2003-09-15
  • 打赏
  • 举报
回复
谢谢dickeybird888(于伟钢),但我用的控件不是listbox,而是listview控件,我用了下面的程序,但不能单独改变某一个单元格的颜色,只能改变这个单元格所在的整行的颜色。更麻烦的是当第一次写数据到listview表格时,超上限值用红色(整行红色),超下限用兰色(整行兰色),还可以接受,而当定时刷新时,我仅改变一行中实时数据的三个单元格的值,其它单元格的值均不变,这样就出现了一行中有红黑、或兰黑、或红兰二种颜色(数值不变的单元格保持了原颜色,数值变化的单元格根据新的值变为另一种颜色),请指教!
procedure Tfrmmain.listview1CustomDrawSubItem(Sender: TCustomListView;
Item: TListItem; SubItem: Integer; State: TCustomDrawState;
var DefaultDraw: Boolean);
var
str1,str2: String;
i:integer;
wd1,wd2,wd3,wdsx,wdxx:single;
pos1:integer;
begin
str1:= Trim(Item.SubItems.Text); //得这一行所有单元格的值(字符串),例形式为:'1'#$D#$A'33'#$D#$A'4'……表示第1个单元格的值为1,第2个单元格的值为33,第3个单元格的值为4……
if compareStr(str1,strstr)<>0 then
begin
strstr:=str1;
delete(str1,1,8);
pos1:=pos(#$A,str1);
delete(str1,1,pos1);
pos1:=pos(#$D,str1);
str2:=copy(str1,1,pos1-1);
delete(str1,1,pos1+1);
wd1:=strtofloat(str2);//得到检测点1的温度值
pos1:=pos(#$D,str1);
str2:=copy(str1,1,pos1-1);
delete(str1,1,pos1+1);
wd2:=strtofloat(str2);//得到检测点2的温度值

pos1:=pos(#$D,str1);
str2:=copy(str1,1,pos1-1);
delete(str1,1,pos1+1);
wd3:=strtofloat(str2);//得到检测点3的温度值
pos1:=pos(#$D,str1);
str2:=copy(str1,1,pos1-1);
delete(str1,1,pos1+1);
wdsx:=strtofloat(str2);//);//得到温度上限值

pos1:=pos(#$D,str1);
str2:=copy(str1,1,pos1-1);
delete(str1,1,pos1+1);
wdxx:=strtofloat(str2);//得到温度下限值
if (wd1>=wdsx) or (wd2>=wdsx) or (wd3>=wdsx) then //温度1、温度2、温度超过温度上限时,为红色显示
Sender.Canvas.font.Color := clred
else if (wd1<=wdxx) or (wd2<=wdxx) or (wd3<=wdxx)then //超温度下限时,兰色显示
Sender.Canvas.font.Color := clblue
else Sender.Canvas.font.Color := clblack;//clwindow;
end;
end;
dickeybird888 2003-09-12
  • 打赏
  • 举报
回复
1.设置listbox的state属性为:lbOwnerDrawFixed
2.在下列事件中写代码:
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
if index=5 then
begin
listbox1.canvas.font.Color := clMaroon;
end
else
begin
listbox1.canvas.font.Color := clRed;
end;
listbox1.canvas.TextOut(Rect.Left+1, Rect.Top+1, ListBox1.Items[Index]);
end;
moqiyayan 2003-09-12
  • 打赏
  • 举报
回复
OnCustomDrawSubItem试试
poleax 2003-09-12
  • 打赏
  • 举报
回复
在OnDrawItem事件中写

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧