delphi listview自绘图形显示进度条 分颜色显示.docx
《delphi listview自绘图形显示进度条 分颜色显示.docx》由会员分享,可在线阅读,更多相关《delphi listview自绘图形显示进度条 分颜色显示.docx(10页珍藏版)》请在冰豆网上搜索。
delphilistview自绘图形显示进度条分颜色显示
delphilistview自绘图形显示进度条分颜色显示
delphilistview自绘图形显示进度条分颜色显示
2011-05-3010:
31delphilistview自绘图形显示进度条自画TlistView带进度条的Item
TListView的Item条一般是由系统自画的,但电驴就实现了自画,使之看起来很漂亮,我们用DELPHI也可以实现!
首先要引用CommCtrl单元,这是TListView底层控制单元:
uses
CommCtrl;
//画状态条
procedureDrawSubItem(LV:
TListView;Item:
TListItem;SubItem:
Integer;
Prosition:
Single;Max,Style:
Integer;IsShowProgress:
Boolean;
DrawColor:
TColor=$00005B00;
FrameColor:
TColor=$00002F00);
//获取SubItem的区域
functionGetItemRect(LV_Handle,iItem,iSubItem:
Integer):
TRect;
var
Rect:
TRect;
begin
ListView_GetSubItemRect(LV_Handle,iItem,iSubItem,LVIR_LABEL,@Rect);
Result:
=Rect;
end;
var
PaintRect,r:
TRect;
i,iWidth,x,y:
integer;
S:
string;
begin
try
withlvdo
begin
//LockPaint:
=True;
PaintRect:
=GetItemRect(LV.Handle,Item.Index,SubItem);
r:
=PaintRect;
//ifSubItem=DrawSubItemthen
Begin
//这一段是算出百分比
ifProsition>=Maxthen
Prosition:
=100
else
ifProsition<=0then
Prosition:
=0
else
Prosition:
=Round((Prosition/Max)*100);
if(Prosition=0)and(notIsShowProgress)then
begin
//如果是百分比是0,就直接显示空白
Canvas.FillRect(r);
end
else
begin
//先直充背景色
Canvas.FillRect(r);
Canvas.Brush.Color:
=Color;
//Canvas.FillRect(r);
//画一个外框
InflateRect(r,-2,-2);
Canvas.Brush.Color:
=FrameColor;//$00002F00;
Canvas.FrameRect(R);
Canvas.Brush.Color:
=Color;
InflateRect(r,-1,-1);
//Canvas.FillRect(r);
InflateRect(r,-1,-1);
//根据百分比算出要画的进度条内容宽度
iWidth:
=R.Right-Round((R.Right-r.Left)*((100-Prosition)/
100));
caseStyleof
0:
//进度条类型,实心填充
begin
Canvas.Brush.Color:
=DrawColor;
r.Right:
=iWidth;
Canvas.FillRect(r);
end;
1:
//进度条类型,竖线填充
begin
i:
=r.Left;
whilei<iWidthdo
begin
Canvas.Pen.Color:
=Color;
Canvas.MoveTo(i,r.Top);
Canvas.Pen.Color:
=DrawColor;
canvas.LineTo(i,r.Bottom);
Inc(i,3);
end;
end;
end;
//画好了进度条后,现在要做的就是显示进度数字了
Canvas.Brush.Style:
=bsClear;
ifProsition=Round(Prosition)then
S:
=Format('%d%%',[Round(Prosition)])
else
S:
=FormatFloat('#0.0',Prosition);
withPaintRectdo
begin
x:
=Left+(Right-Left+1-Canvas.TextWidth(S))div2;
y:
=Top+(Bottom-Top+1-Canvas.TextHeight(S))div2;
end;
SetBkMode(Canvas.handle,TRANSPARENT);
Canvas.TextRect(PaintRect,x,y,S);
end;
//进度条全部画完,把颜色设置成默认色了
Canvas.Brush.Color:
=Color;
end
end;
except
end;
end;
上面是画进度条的,现在要给TlistView处理Item重绘的消息,事件是OnCustomDrawItem,需要说明的是,如果想要随心所欲的自画Item,那么就要全部自己来完成,不再需要系统来处理:
procedureTForm1.ListView1CustomDrawItem(
Sender:
TCustomListView;Item:
TListItem;State:
TCustomDrawState;
varDefaultDraw:
Boolean);
var
BoundRect,Rect:
TRect;
i:
integer;
TextFormat:
Word;
LV:
TListView;
//这个子过程是用来画CheckBox和ImageList的
procedureDraw_CheckBox_ImageList(r:
TRect;aCanvas:
TCanvas;Checked:
Boolean);
var
R1:
TRect;
i:
integer;
begin
ifSender.Checkboxesthen
begin
aCanvas.Pen.Color:
=clBlack;
aCanvas.Pen.Width:
=2;
//画CheckBox外框
aCanvas.Rectangle(r.Left+2,r.Top+2,r.Left+14,r.Bottom-2);
ifCheckedthen
begin//画CheckBox的勾
aCanvas.MoveTo(r.Left+4,r.Top+6);
aCanvas.LineTo(r.Left+6,r.Top+11);
aCanvas.LineTo(r.Left+11,r.Top+5);
end;
aCanvas.Pen.Width:
=1;
end;
//开始画图标
i:
=PDownLoadListItem(Item.Data)^.StatsImageIndex;
ifi>-1then
begin
//获取图标的RECT
ifBoolean(ListView_GetSubItemRect(sender.Handle,item.Index,0,LVIR_ICON,@R1))then
begin
ImageList_Stats.Draw(LV.Canvas,R1.Left,R1.Top,i);
ifitem.ImageIndex>-1then
LV.SmallImages.Draw(LV.Canvas,R1.Right+2,R1.Top,item.ImageIndex);
end;
end;
end;
begin
LV:
=ListView1;
BoundRect:
=Item.DisplayRect(drBounds);
InflateRect(BoundRect,-1,0);
//这个地方你可以根据自己的要求设置成想要的颜色,实现突出显示
LV.Canvas.Font.Color:
=clBtnText;
//查看是否是被选中
ifItem.Selectedthen
begin
ifcdsFocusedinStatethen
begin
LV.Canvas.Brush.Color:
=$00ECCCB9;////clHighlight;
end
else
begin
LV.Canvas.Brush.Color:
=$00F8ECE5;//clSilver;
end;
end
else
begin
if(Item.Indexmod2)=0then
LV.Canvas.Brush.Color:
=clWhite
else
LV.Canvas.Brush.Color:
=$00F2F2F2;
end;
LV.Canvas.FillRect(BoundRect);//初始化背景
fori:
=0toLV.Columns.Count-1do
begin
//获取SubItem的Rect
ListView_GetSubItemRect(LV.Handle,Item.Index,i,LVIR_LABEL,@Rect);
caseLV.Columns[i].Alignmentof
taLeftJustify:
TextFormat:
=0;
taRightJustify:
TextFormat:
=DT_RIGHT;
taCenter:
TextFormat:
=DT_CENTER;
end;
caseiof
0:
//画Caption,0就是表示Caption,这不是Subitems[0]
begin
//先画选择框与图标
Draw_CheckBox_ImageList(BoundRect,LV.Canvas,Item.Checked);
//再画Caption的文字
InflateRect(Rect,-(5+ImageList_Stats.Width),0);//向后移3个像素,避免被后面画线框时覆盖
DrawText(
LV.Canvas.Handle,
PCHAR(Item.Caption),
Length(Item