VB的FileCopy函数无法知道拷贝了多少字节,进度条的Value无法知道。 API函数SHFileOperation,这个函数可以实现拷贝功能,同时还可以显示带有进度条的标准动画对话框。该函数的使用可以参考微软的Knowledge Base的文章:“Q151799 OWTO: Use the Animated Copy Functions in Windows 95/98”。 但是它要打开自己的窗口,如果希望的是把进度条嵌入到自己的窗口中,可以这么写。自己填加processbar Public 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
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
VB的FileCopy函数无法知道拷贝了多少字节,进度条的Value无法知道。 API函数SHFileOperation,这个函数可以实现拷贝功能,同时还可以显示带有进度条的标准动画对话框。该函数的使用可以参考微软的Knowledge Base的文章:“Q151799 OWTO: Use the Animated Copy Functions in Windows 95/98”。(网址:http://support.microsoft.com/kb/q151799/) 但是它要打开自己的窗口,如果希望的是把进度条嵌入到自己的窗口中,可以这么写。自己填加processbar Public 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
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
1、添加引用microsoft scripting runtime 方法:选择菜单“工程”-->“引用”-->选择“microsoft scripting runtime”2、使用方法 Option ExplicitPrivate Sub Form_Load() Dim FileSys As New FileSystemObject Dim FolderObj As Folder Set FileSys = CreateObject("scripting.filesystemobject") FileSys.CopyFile "c:\ss.txt", "d:\mm.txt", True '拷贝文件 FileSys.CopyFolder "c:\1", "d:\2", True '拷贝文件夹 End Sub
API函数SHFileOperation,这个函数可以实现拷贝功能,同时还可以显示带有进度条的标准动画对话框。该函数的使用可以参考微软的Knowledge Base的文章:“Q151799 OWTO: Use the Animated Copy Functions in Windows 95/98”。 但是它要打开自己的窗口,如果希望的是把进度条嵌入到自己的窗口中,可以这么写。自己填加processbar Public 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
API函数SHFileOperation,这个函数可以实现拷贝功能,同时还可以显示带有进度条的标准动画对话框。该函数的使用可以参考微软的Knowledge Base的文章:“Q151799 OWTO: Use the Animated Copy Functions in Windows 95/98”。(网址:http://support.microsoft.com/kb/q151799/) 但是它要打开自己的窗口,如果希望的是把进度条嵌入到自己的窗口中,可以这么写。自己填加processbar Public 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
方法:选择菜单“工程”-->“引用”-->选择“microsoft scripting runtime”2、使用方法
Option ExplicitPrivate Sub Form_Load()
Dim FileSys As New FileSystemObject
Dim FolderObj As Folder
Set FileSys = CreateObject("scripting.filesystemobject")
FileSys.CopyFile "c:\ss.txt", "d:\mm.txt", True '拷贝文件
FileSys.CopyFolder "c:\1", "d:\2", True '拷贝文件夹
End Sub
自己做一个进度条就是了,既好看又适用。