Easy way to modify progress bar in existing Delphi code?

喜欢而已 提交于 2019-12-25 09:03:46

问题


I am using demo code which compresses all files in folder. However, it's progress bar displays not total progress, but progress for every file.

Is there any easy way to modify code so progress bar would display total progress and not progress for every single file?

procedure DoProgress(Sender: TObject; Position, Total: Integer);
procedure DoCompressFile(Sender: TObject; const Filename: string);

....

procedure TJvZLibMultipleMainForm.DoCompressFile(Sender:TObject;const Filename:string);
begin
  lblFilename.Caption := Filename;
  Update;
end;

    procedure TJvZLibMultipleMainForm.btnCompressClick(Sender: TObject);
var
  z : TJvZlibMultiple;
begin
  ForceDirectories(ExtractFilePath(edFilename.Text));
  z := TJvZlibMultiple.Create(nil);
  Screen.Cursor := crHourGlass;
  try
    lblFilename.Caption := '';
    pbProgress.Position := 0;
    z.OnProgress := DoProgress;
    z.OnCompressingFile := DoCompressFile;
    z.CompressDirectory(edSrcFolder.Text,true,edFilename.Text);
  finally
    z.Free;
    Screen.Cursor := crDefault;
  end;
  pbProgress.Position := 0;
  lblFilename.Caption := 'Ready';
end;


procedure TJvZLibMultipleMainForm.DoProgress(Sender: TObject; Position, Total: Integer);
begin
  pbProgress.Max := Total;
  pbProgress.Position := Position;
  Update;
end;

回答1:


Or better yet, add up the size of the files to a total size and increment position by the number of bytes processed.




回答2:


A simple approach would be counting the files in the directory with Findfirst and Findnext, then setting pbProgress.Max to that and then incrementing pcProgress.Position by 1 in DoCompressFile.



来源:https://stackoverflow.com/questions/1448744/easy-way-to-modify-progress-bar-in-existing-delphi-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!