现在的位置: 首页 > 综合 > 正文

Delphi带进度条复制文件

2012年11月14日 ⁄ 综合 ⁄ 共 1113字 ⁄ 字号 评论关闭

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;

抱歉!评论已关闭.