上一篇:Delphi控制ADSL拨号和断开
下一篇:Delphi定义静态变量

在TListView控件中添加进度条效果

发布于: 2012-12-11 22:47:21   |  发布在: Delphi文章   |  点击:

当然并不是真的添加一个进度条,而是通过添加ListView1的“OnAdvancedCustomDrawSubItem”事件的响应代码在指定的格子中绘制出一个进度条效果的方框,具体代码如下:

 procedure TForm1.ListView1AdvancedCustomDrawSubItem(
  Sender: TCustomListView; Item: TListItem; SubItem: Integer;
  State: TCustomDrawState; Stage: TCustomDrawStage;
  var DefaultDraw: Boolean);
const
  p_Col = 1; //需绘制进度条的列号
var
  r, tr: TRect;
  c: TCanvas;
  i: Integer;
  oldColor: TColor;
  pStr: String;
begin
  pStr := Item.SubItems[p_Col]; //百分制的进度值,如:75.50
  oldColor:= Sender.Canvas.Brush.Color;
  c := (Sender as TListView).Canvas;
  r := Item.DisplayRect(drBounds);
  for i:=0 to p_Col do r.Left := r.Left + (Sender as TListview).columns[i].Width;
  r.Right := r.Left + (Sender as TListview).columns[p_Col+1].Width - 2;
  r.Left := r.Left + 2;
  r.Top := r.Top + 2;
  r.Bottom := r.Bottom-2;
  c.Rectangle(r);
  tr.Left := r.Left + 1;
  tr.Top := r.Top + 1;
  tr.Bottom := r.Bottom - 1;
  tr.Right:=tr.Left + Trunc((r.Right - r.Left - 2) * StrToFloat(pStr) / 100);
  c.Brush.Color :=clYellow;
  c.FillRect(tr);
  SetBkMode(c.handle, TRANSPARENT);
  c.TextOut(r.Left + Trunc((r.Right - r.Left)/2) - 10, r.Top + 1 , pStr + '%');
  Sender.Canvas.Brush.Color := oldColor;
end;

进度条