unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ImgList;
type
TForm1 = class(TForm)
btn1: TButton;
lv1: TListView;
trckbr1: TTrackBar;
il1: TImageList;
procedure lv1CustomDraw(Sender: TCustomListView; const ARect: TRect; var DefaultDraw: Boolean);
procedure lv1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
procedure btn1Click(Sender: TObject);
procedure trckbr1Change(Sender: TObject);
private
function ReDrawItem(HwndLV: HWND; ItemIndex: integer): boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
CommCtrl;
{$R *.dfm}
//代码来源:http://www.cnblogs.com/snow001x/archive/2008/12/04/1347854.html
//画状态条
procedure DrawSubItem(LV: TListView; Item: TListItem; SubItem: Integer; Prosition: Single; Max, Style: Integer; IsShowProgress: Boolean; DrawColor: TColor = $00005B00; FrameColor: TColor = $00002F00);
//获取SubItem的区域
function GetItemRect(hWndLV: HWnd; iItem, iSubItem: Integer): TRect;
var
Rect: TRect;
begin
ListView_GetSubItemRect(hWndLV, iItem, iSubItem, iSubItem, @Rect);
Result := Rect;
end;
var
PaintRect, R: TRect;
i, iWidth, x, y: Integer;
S: string;
begin
with lv do
begin
PaintRect := GetItemRect(lv.Handle, Item.Index, SubItem);
R := PaintRect;
if Prosition >= Max then
Prosition := 100
else
begin
if Prosition <= 0 then
Prosition := 0
else
Prosition := Round((Prosition / MAX) * 100);
end;
if (Prosition = 0) and (not IsShowProgress) then
Canvas.FillRect(r) //如果是0 ,直接显示空白
else
begin
//先填充背景
Canvas.FillRect(r);
Canvas.Brush.Color := Color;
//画一个外框
InflateRect(R, -2, -2);
Canvas.Brush.Color := FrameColor;
Canvas.FrameRect(R);
Canvas.Brush.Color := Color;
InflateRect(R, -1, -1);
//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; //case end
//画好进度条后,现在要做的就是显示进度数字了
Canvas.Brush.Style := bsClear;
if Prosition = Round(Prosition) then
S := Format('%d%%', [Round(Prosition)])
else
S := FormatFloat('#0.0', Prosition);
with PaintRect do
begin
x := Left + (Right - Left + 1 - Canvas.TextWidth(S)) div 2;
y := Top + (Bottom - Top + 1 - Canvas.TextHeight(S)) div 2;
end;
SetBkMode(Canvas.Handle, TRANSPARENT);
Canvas.TextRect(PaintRect, x, y, S);
end;
//画完恢复
Canvas.Brush.Color := Color;
end;
end;
procedure TForm1.lv1CustomDraw(Sender: TCustomListView; const ARect: TRect; var DefaultDraw: Boolean);
begin
end;
//上面是画进度条的,现在要给TlistView处理Item重绘的消息,事件是OnCustomDrawItem,需要说明的是,如果想要随心所欲的自画Item,那么就要全部自己来完成,不再需要系统来处理:
procedure TForm1.lv1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
BoundRect, Rect: TRect;
i: integer;
TextFormat: Word;
LV: