给你放几个进度条的代码,你模仿一下吧:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, CommCtrl, Grids;
type
TLVData = ^LVData;
LVData = record
State: integer;
Prosition: Integer;
Max: integer;
Min: integer;
end;
TForm1 = class(TForm)
ListView1: TListView;
Button3: TButton;
Button: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ButtonClick(Sender: TObject);
procedure ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure DrawSubItem(LV: TListView; Item: TListItem; SubItem: Integer;
Prosition, Max, Style: Integer; DrawColor: TColor = $00005B00;
FrameColor: TColor = $00002F00);
function GetItemRect(LV_Handle, iItem, iSubItem: Integer): TRect;
var
Rect: TRect;
begin
ListView_GetSubItemRect(LV_Handle, iItem, iSubItem, LVIR_LABEL, @Rect);
Result := Rect;
end;
var
r: TRect;
i, iWidth: integer;
begin
try
with lv do
begin
//LockPaint := True;
r := GetItemRect(LV.Handle, Item.Index, SubItem);
// if SubItem = DrawSubItem then
begin
if Prosition >= Max then
Prosition := 100
else
if Prosition <= 0 then
Prosition := 0
else
Prosition := Round((Prosition / Max) * 100);
r := Item.DisplayRect(drBounds);
if SubItem > 0 then
for i := 0 to SubItem - 1 do
r.Left := r.Left + columns.Width; // + lv.columns[1].Width;
r.Right := r.Left + columns[SubItem].Width;
// InflateRect(r, -3, -1);
if Prosition = 0 then
begin
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));
case Style of
0:
begin
Canvas.Brush.Color := DrawColor;
r.Right := iWidth;
Canvas.FillRect(r);
end;
1:
begin
i := r.Left;
while i < iWidth do
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;
end;
Canvas.Brush.Color := Color;
end
// else
// Canvas.Brush.Color := Color;
//LockPaint := False;
end;
except
// lv.Repaint;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
r: TRect;
pb: TProgressBar;
begin
Listview1.Columns.Add.Width := 100;
Listview1.Columns.Add.Width := 200;
Listview1.ViewStyle := vsReport;
Listview1.Items.Add.Caption := 'Text';
r := Listview1.Items[0].DisplayRect(drBounds);
r.Left := r.Left + Listview1.columns[0].Width;
r.Right := r.Left + Listview1.columns[1].Width;
pb := TProgressBar.Create(Self);
pb.Parent := Listview1;
pb.BoundsRect := r;
pb.Position := 30;
Listview1.Items[0].Data := pb;
end;
// Change the ProgressBar Position
// ProgressBar Position ?ndern
procedure TForm1.Button2Click(Sender: TObject);
var
pb: TProgressBar;
begin
pb := TProgressBar(Listview1.Items[0].Data);
pb.StepIt;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
item: TListItem;
i: integer;
P: TLVData;
begin
for i := 0 to 10 do
begin
item := ListView1.Items.Add;
item.Caption := Inttostr(i);
item.SubItems.Add('000');
item.SubItems.Add('');
item.SubItems.Add('222');
item.SubItems.Add('333');
item.SubItems.Add('444');
New(P);
P^.State := 0;
P^.Prosition := 0;
P^.Max := 100;
P^.Min := 0;
Item.Data := P;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
item: TListItem;
begin
if ListView1.Selected <> nil then
begin
item := listview1.Selected;
if TLVData(item.Data)^.Prosition < TLVData(Item.Data)^.Max then
begin
TLVData(item.Data)^.Prosition := TLVData(item.Data)^.Prosition + 1;
DrawSubItem(ListView1,
item,
2,
TLVData(Item.Data)^.Prosition,
TLVData(Item.Data)^.Max,
1,
$00BBC5CA, //clBlue,
clGray);
end;
end;
end;
procedure TForm1.ButtonClick(Sender: TObject);
var
item: TListItem;
begin
if ListView1.Selected <> nil then
begin
item := listview1.Selected;
if TLVData(item.Data)^.Prosition <> 0 then
begin
TLVData(item.Data)^.Prosition := TLVData(item.Data)^.Prosition - 1;
DrawSubItem(ListView1,
item,
2,
TLVData(Item.Data)^.Prosition,
TLVData(Item.Data)^.Max,
1,
$00BBC5CA, //clBlue,
clGray);
end;
end;
end;
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
BoundRect, Rect: TRect;
i: integer;
TextFormat: Word;
LV: TListView;
begin
LV := ListView1;
BoundRect := Item.DisplayRect(drBounds);
InflateRect(BoundRect, -1, 0);
if Item.Selected then
begin
if cdsFocused in State then
begin
LV.Canvas.Brush.Color := $00ECCCB9; // //clHighlight;
LV.Canvas.Font.Color := clBtnText; //clHighlightText;
end
else
begin
LV.Canvas.Brush.Color := $00F8ECE5; //clSilver;
LV.Canvas.Font.Color := clBtnText;
end;
end
else
begin
LV.Canvas.Brush.Color := clWindow;
LV.Canvas.Font.Color := clWindowText;
end;
LV.Canvas.FillRect(BoundRect); //初始化背景
for i := 0 to LV.Columns.Count - 1 do
begin
//获取SubItem的Rect
ListView_GetSubItemRect(LV.Handle, Item.Index, i, LVIR_BOUNDS, @Rect);
case LV.Columns.Alignment of
taLeftJustify:
TextFormat := 0;
taRightJustify:
TextFormat := DT_RIGHT;
taCenter:
TextFormat := DT_CENTER;
end;
case i of
0: //画Caption
begin
InflateRect(Rect, -3, 0); //向后移3个像素,避免被后面画线框时覆盖
DrawText(
LV.Canvas.Handle,
PCHAR(Item.Caption),
Length(Item.Caption),
Rect,
DT_VCENTER or DT_SINGLELINE or DT_END_ELLIPSIS or TextFormat);
end;
1..MaxInt: //画Subitems
begin
if i - 1 <= Item.SubItems.Count - 1 then
DrawText(
LV.Canvas.Handle,
PCHAR(Item.SubItems[i - 1]),
Length(Item.SubItems[i - 1]),
Rect,
DT_VCENTER or DT_SINGLELINE or DT_END_ELLIPSIS or TextFormat);
if i - 1 = 1 then
begin
DrawSubItem(TListView(Sender),
item,
i,
20,
100,
// TLVData(Item.Data)^.Prosition,
// TLVData(Item.Data)^.Max,
0,
$00BBC5CA, //clBlue,
clGray);
end;
end;
end;
end;
LV.Canvas.Brush.Color := clWhite;
if Item.Selected then //画选中条外框
begin
if cdsFocused in State then
LV.Canvas.Brush.Color := $00DAA07A // $00E2B598; //clHighlight;
else
LV.Canvas.Brush.Color := $00E2B598; //$00DAA07A // clHighlight;
LV.Canvas.FrameRect(BoundRect); // DrawFocusRect(Item.DisplayRect(drBounds)); //
end;
DefaultDraw := False; //True;//cdsSelected in State;
with Sender.Canvas do
if Assigned(Font.OnChange) then Font.OnChange(Font);
end;
end.