TListView有丰富的表现形式,可以显示大图标,小图标,详细信息等。以下过程介绍如何在显示详细信息方式时,在其中的某一个子项上画出进度条。这对于设备监控程序是比较有用的,可用来表示设备连接的时间等。 TListView 有一个AdvancedCustomDrawSubItem 方法,双击这个名称即可进入以下函数体。
//在指定的子项上自画进度条。倒计数减
procedure Tfm_SocketManage.LV_StationAdvancedCustomDrawSubItem(
Sender: TCustomListView; Item: TListItem; SubItem: Integer;
State: TCustomDrawState; Stage: TCustomDrawStage;
var DefaultDraw: Boolean);
var
i:integer;
cvs:TCanvas;
tr:TRect;
w:integer;
begin
if Item.SubItems.Count <5 then exit; //如果显示子项数不足5个,不画。当Item还在建立时就开始画可能会出错
try
if subitem=6 then //当前要画的子项是第6项
begin
psck:=PSocketRecord(Item.Data); //跟这个项显示内容有关的结构体,保存应该画多少的值
if (psck<>nil) and (psck.overtime <=17) then i:=psck.overtime
else i:=0;
if Boolean(ListView_GetSubItemRect(sender.Handle,item.Index,subitem,LVIR_BOUNDS,@tr)) then //取得要画的区域
begin
cvs:=sender.Canvas; //绘画的TCanvas
w:=tr.Right -tr.Left; //确定要画的范围。
if w=0 then w:=1;
w:=(17-i)*w div 17;
tr.Top :=tr.Top +3;
tr.Right :=tr.Left +w;
Dec(tr.Bottom,4);
cvs.Brush.Color :=clBlue; //设定画刷颜色
cvs.FillRect(tr); //填充要画的区域
end;
DefaultDraw:=false; //不需要下一步再绘画了
end;
except
end;
end;
只要Canvas对象取到了,在上面画些什么就完全可以发挥创意性了,画图,打字,渐变,能想的都可以做。
本文介绍在TListView的详细视图中为特定子项绘制进度条的方法。通过使用TListView的AdvancedCustomDrawSubItem方法,可以在子项上自定义绘制进度条,适用于监控设备连接时间等场景。
881

被折叠的 条评论
为什么被折叠?



