进度条编程问题(88分)

M

messah

Unregistered / Unconfirmed
GUEST, unregistred user!
程序当中,使用下列代码进行目录的拷贝过程。但是如何设置个进度条来显示拷贝文件的进度呢?谢谢
fillchar(frombuf,sizeof(frombuf),0);
fillchar(tobuf,sizeof(tobuf),0);
strpcopy(frombuf,pchar(sourdir));
strpcopy(tobuf,pchar(todir));
with opstrucdo
begin
wnd:=handle;
wfunc:=FO_COPY;
pFrom:=@FromBuf;
pTo:=@Tobuf;
fFlags:= FOF_FILESONLY or FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR or FOF_NOCONFIRMMKDIR or FOF_SILENT;
fAnyOperationsAborted:=False;
hNameMappings:=nil;
lpszProgressTitle:=nil;
end;
if SHFileOperation(OpStruc)=0 then
Application.terminate;
 
fFlags:= FOF_FILESONLY or FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR or FOF_NOCONFIRMMKDIR;
 
LS的。我就是不要出现系统自带的那种拷贝文件的图画。而是要单独自己搞个进度条。
 
SHFileOperation没有复制进度的返回值,你想通过他来自己写进度条就别想了
如果你一定要自己显示进度条的话,那可以考虑copyfileex不过这个只能copy单个文件
或者你自己用BlockRead、BlockWrite配合FindFirst、FindNext、CreateDirectory来自己写个复制方法
 
hs-kill,能麻烦指导下具体代码如何写吗?
 
恩....手边没现成代码,只有个多文件复制的
思路大概是这样:
先用类似下面的代码取得你要复制的目录的全部结构:
if FindFirst(tmpPath+'*.*',faAnyFile,SRec)=0 then
repeat
If (srec.Attr and FILE_ATTRIBUTE_DIRECTORY)<>0 then
{这个是目录}
//记下目录 tmpPath+srec.Name 并且用CreateDirectory创建目录
else
//记下文件 tmpPath+srec.Name 并且取得文件字节大小
until FindNext( SRec )<>0;
存起来
然后再用下面的代码复制文件并显示进度
进度条的总长度应该为全部文件的大小或者数量,具体用哪个你自己选
下面的例子是文件数量的
var
SrcFile, DestFile: File;
BytesRead, BytesWritten, TotalRead: Integer;
Buffer: array[1..500] of byte;
FSize,i,intfiles,x: Integer;
fname:string;
begin
if cb_copy.Checked then
begin
if edt_topath.Text='' then
begin
application.MessageBox('先选择目标路径!','提示',mb_ok);
exit;
end;
fname:='';
intfiles:=0;
if CB_all.Checked then
intfiles:=lv_file.Items.Count
else
for i:=0 to lv_file.Items.Count-1do
if lv_file.Items.Selected then
intfiles:=intfiles+1;
ProgressBar2.Max:=intfiles;
{这里,进度条总长度为文件的总数量}
ProgressBar2.Step:=1;
x:=strtoint(edit3.Text);
for i:=0 to lv_file.Items.Count-1do
begin
if not CB_all.Checked then
if not lv_file.Items.Selected then
continue;
AssignFile(SrcFile,edt_frompath.Text+'/'+lv_file.Items.Caption);
fname:=inttostr(i+x)+copy(lv_file.Items.Caption,pos('.',lv_file.Items.Caption),length(lv_file.Items.Caption));
StatusBar1.Panels.Items[1].Text:=lv_file.Items.Caption+'---->'+fname;
AssignFile(DestFile, edt_topath.Text+'/'+fname);
Reset(SrcFile, 1);
try
Rewrite(DestFile, 1);
try
try
TotalRead := 0;
FSize := FileSize(SrcFile);
ProgressBar1.min:=0;
ProgressBar1.max:=trunc(fsize/500);
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;
ProgressBar1.Position :=ProgressBar1.position+1;
application.ProcessMessages;
end;
end
until BytesRead = 0;
except
Erase(DestFile);
raise;
end;
finally
CloseFile(DestFile);
// Close the destination file.
end;
finally
CloseFile(SrcFile);
// Close the source file.
end;
lv_file.Items.SubItems.Add('完成');
ProgressBar2.StepIt;
ProgressBar2.Update;
end;
application.MessageBox('复制完成!!','提示',mb_ok);
StatusBar1.Panels.Items[1].Text:='';
end;
end;
 
别人都告诉你用哪几个函数了。你还要代码。支付酬金不。****。
 
LS的。关你鸟事。死远点去。
 
你帖子碍眼 骂骂你这龌龊的发帖者。发帖者继续。
 
版主啊。我帖子里有条疯狗,来管理下呢。
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
512
import
I
I
回复
0
查看
539
import
I
I
回复
0
查看
752
import
I
顶部