Fórum Evolução de cópia de um único arquivo no ProgressBar #254713

16/10/2004

0

Bom dia programadores,

Como usar um ProgressBar na cópia de [color=darkblue:622696edca]um único arquivo[/color:622696edca], tipo:

CopyFile(PChar(´C:\ClubeMG\Oficina\CMG.exe´),PChar(´F:\ClubeMG\Oficina\CMG.exe´), true);

Agradeço.


Vetorzero

Vetorzero

Responder

Posts

16/10/2004

Aroldo Zanela

Colega,

Veja esta dica:

[quote:034d81d781=´Peter Below (TeamB)´]
Lets assume you call CopyFileEx and want the callback to update a progress bar. The callback cannot be an objects method but you can use the lpData parameter of CopyFileEx to pass any kind of data to the callback, e.g. a form reference. So, if you want to serve a progress form in the callback that would look like this:[/quote:034d81d781]

type
  TProgressForm = class(TForm)
    AbortButton: TButton;
    ProgressBar: TProgressBar;
    procedure AbortButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    FCancel: BOOL;
  end;
  // form has fsStayOnTop formstyle!

implementation

{$R *.DFM}

procedure TProgressForm.AbortButtonClick(Sender: TObject);
begin
  FCancel := True;
end;
   
// Note: could use int64 instead of COMP, but that would make this D4 specific
Function CopyCallback( TotalFileSize, TotalBytesTransferred, 
                       StreamSize, StreamBytesTransferred: COMP; 
                       dwStreamNumber, dwCallbackReason : DWORD;
                       hSourceFile, hDestinationFile: THandle;
                       progressform: TProgressForm ): DWORD; stdcall;
Var
  newpos: Integer;
Begin
  Result := PROCESS_CONTINUE;
  If dwCallbackReason = CALLBACK_CHUNK_FINISHED Then Begin
    newpos := Round( TotalBytesTransferred / TotalFileSize * 100 );
    With progressform.Progressbar Do
      if newpos <> Position then
        Position := newpos;
    Application.ProcessMessages; 
  End; { If }
End; { CopyCallback }

Function DoFilecopy( Const source, target: String ): Boolean;
Var
  progressform: TProgressForm;
Begin
  progressform := TProgressform.Create;
  try
    progressform.Show;
    Application.ProcessMessages;
    Result := CopyFileEx(
                PChar( source ),
                PChar( target ),
                @CopyCallback,
                Pointer( progressform ),
                @progressform.FCancel,
                0 );
 finally
   progressform.Hide;
   progressform.free;
 end;
end; { DoFileCopy }


Note that this is completely untested! Also not that this function is not
implemented on Win95.

Why do you want to go to all this hassle when ShFileOperation does it all for you, including the progress bar?
Peter Below (TeamB) 100113.1101@compuserve.com)
No replies in private e-mail, please, unless explicitly requested!


Responder

Gostei + 0

16/10/2004

Vetorzero

Prezado Aroldo,

E como faço para acionar a função ´[color=blue:177c5c9006]ShFileOperation[/color:177c5c9006]´ ?


Responder

Gostei + 0

16/10/2004

Aroldo Zanela

Colega,

Crie um novo forumulário e adicione na lista de uses a unit ShellAPI, em seguida, solte um Button e no evento OnClick adicione o seguinte código:

procedure TForm1.Button1Click(Sender: TObject);
var
  OpStruc: TSHFileOpStruct;
  frombuf, tobuf: Array [0..128] of Char;
Begin
  fillChar( OpStruc, Sizeof(OpStruc), 0 );
  FillChar( frombuf, Sizeof(frombuf), 0 );
  FillChar( tobuf, Sizeof(tobuf), 0 );
  StrPCopy( frombuf, ´c:\temp\*.*´ );
  StrPCopy( tobuf, ´c:\lixo\tmp´ );
  With OpStruc DO Begin
    Wnd:= Handle;
    wFunc:= FO_COPY;
    pFrom:= @frombuf;
    pTo:=@tobuf;
    fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
    fAnyOperationsAborted:= False;
    hNameMappings:= Nil;
    lpszProgressTitle:= Nil;
  end;
  ShFileOperation( OpStruc );
end;


Note que em caso do diretório destino não exista, será enviada uma mensagem para confirmar a criação do mesmo e sua barra de progressão será mostrada em conjunto com a CommonAVI ´CopyFile´ padrão do Win32.


Responder

Gostei + 0

16/10/2004

Vetorzero

!!! Bingo !!!


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar