copyfile的时候,程序会卡住,没有机会显示进度条。如果非要用copyfile,需要创建新线程监视,这是高技术。实际可行的方法是自己编写copyfile,用基本文件操作来一块块复制,这样才能显示。
自己写替代函数比较容易
Function CopyFile(Src As String, Dst As String) As Single
Dim BTest!, FSize!
Dim F1%, F2%
Dim sArray() As Byte
Dim buff As Integer
Const BUFSIZE = 1024
buff = 1024
F1 = FreeFile
Open Src For Binary As F1
F2 = FreeFile
Open Dst For Binary As F2
FSize = LOF(F1)
BTest = FSize - LOF(F2)
ReDim sArray(BUFSIZE) As Byte
Do
If BTest < BUFSIZE Then
buff = BTest
ReDim sArray(buff) As Byte
End If
Get F1, , sArray
Put F2, , sArray
BTest = FSize - LOF(F2)
If BTest < 0 Then
ProgressBar.Value = 100 '这里是进度条代码
Else
ProgressBar.Value = (100 - Int(100 * BTest / FSize)) '这里是进度条代码
End If
Loop Until BTest <= 0
Close F1
Close F2
CopyFile = FSize
End Function
Private Sub Command1_Click()
CopyFile "z:\\win.rar", "z:\\1\\win.rar"'调用: CopyFile "源", "目标"
End Sub