在数据传送中如何显示进度条呢
解决方案 »
- fastreport打印问题,总是重复出现一条数据
- 在Delphi中调用VC的DLL的问题,该dll使用MFC类库,并返回一个对象
- 怎么响应回车事件,(急)
- 保存控件信息写入文件中的问题
- 软件启动问题!
- 如何在其它事件中调ListrView的ListView1CustomDrawItem事件!!!!
- 怎样实现当我把光标(用鼠标)移动到一个EDIT上时EDIT的背景改变?
- 寻找Delphi高手
- 告诉我几个关于delphi的好一点的网站?就是你们放在收藏夹里的。
- 有那位用Delphi做过Web Service,它能支持XML和SOAP标准吗?
- 请高手帮忙?
- 请各位谈谈Prepare和UnPrepare在Query中的作用和用法
var
Ftrans: file of Byte;
Flen: Integer;
BlockNum, RemainLen: Integer;
BlockBuf: array[0..BlockLen - 1] of Byte;
i: Integer;
SendLen: Integer;
begin
AssignFile(Ftrans, filename);
Reset(Ftrans);
Flen := FileSize(Ftrans);
BlockNum := Flen div BlockLen;
ProgressBar.Max := 1 + BlockNum; //进度条
RemainLen := Flen mod BlockLen;
StopTrans := False;
InTrans := True;
SendLen := 1;
for i := 0 to BlockNum - 1 do
begin
if (StopTrans) or (SendLen <= 0) then Break;
BlockRead(Ftrans, BlockBuf[0], BlockLen);
SendLen := send(Client, BlockBuf, BlockLen, 0);
ProgressBar.Position := i;
Application.ProcessMessages;
end;
if StopTrans then
begin
CloseFile(Ftrans);
InTrans := False;
StatusBar.SimpleText := '';
MessageBox(Handle, '停止传输!', '提示', MB_OK);
ProgressBar.Position := 0;
Exit;
end;
if (SendLen <= 0) then
begin
CloseFile(Ftrans);
InTrans := False;
StatusBar.SimpleText := '';
MessageBox(Handle, '传输异常终止!', '提示', MB_OK);
ProgressBar.Position := 0;
Exit;
end;
if RemainLen > 0 then
begin
BlockRead(Ftrans, BlockBuf[0], RemainLen);
SendLen := send(Client, BlockBuf, RemainLen, 0);
if (SendLen <= 0) then
begin
CloseFile(Ftrans);
InTrans := False;
StatusBar.SimpleText := '';
MessageBox(Handle, '传输异常终止!', '提示', MB_OK);
ProgressBar.Position := 0;
Exit;
end;
end;
ProgressBar.Position := ProgressBar.Max;
CloseFile(Ftrans);
InTrans := False;
StatusBar.SimpleText := '';
MessageBox(Handle, '传输完成!', '提示', MB_OK);
ProgressBar.Position := 0;
end;
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, ComCtrls, Gauges;type
TMainForm = class(TForm)
prbCopy: TProgressBar;
btnCopy: TButton;
procedure btnCopyClick(Sender: TObject);
end;var
MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.btnCopyClick(Sender: TObject);
var
SrcFile, DestFile: File;
BytesRead, BytesWritten, TotalRead: Integer;
Buffer: array[1..500] of byte;
FSize: Integer;
begin
{ Assign both the source and destination files to their
respective file variables }
AssignFile(SrcFile, 'srcfile.tst');
AssignFile(DestFile, 'destfile.tst');
// Open the source file for read access.
Reset(SrcFile, 1);
try
// Open destination file for write access.
Rewrite(DestFile, 1);
try
{ Encapsulate this into a try..except so that we can erase the file if
an error occurs. }
try
// Initialize total bytes read to zero.
TotalRead := 0;
// Obtain the filesize of the source file
FSize := FileSize(SrcFile);
{ Read SizeOf(Buffer) bytes from the source file
and add these bytes to the destination file. Repeat this
process until all bytes have been read from the source
file. A progress bar is provided to show the progress of the
copy operation. }
repeat
BlockRead(SrcFile, Buffer, SizeOf(Buffer), BytesRead);
if BytesRead > 0 then
begin
BlockWrite(DestFile, Buffer, BytesRead, BytesWritten);
if BytesRead <> BytesWritten then
raise Exception.Create('Error copying file')
else begin
TotalRead := TotalRead + BytesRead;
prbCopy.Position := Trunc(TotalRead / Fsize) * 100;
prbCopy.Update;
end;
end
until BytesRead = 0;
except
{ On an exception, erase the destination file as it may be
corrupt. Then re-raise the exception. }
Erase(DestFile);
raise;
end;
finally
CloseFile(DestFile); // Close the destination file.
end;
finally
CloseFile(SrcFile); // Close the source file.
end;
end;end.