this slowpoke moves

Stream ProgressBar Adapter

Unit StreamProgressAdapter.pas

unit StreamProgressAdapter;

interface

uses
  SysUtils, Classes;

type
  TOnProgress = procedure(Sender: TObject; Size, CurrentPos: Int64) of
    object;
  TStreamProgressAdapter = class(TStream)
  private
    FStream: TStream;
    FCurrentPos: Int64;
    FMax: Integer;
    FPercent: Integer;
    FOnProgress: TOnProgress;
  protected
    procedure SetSize(NewSize: Longint); override;
  public
    constructor Create(AStream: TStream);
    destructor Destroy; override;
    function Read(var Buffer; Count: Longint): Longint; override;
    function Write(const Buffer; Count: Longint): Longint; override;
    function Seek(Offset: Longint; Origin: Word): Longint; override;
    procedure DoProgress(Writing: Boolean); virtual;
  published
    property OnProgress: TOnProgress read FOnProgress write FOnProgress;
  end;

implementation

procedure TStreamProgressAdapter.SetSize(NewSize: Longint);
begin
  TStreamProgressAdapter(FStream).SetSize(NewSize);
end;

constructor TStreamProgressAdapter.Create(AStream: TStream);
begin
  inherited Create;
  FStream := AStream;
  FMax := FStream.Size;
  FPercent := 0;
end;

destructor TStreamProgressAdapter.Destroy;
begin
  FStream.Free;
  inherited Destroy;
end;

function TStreamProgressAdapter.Read(var Buffer; Count: Longint): Longint;
begin
  Result := FStream.Read(Buffer, Count);
  FCurrentPos := FStream.Position;
  DoProgress(False);
end;

function TStreamProgressAdapter.Write(const Buffer; Count: Longint): Longint;
begin
  Result := FStream.Write(Buffer, Count);
  DoProgress(False);
end;

function TStreamProgressAdapter.Seek(Offset: Longint; Origin: Word): Longint;
begin
  Result := FStream.Seek(Offset, Origin);
end;

procedure TStreamProgressAdapter.DoProgress(Writing: Boolean);
begin
  //FPercent := Round(100 / FMax * FCurrentPos);
  if Assigned(OnProgress) then
    OnProgress(self, FMax, FCurrentPos);
end;

end.


Unit Datei Kopieren :
uses StreamProgressAdapter, ComCtrls, Dialogs

private
    { Private-Deklarationen }
    procedure Progress(Sender: TObject; Size, CurrentPos: Int64);
    
//

procedure TForm1.Progress;
begin
  Progressbar1.Max := Size;
  Progressbar1.Position := CurrentPos;
  Progressbar1.Refresh;
  //Sleep(2000);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  SrcFile: TStreamProgressAdapter;
  DestFile: TFileStream;
  Filename: string;
begin
  if OpenDialog1.Execute then
  begin
    Filename := OpenDialog1.FileName;
    SrcFile := TStreamProgressAdapter.Create(TFileStream.Create(FileName,
      fmOpenRead or fmShareDenyNone));
    try
      SrcFile.OnProgress := Progress;
      DestFile := TFileStream.Create(Filename + '.copy', fmCreate);
      try
        DestFile.CopyFrom(SrcFile, SrcFile.Size);
      finally
        DestFile.Free;
      end;
    finally
      SrcFile.Free;
    end;
  end;
end;

Unit Grafik Laden :
uses StreamProgressAdapter, ComCtrls, Dialogs

private
    { Private-Deklarationen }
    procedure Progress(Sender: TObject; Size, CurrentPos: Int64);
    
//

procedure TForm1.Progress;
begin
  Progressbar1.Max := Size;
  Progressbar1.Position := CurrentPos;
  Progressbar1.Refresh;
  //Sleep(2000);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  SrcFile: TStreamProgressAdapter;
  DestFile: TFileStream;
  Filename: string;
begin
  if OpenDialog1.Execute then
  begin
    Filename := OpenDialog1.FileName;
    SrcFile := TStreamProgressAdapter.Create(TFileStream.Create(FileName,
      fmOpenRead or fmShareDenyNone));
    try
      SrcFile.OnProgress := Progress;
      DestFile := TFileStream.Create(Filename + '.copy', fmCreate);
      try
        DestFile.CopyFrom(SrcFile, SrcFile.Size);
      finally
        DestFile.Free;
      end;
    finally
      SrcFile.Free;
    end;
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate