如何在SaveToFile
方法中使用进度条?实际上,我想将资源保存到文件中,并在保存时将进度条从0%更新到100%,我该怎么做?
如何在SaveToFile
方法中使用进度条?实际上,我想将资源保存到文件中,并在保存时将进度条从0%更新到100%,我该怎么做?
type
TForm1 = class(TForm)
Button: TButton;
ProgressBar: TProgressBar;
procedure ButtonClick(Sender: TObject);
private
procedure StreamProgress(Sender: TObject; Percentage: Single);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TStreamProgressEvent = procedure(Sender: TObject;
Percentage: Single) of object;
TProgressResourceStream = class(TResourceStream)
private
FOnProgress: TStreamProgressEvent;
public
procedure SaveToFile(const FileName: TFileName);
property OnProgress: TStreamProgressEvent read FOnProgress
write FOnProgress;
end;
{ TProgressResourceStream }
procedure TProgressResourceStream.SaveToFile(const FileName: TFileName);
var
Count: Int64;
Stream: TStream;
BlockSize: Int64;
P: PAnsiChar;
WriteCount: Int64;
begin
if Assigned(FOnProgress) then
begin
Count := Size;
if Count <> 0 then
begin
Stream := TFileStream.Create(FileName, fmCreate);
try
if Count < 500 then
BlockSize := 5
else
BlockSize := Count div 50;
P := Memory;
WriteCount := 0;
while WriteCount < Count do
begin
if WriteCount < Count - BlockSize then
Inc(WriteCount, Stream.Write(P^, BlockSize))
else
Inc(WriteCount, Stream.Write(P^, Count - WriteCount));
Inc(P, BlockSize);
FOnProgress(Self, WriteCount / Count);
end;
finally
Stream.Free;
end;
end;
end
else
inherited SaveToFile(FileName);
end;
{ TForm1 }
procedure TForm1.ButtonClick(Sender: TObject);
var
Stream: TProgressResourceStream;
begin
ProgressBar.Min := 0;
Stream := TProgressResourceStream.Create(HInstance, 'TFORM1', RT_RCDATA);
try
Stream.OnProgress := StreamProgress;
Stream.SaveToFile('TForm1.dat');
finally
Stream.Free;
end;
end;
procedure TForm1.StreamProgress(Sender: TObject; Percentage: Single);
begin
with ProgressBar do
Position := Round(Percentage * Max);
end;
P: PChar;
更改为P: PAnsiChar;
吗? - NGLN由于它继承自TStream,因此您可以使用Size属性获取总大小,并使用Position获取当前位置。您可以使用这些来“驱动”进度条。然后,您不会使用SaveToFile来写入文件,而是使用单独的TFileStream,并从TResourceStream按块写入它。您可以使用TStream.CopyFrom方法进行最后一部分。
CopyFrom
方法。 - Kermia