拷贝进程的实现 (200分)

  • 主题发起人 主题发起人 asw
  • 开始时间 开始时间
A

asw

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp; 我们在用DELPHI5.0编程中,想实现象WINDOWS中拷贝文件那样,怎样使进度条的滚动与<br>文件的拷贝进程相一致,谢谢大家!!!
 
用SHFileOperation 这个API<br>
 
用API的话,用cheka,如果自己用流或是其它的方法自己来写代码的话,就简单了,只需要<br>获得源文件的大小,在每次写入一部分内容的时间设置进度条的Position值。
 
没人给出SHFileOperation的用法代码吗?……流口水……嘿嘿……<br>算了,先不做这个投机生意。<br>To 教父:如果要拷的是一串文件或一个目录下的所有文件呢?如何像SHFileOperation那<br>样显示进度条?
 
SHFileOperation !<br><br>如果有人参与讨论这个问题,大富翁会用EMail通知我
 
我看了一下帮助,试着写了如下代码,测试通过,不过有些参数还没弄明白,慢慢来吧 :)<br>记得uses ShellApi!<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; FileOp:SHFileOpStruct;<br>&nbsp; l:Boolean;<br>begin<br>FileOp.Wnd:=Handle;<br>FileOp.wFunc:=FO_COPY;<br>FileOp.pFrom:='c:/download/test.dat';<br>FileOp.pTo:='d:/temp/test/';<br>FileOp.fAnyOperationsAborted:=l;<br>SHFileOperation(FileOp);<br>end;<br>
 
我看了一下帮助,试着写了如下代码,测试通过,不过有些参数还没弄明白,慢慢来吧 :)<br>记得uses ShellApi!<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; FileOp:SHFileOpStruct;<br>&nbsp; l:Boolean;<br>begin<br>FileOp.Wnd:=Handle;<br>FileOp.wFunc:=FO_COPY;<br>FileOp.pFrom:='c:/download/test.dat';<br>FileOp.pTo:='d:/temp/test/';<br>FileOp.fAnyOperationsAborted:=l;<br>SHFileOperation(FileOp);<br>end;<br>
 
忘了说一句,这段代码执行时,如果拷贝的文件太小就不会出现拷贝的进度条,因为太快了,<br>要想看到的话,你要找个大文件试试 :)
 
分成两个线程,一个用于拷贝,一个用于使进度条前进
 
设置两个变量,一个用来计算已经拷贝的文件大小n,用n与文件总量求比,百分比,然后<br>用另一个变量m,来显示进度条,即可
 
用这个不可以吗?<br>procedure TForm1.FileCopy(const FromFile, ToFile: string);<br>var<br>&nbsp; &nbsp;fsize:Longint;<br>&nbsp; &nbsp;FromF, ToF: file;<br>&nbsp; &nbsp;NumRead, NumWritten: integer;<br>&nbsp; &nbsp;Buf: array[1..2048] of Char;<br>begin<br>&nbsp; &nbsp;AssignFile(FromF, FromFile);<br>&nbsp; &nbsp;Reset(FromF, 1);<br>&nbsp; &nbsp;fsize := filesize(fromf);<br>&nbsp; &nbsp;ProgressBar1.Max:=fsize;<br>&nbsp; &nbsp;AssignFile(ToF, ToFile);<br>&nbsp; &nbsp;Rewrite(ToF, 1);<br>&nbsp; &nbsp;repeat<br>&nbsp; &nbsp; &nbsp; BlockRead(FromF, Buf, SizeOf(Buf), NumRead);<br>&nbsp; &nbsp; &nbsp; ProgressBar1.Position:=ProgressBar1.Position+sizeof(buf);<br>&nbsp; &nbsp; &nbsp; application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; BlockWrite(ToF, Buf, NumRead, NumWritten);<br>&nbsp; &nbsp;until (NumRead = 0) or (NumWritten &lt;&gt; NumRead);<br>&nbsp; &nbsp;System.CloseFile(FromF);<br>&nbsp; &nbsp;System.CloseFile(ToF);<br>&nbsp; &nbsp;ShowMessage('OK');<br>end;<br>
 
function CopyProgress(FileSize, Transferred: Int64;<br>&nbsp; StreamSize, StreamTransferred: Int64;<br>&nbsp; StreamNumber, CallbackReason: DWORD;<br>&nbsp; SourceFile, DestFile: THandle; Data: Pointer): DWORD; stdcall;<br>var<br>&nbsp; Form: TForm1;<br>begin<br>&nbsp; Form := Data;<br>&nbsp; Form.ProgressBar1.Max := FileSize;<br>&nbsp; Form.ProgressBar1.Position := Transferred;<br>&nbsp; Result := PROGRESS_CONTINUE;<br>&nbsp; Application.ProcessMessages;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; CopyFileEx('ExistsFile.ext', 'newFile', @CopyProgress,<br>&nbsp; &nbsp; Self, nil, COPY_FILE_FAIL_IF_EXISTS);<br>end;
 
后退
顶部