在现有的Delphi代码中修改进度条的简单方法?
我正在使用演示代码来压缩文件夹中的所有文件。但是,它的进度条显示的不是总进度,而是每个文件的进度。
是否有任何简单的方法可以修改代码,以便进度条显示总进度而不是每个文件的进度?
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者更好的是,将文件的大小添加到总大小中,并按处理的字节数增加位置。
Or better yet, add up the size of the files to a total size and increment position by the number of bytes processed.
一种简单的方法是使用 Findfirst 和 Findnext 计算目录中的文件,然后将 pbProgress.Max 设置为该值,然后在 DoCompressFile 中将 pcProgress.Position 加 1。
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.