问题: 如何给listview每行设置不同颜色? ( 积分: 50 )
分类: 数据库-C/S型
来自: dpfrog, 时间: 2003-07-22 21:02:00, ID: 2051181
我用listview作为显示查询得到的数据,请问怎么根据条件设置每行不同的颜色啊?
比如根据成绩低于60分,设置为红色?
来自: dpfrog, 时间: 2003-07-22 21:28:00, ID: 2051217
等待。。。。。
来自: qingniao, 时间: 2003-07-22 21:30:00, ID: 2051224
k看看这个,或许有用
procedure TForm2.SeSkinListBox1DrawItem(ListBox: TSeCustomListBox;
Canvas: TCanvas; Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
s:string;
mytime:string;
begin
if not FileExists(listbox.Items.Strings[index]) then
begin
listbox.Items.Delete(index);
exit;
end;
if extractFileExt(listbox.Items.Strings[index])='.mp3' then //判断mp3
begin
mp3inf.LoadFromFile(listbox.Items.Strings[index]);
mytime:=FormatDateTime ('nn:ss', mp3inf.MpegDuration / (24.0 * 60 * 60 * 1000));
if mp3inf.ID3.Ok then
s:=inttostr(index+1)+'.'+mp3inf.ID3.Artist+'-'+mp3inf.ID3.Title
else
s:=inttostr(index+1)+'.'+extractFilename(listbox.Items.Strings[index]);
if index<9 then
s:='0'+s;
end
else
begin
s:=inttostr(index+1)+'.'+extractFilename(listbox.Items.Strings[index]);
if index<9 then
s:='0'+s;
end;
if form1.MediaPlayer1.FileName=listbox.Items.Strings[index] then//改变字色
Canvas.Font.Color:=clgreen
else
Canvas.Font.Color:=clblack;
if odSelected in State then//选种色
begin
tag:=index;
Canvas.Brush.Style := bsSolid;
Canvas.Brush.Color := clGradientActiveCaption;
Canvas.Font.Size:=10;
end
else
begin
Canvas.Brush.Style := bsClear;
Canvas.Font.Size:=8;
end;
Canvas.TextRect(Rect,rect.Left+2,Rect.Top,s);
drawtext(Canvas,mytime,Rect, DT_RIGHT);
end;
来自: SharpExpress, 时间: 2003-07-22 21:39:00, ID: 2051244
如上图,代码类似下面:注意,ListView的OwnerDraw应该设置为False!
然后在CustDrawItem中,添加代码:
var
vColor: TColor;
begin
adsTmp.RecNo := Item.Index;
case adsTmp.FieldByName('成绩').AsFloat of
80..100: vColor := clSkyBlue;
60..79: vColor := clGradientActiveCaption;
0..59: vColor := clRed;
end;
Sender.Canvas.Brush.Color := vColor;
end;
来自: dpfrog, 时间: 2003-07-24 8:53:00, ID: 2054746
SharpExpress老兄,你的代码中adsTmp是什么?麻烦您了!
来自: 影 子, 时间: 2003-07-24 8:55:00, ID: 2054752
有可能是ADODataSet,并不重要
来自: dpfrog, 时间: 2003-07-24 9:11:00, ID: 2054824
怎么定义这个变量啊?他是什么类型的?
来自: stuwe, 时间: 2003-07-24 9:14:00, ID: 2054849
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if StrToFloat(Item.SubItems.Strings[[red]i[/red]])<60 then//i是指成績那一列
ListView1.Canvas.Brush.Color:=clRed;
end;
来自: aolo, 时间: 2003-07-24 9:16:00, ID: 2054857
procedure TMainForm.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if Item.Caption = '中国' then
begin
(Sender as TListView).Canvas.Font.Color := clMaroon;
end
else if Item.Caption = '英国' then
begin
(Sender as TListView).Canvas.Font.Color := clRed;
end
...
end;
来自: goeasy, 时间: 2003-07-24 9:16:00, ID: 2054859
这个绝对可以,我正用的。
procedure TFrm_ServerWork.ListView1AdvancedCustomDrawItem(
Sender: TCustomListView; Item: TListItem; State: TCustomDrawState;
Stage: TCustomDrawStage; var DefaultDraw: Boolean);
begin
if(Item.SubItems.Strings[3] = '教师') then
begin
Sender.Canvas.Brush.Color := RGB(216,232,208);
end
else
begin
Sender.Canvas.Brush.Color := RGB(232,232,208);
end;
if (Item.Selected) then
begin
Sender.Canvas.Font.Style := [fsBold];
end;
end;
来自: plzw, 时间: 2003-07-24 9:37:00, ID: 2054961
请问楼上的AdvancedCustomDrawItem和CustomDrawItem的区别在哪?
来自: dpfrog, 时间: 2003-08-08 19:54:00, ID: 2095727
多人接受答案了。
问题讨论没有结束 ...