delphi带进度条复制文件

     阅读 1981 次    更新时间:2015/2/4    
delphi带进度条复制文件

function FileCopy(SourceFile,TargetFile : string;ProgressBar :TProgressBar ) : boolean;
var
getStream,setStream: TFileStream;
num, n: Integer;
buf: PByte;
BufSize,block: Integer;
begin
result := false;
if not FileExists(SourceFile) then
begin
//ShowMessage('源文件不存在!');
Exit;
end;

getStream := TFileStream.Create(SourceFile, fmOpenRead or fmShareExclusive);
setStream := TFileStream.Create(TargetFile, fmCreate);

num := getStream.Size;
setStream.Size := num;
getStream.Position := 0;
setStream.Position := 0;

BufSize := num;
block := BufSize div 100;
GetMem(buf, BufSize);

 ProgressBar.Max := 100;
//ProgressBar.Percent := 0;
//ProgressBar.min := 0;
ProgressBar.Position := 0;

while num <> 0 do
begin
Application.ProcessMessages;
n := block;
if n > num then n := num;
getStream.ReadBuffer(buf^, n);
setStream.WriteBuffer(buf^, n);
//ProgressBar.Percent := Trunc((1 - num / BufSize)*100);
ProgressBar.Position := Trunc((1 - num / BufSize)*100);
Dec(num, n);
end;
//ProgressBar.Percent := 0;
ProgressBar.Position := 0;
FreeMem(buf, BufSize);
getStream.Free;
setStream.Free;
result := true;
end;

 
 

Copyright 2003-2008 All Rights Reserved 自由风工作室 版权没有 [湘ICP备06002185号]
.