Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@owlsperspective
Created August 9, 2013 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owlsperspective/6191593 to your computer and use it in GitHub Desktop.
Save owlsperspective/6191593 to your computer and use it in GitHub Desktop.
Win32APIのCopyFileExのコールバックを受け入れる
// Delphi 2007 and XE3
// Create form 'TForm1', place 2 Edit, 2 Button and 1 Label.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
FAborted: Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{$WARN SYMBOL_PLATFORM OFF}
type
TCopyProgressRoutine = function (TotalFileSize: Int64;
TotalBytesTransferred: Int64;
StreamSize: Int64;
StreamBytesTransferred: Int64;
dwStreamNumber: DWORD;
dwCallbackReason: DWORD;
hSourceFile: THandle;
hDestinationFile: THandle;
lpData: Pointer): DWORD; stdcall;
function CopyFileEx(lpExistingFileName: PChar;
lpNewFileName: PChar;
lpProgressRoutine: TCopyProgressRoutine;
lpData: Pointer;
pbCancel: PBool;
dwCopyFlags: DWORD): BOOL; stdcall; external kernel32
{$IFDEF UNICODE}
name 'CopyFileExW';
{$ELSE}
name 'CopyFileExA';
{$ENDIF}
{$EXTERNALSYM CopyFileEx}
const
COPY_FILE_NO_BUFFERING = $00001000;
function CopyProgressFunc(TotalFileSize: Int64;
TotalBytesTransferred: Int64;
StreamSize: Int64;
StreamBytesTransferred: Int64;
dwStreamNumber: DWORD;
dwCallbackReason: DWORD;
hSourceFile: THandle;
hDestinationFile: THandle;
lpData: Pointer): DWORD; stdcall;
var
TBT: Extended;
TFS: Extended;
begin
TFS := TotalFileSize;
TBT := TotalBytesTransferred;
with TObject(lpData) as TForm1 do
begin
if (TotalFileSize = 0) or (TotalBytesTransferred = 0) then
begin
Label1.Caption := '';
end
else
begin
Label1.Caption := Format('%.0n / %.0n bytes',[TBT,TFS]);
end;
Application.ProcessMessages;
if FAborted = True then
begin
Result := PROGRESS_CANCEL;
end
else
begin
Result := PROGRESS_CONTINUE;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Canceled: BOOL;
CopyFlags: DWORD;
begin
FAborted := False;
Button1.Enabled := False;
try
Canceled := False;
CopyFlags := COPY_FILE_FAIL_IF_EXISTS;
if CheckWin32Version(6,0) then
begin
CopyFlags := CopyFlags or COPY_FILE_NO_BUFFERING;
end;
Win32Check(CopyFileEx(PChar(Edit1.Text),PChar(Edit2.Text),
CopyProgressFunc,Self,@Canceled,CopyFlags));
finally
Button1.Enabled := True;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FAborted := True;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment