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;
谢谢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;
procedure TForm2.ListView2CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean); begin if 超过最大值 then ListView.Canvas.font.Color:= clWindow; end;
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;
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;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if 超过最大值 then
ListView.Canvas.font.Color:= clWindow;
end;