不会用这个函数,谁能提供一个示例,特别是该函数回调函数的使用,先说声谢谢!
解决方案 »
- 文本去掉空行------------的问题
- 十万火急,高手请进
- sql 有关集合的查询及表的设定
- spcomm接收串口设备回应的信息
- 用ClientDataSet调用存储过程返回的数据集可以进行Edit操作吗?
- 为什么用delphi7的TImage控件无法显示用photoshop转换来的.jpg文件呢?
- 请问关闭adoconnection后,再次打开连接时为何会出现'OLE error 80030002'错误!急~~~~~~~~~~~~~~~~~~~~~~~~~~~!!
- 求助: 像TPoint、TRect这样的对象如何销毁
- 怎么改变状态栏字体的颜色?
- 线程同步 马上送满分一人
- 简单问题,来者有分!
- 下个月去大连,麻烦熟悉的兄弟给简单介绍一下情况!
【作者】Thomas Stutz
【內文】http://delphi.ktop.com.tw/topic.asp?topic_id=56095程式來源:http://www.swissdelphicenter.ch/en/showcode.php?id=330{ 1. } {
You need a TProgressBar on your form for this tip.
Für diesen Tip wird eine TProgressBar benötigt.
}
procedure TForm1.CopyFileWithProgressBar1(Source, Destination: string);
var
FromF, ToF: file of byte;
Buffer: array[0..4096] of char;
NumRead: integer;
FileLength: longint;
begin
AssignFile(FromF, Source);
reset(FromF);
AssignFile(ToF, Destination);
rewrite(ToF);
FileLength := FileSize(FromF);
with Progressbar1 do
begin
Min := 0;
Max := FileLength;
while FileLength > 0 do
begin
BlockRead(FromF, Buffer[0], SizeOf(Buffer), NumRead);
FileLength := FileLength - NumRead;
BlockWrite(ToF, Buffer[0], NumRead);
Position := Position + NumRead;
end;
CloseFile(FromF);
CloseFile(ToF);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
CopyFileWithProgressBar1('c:\Windows\Welcome.exe', 'c:\temp\Welcome.exe');
end; { 2. } {***************************************} // To show the estimated time to copy a file: procedure TForm1.CopyFileWithProgressBar1(Source, Destination: string);
var
FromF, ToF: file of byte;
Buffer: array[0..4096] of char;
NumRead: integer;
FileLength: longint;
t1, t2: DWORD;
maxi: integer;
begin
AssignFile(FromF, Source);
reset(FromF);
AssignFile(ToF, Destination);
rewrite(ToF);
FileLength := FileSize(FromF);
with Progressbar1 do
begin
Min := 0;
Max := FileLength;
t1 := TimeGetTime;
maxi := Max div 4096;
while FileLength > 0 do
begin
BlockRead(FromF, Buffer[0], SizeOf(Buffer), NumRead);
FileLength := FileLength - NumRead;
BlockWrite(ToF, Buffer[0], NumRead);
t2 := TimeGetTime;
Min := Min + 1;
// Show the time in Label1
label1.Caption := FormatFloat('0.00', ((t2 - t1) / min * maxi - t2 + t1) / 100);
Application.ProcessMessages;
Position := Position + NumRead;
end;
CloseFile(FromF);
CloseFile(ToF);
end;
end; { 3. }
{***************************************}
// To show the estimated time to copy a file, using a callback function: type
TCallBack = procedure(Position, Size: Longint); { export; } procedure FastFileCopy(const InFileName, OutFileName: string;
CallBack: TCallBack);
implementation procedure FastFileCopyCallBack(Position, Size: Longint);
begin
Form1.ProgressBar1.Max := Size;
Form1.ProgressBar1.Position := Position;
end; procedure FastFileCopy(const InFileName, OutFileName: string;
CallBack: TCallBack);
const
BufSize = 3 * 4 * 4096; { 48Kbytes gives me the best results }
type
PBuffer = ^TBuffer;
TBuffer = array[1..BufSize] of Byte;
var
Size: DWORD;
Buffer: PBuffer;
infile, outfile: file;
SizeDone, SizeFile: LongInt;
begin
if (InFileName <> OutFileName) then
begin
buffer := nil;
Assign(infile, InFileName);
Reset(infile, 1);
try
SizeFile := FileSize(infile);
Assign(outfile, OutFileName);
Rewrite(outfile, 1);
try
SizeDone := 0;
New(Buffer);
repeat
BlockRead(infile, Buffer^, BufSize, Size);
Inc(SizeDone, Size);
CallBack(SizeDone, SizeFile);
BlockWrite(outfile, Buffer^, Size)
until Size < BufSize;
FileSetDate(TFileRec(outfile).Handle,
FileGetDate(TFileRec(infile).Handle));
finally
if Buffer <> nil then
Dispose(Buffer);
CloseFile(outfile)
end;
finally
CloseFile(infile);
end;
end
else
raise EInOutError.Create('File cannot be copied onto itself')
end; {FastFileCopy}
procedure TForm1.Button1Click(Sender: TObject);
begin
FastFileCopy('c:\daten.txt', 'c:\test\daten2.txt', @FastFileCopyCallBack);
end; { 4. }
{***************************************}
function CopyFileWithProgressBar2(TotalFileSize,
TotalBytesTransferred,
StreamSize,
StreamBytesTransferred: LARGE_INTEGER;
dwStreamNumber,
dwCallbackReason: DWORD;
hSourceFile,
hDestinationFile: THandle;
lpData: Pointer): DWORD; stdcall;
begin
// just set size at the beginning
if dwCallbackReason = CALLBACK_STREAM_SWITCH then
TProgressBar(lpData).Max := TotalFileSize.QuadPart; TProgressBar(lpData).Position := TotalBytesTransferred.QuadPart;
Application.ProcessMessages;
Result := PROGRESS_CONTINUE;
end; function TForm1.CopyWithProgress(sSource, sDest: string): Boolean;
begin
// set this FCancelled to true, if you want to cancel the copy operation
FCancelled := False;
Result := CopyFileEx(PChar(sSource), PChar(sDest), @CopyFileWithProgressBar2,
ProgressBar1, @FCancelled, 0);
end; end; {***************************************}
//转自http://delphi.ktop.com.tw/topic.asp?TOPIC_ID=56566
Form1: TForm1;
CancelCopy: Boolean; implementation {$R *.dfm} function CopyFileProgress(TotalFileSize, TotalBytesTransferred, StreamSize,
StreamBytesTransferred: LARGE_INTEGER; dwStreamNumber, dwCallbackReason,
hSourceFile, hDestinationFile, lpData: DWORD): DWORD; stdcall;
begin
Application.ProcessMessages;
if CancelCopy = True then
begin
ShowMessage('Abbruch');
result := PROGRESS_CANCEL;
Exit;
end;
case dwCallbackReason of
CALLBACK_CHUNK_FINISHED:
begin
Form1.ProgressBar1.Position := TotalBytesTransferred.QuadPart;
result := PROGRESS_CONTINUE;
end;
CALLBACK_STREAM_SWITCH:
begin
Form1.ProgressBar1.Max := TotalFileSize.QuadPart;
result := PROGRESS_CONTINUE;
end;
end;
end; procedure TForm1.Button1Click(Sender: TObject);
var
Cancel: PBOOL;
begin
CancelCopy := False;
Cancel := nil;
CopyFileEx('g:\Brennen\Madonna - Erotica.mpg', 'g:\Madonna - Erotica.mpg',
@CopyFileProgress, nil, Cancel, 0);
end; procedure TForm1.Button2Click(Sender: TObject);
begin
CancelCopy := True;;
end;
//转自http://www.delphipraxis.net/topic26076,next.html