procedure TForm1.AddItemButtonClick(Sender: TObject); const pbColumnIndex = 1; pbMax = 100; var li : TListItem; lv : TListView; pb : TProgressBar; pbRect : TRect; begin lv := ListViewEx1; //建立一个新的ListItem li := lv.Items.Add; li.Caption := ’Item ’ + IntToStr(lv.Items.Count); //建立一个ProgressBar,置入TListView的第二列中 pb := TProgressBar.Create(nil); pb.Parent := lv; li.Data := pb; pbRect := li.DisplayRect(drBounds); pbRect.Left := pbRect.Left + lv.Columns[-1 + pbColumnIndex].Width; pbRect.Right := pbRect.Left + lv.Columns[pbColumnIndex].Width; pb.BoundsRect := pbRect; end; //添加ItemButton事件 |
procedure TForm1.RemoveItemButtonClick(Sender: TObject); var lv : TListView; li : TListItem; i, idx : integer; pb : TProgressBar; begin lv := ListViewEx1; li := lv.Selected; if li <> nil then begin idx := li.Index; TProgressBar(li.Data).Free;//先释放TProgressBar lv.Items.Delete(idx); //把行向上移动 for i := idx to -1 + lv.Items.Count do begin li := lv.Items.Item[i]; pb := TProgressBar(li.Data); pb.Top := pb.Top - (pb.BoundsRect.Bottom - pb.BoundsRect.Top); end; end; end; //删除ItemButton事件 |
procedure TForm1.Timer1Timer(Sender: TObject); var idx : integer; pb: TProgressbar; lv : TListView; begin lv := ListViewEx1; if lv.Items.Count = 0 then Exit; //随机生成一个数据项 //根据生成的数据来控制TProgressBar的长度 idx := Random(lv.Items.Count); pb := TProgressBar(lv.Items[idx].Data); if pb.Position < pb.Max then pb.StepIt else pb.Position := 0; end;//Timer事件 |