Delphi带进度条复制文件

function FileCopy(SourceFile,TargetFile : string;ProgressBar :TRzProgressStatus ) : boolean;

//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;

posted @ 2011-09-16 14:34  Max Woods  阅读(745)  评论(0编辑  收藏  举报