【求助】VB课设:编写一个文件复制程序,并用进度条显示复制进度,要求程序具有打开文件,执行复制,保... 40
Option Explicit
Dim strFileName1 As String
Dim strFileName2 As String
Dim intFile1 As Integer
Dim intFile2 As Integer
Private Declare Function GetTickCount Lib "kernel32" () As Long
'复制文件
Private Sub bnCopy_Click()
ProgressBar1.Visible = False
If strFileName1 = "" Or Dir(strFileName1) = "" Then
MsgBox "原文件不存在!", vbOKOnly, "CopyFile"
Exit Sub
End If
If strFileName2 = "" Or Dir(strFileName2) = "" Then
lblFile2.Caption = strFileName2
Else
MsgBox "复制为文件已经存在!", vbOKOnly, "CopyFile"
Exit Sub
End If
If strFileName1 = strFileName2 Then
MsgBox "2个文件名不能相同!", vbOKOnly, "CopyFile"
Exit Sub
End If
ProgressBar1.Visible = True
intFile1 = FreeFile
Open strFileName1 For Binary As #intFile1
intFile2 = FreeFile
Open strFileName2 For Binary As #intFile2
Dim i As Long
Dim tmp As Byte
ProgressBar1.Min = 0
ProgressBar1.Max = LOF(intFile1)
ProgressBar1.Value = 0
For i = 1 To LOF(intFile1)
Get #intFile1, , tmp
Put #intFile2, , tmp
ProgressBar1.Value = i
'以下为延时,减缓运行查看进度条效果,如果大文件复制慢则可以删除延时
Dim k As Long
k = GetTickCount()
While GetTickCount() < k + 10
DoEvents
Wend
Next i
Close #intFile1
Close #intFile1
End Sub
'退出
Private Sub bnExit_Click()
Unload Me
End Sub
'指定原文件
Private Sub bnFile1_Click()
ProgressBar1.Visible = False
CommonDialog1.FileName = ""
CommonDialog1.ShowOpen
strFileName1 = CommonDialog1.FileName
If strFileName1 = "" Or Dir(strFileName1) = "" Then
MsgBox "原文件不存在!", vbOKOnly, "CopyFile"
Exit Sub
Else
lblFile1.Caption = strFileName1
End If
End Sub
'指定复制为的文件
Private Sub bnFile2_Click()
ProgressBar1.Visible = False
CommonDialog1.FileName = ""
CommonDialog1.ShowSave
strFileName2 = CommonDialog1.FileName
If strFileName2 = "" Or Dir(strFileName2) = "" Then
lblFile2.Caption = strFileName2
Else
MsgBox "复制为文件已经存在!", vbOKOnly, "CopyFile"
Exit Sub
End If
End Sub
'初始化
Private Sub Form_Load()
ProgressBar1.Visible = False
CommonDialog1.Filter = "*.*"
End Sub