关于LISTVIEW中记录颜色改变,在线等,急~!!!!!(100分)

  • 主题发起人 主题发起人 noelse520
  • 开始时间 开始时间
N

noelse520

Unregistered / Unconfirmed
GUEST, unregistred user!
我的意图是当记录中有报警时变成红色,断线时为黄色。但我在CustomDrawItem中加入代码。可是颜色并没像预期的那样改变,而是当窗体有变化时才变。大家帮我看看代码,到底哪儿有问题。
Var
SensorType:String;
WarnValue,CurValue:Real;
SiteID,SensorID:Integer;
begin
SiteID:=StrToIntDef(Copy(Trim(Item.Caption),1,2),1);
SensorID:=StrToIntDef(Copy(Trim(Item.Caption),3,2),1);
SensorType:=Copy(Trim(Item.Caption),5,1);
ListViewAll.Canvas.Font.Color:=clBlack;
if SensorType='A' then
begin
if SensorAInfo[SiteID][SensorID].Warned then
begin
if ListViewAll.Canvas.Font.Color<>clRed then
begin
ListViewAll.Canvas.Font.Color := clRed;
end;
end
else
if SensorAInfo[SiteID][SensorID].CurValue='未接' then
begin
if ListViewAll.Canvas.Font.Color<>clYellow then
ListViewAll.Canvas.Font.Color := clYellow;
end
else
begin
if ListViewAll.Canvas.Font.Color<>clGreen then
ListViewAll.Canvas.Font.Color := clGreen;
end;
end;
end;
 
问题: 如何给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
多人接受答案了。

问题讨论没有结束 ...
 
写的是电话录音系统吧?断线报警??!!
没仔细看你的代码,如果是如你说的窗体变化时可以改变颜色.
有个简单的方法,就是加行代码:将需要改变颜色的Columns列尺寸+1,然后-1,就可以了
 
too 猎爱者:
你的办法不行,界面一直闪动!
 
在窗口的打印事件里写代码
 
加上BeginUpdate和EndUpdate
 
too 猎爱者:
这样也不行,加上BeginUpdate和EndUpdate

窗体一直闪动.
 
贴一段用BCB写的(我不用DELPHI),响应的是OnDrawItem事件.
void __fastcall TFrmSJShow::LVDrawItem(TCustomListView *Sender,
TListItem *Item, TRect &Rect, TOwnerDrawState State)
{ //根据用户设定颜色重画
TListView*LV=(TListView*)Sender;

TCanvas*can=LV->Canvas;
TListColumns*LC=LV->Columns;
TRect r;

if(State.Contains(odSelected))
can->Brush->Color=clSilver;
else
can->Brush->Color = clWindow;

if(!bUseColor)
can->Font->Color=clBlack;
else
{
int Style=FEventStyle->GetStyle(Item->SubItems->Strings[2]);
if(Style<0)
can->Font->Color=clBlack;
else can->Font->Color=cl[Style];
}
can->FillRect(Rect);
r=TRect(Rect.left,Rect.top,LC->Items[0]->Width,Rect.Bottom);
can->TextRect(r,r.left+1,r.Top,Item->Caption );
AnsiString str;
for(int i=1;i<LV->Columns->Count;i++)
{
r=TRect(r.right,r.top,r.right+LC->Items->Width,r.Bottom);
str=Item->SubItems->Strings[i-1];
can->TextRect(r,r.left+2,r.Top+1,str );
}
 
你的这段代码我完全明白什么意思。我也会用VC,但没用。改变颜色思路上没问题,可能就是某一点没把握住,怎么没人来帮我看看呢~!
 
试试这个
Inherited ;
DefaultDraw := False;
if (SubItem = 1) and (Item.Index = 1) then
begin
if Item.SubItems.Count > 0 then
ListView1.Canvas.Font.Color := clRed;
end;
DefaultDraw := True;
 
procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
Item: TListItem; SubItem: Integer; State: TCustomDrawState;
var DefaultDraw: Boolean);
begin
Inherited ;
DefaultDraw := False;
if (SubItem = 1) and (Item.Index = 1) then
begin
if Item.SubItems.Count > 0 then
ListView1.Canvas.Font.Color := clRed;
end;
DefaultDraw := True;
end;
 
后退
顶部