提示:
@ 在程序运行的过程中,用户还可以通过选择下拉式列表框中的内容来决定在文件控件中显示的类型,如所有文件、以*.txt为文件后缀的文本文件、以*.bmp为文件后缀的图像文件等。
附程序完整源代码如下所示:
程序清单
VERSION 6.00
Begin Visual Basic.Form Form1
BorderStyle = 3
Caption = "Form1"
MaxButton = 0
MinButton = 0
Moveable = 0
ScaleHeight = 4590
ScaleWidth = 6885
StartUpPosition = 2
End
Attribute Visual Basic_Name = "Form1"
Attribute Visual Basic_GlobalNameSpace = False
Attribute Visual Basic_Creatable = False
Attribute Visual Basic_PredeclaredId = True
Attribute Visual Basic_Exposed = False
Private Sub Combo1_Click()
Form1.File1.Pattern = Form1.Combo1.Text
End Sub
Private Sub Dir1_Change()
Form1.File1.Path = Form1.Dir1.Path
End Sub
Private Sub Dir1_Click()
Fname = Form1.Dir1.Path
Result = GetAttr(Fname) And vbDirectory
If Result <> 0 Then
Form1.Text2.Text = "目录"
End If
Form1.Text1.Text = ""
Form1.Text3.Text = ""
End Sub
Private Sub Dir1_DragDrop(Source As Control, X As Single, Y As Single)
If Mid(File1.Path, Len(File1.Path)) = "\" Then
Fname = File1.Path & File1.FileName
'如果被单击的文件在根目录,就追加文件名
Else
Fname = File1.Path & "\" & File1.FileName
'如果被单击的文件不在根目录,就追加 "\" 和文件名
End If
Kill Fname
Form1.File1.Refresh
End Sub
Private Sub Dir1_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
Select Case State
Case 0
' 当"源"进入"放"区域内时,显示一个新图标。
File1.DragIcon = Dir1.DragIcon
Case 1
' 当"源"离开"放"区域时,显示原来的 DragIcon 图标。
File1.DragIcon = Drive1.DragIcon
End Select
End Sub
Private Sub Drive1_Change()
On Error GoTo DriveErrs
'错误处理
Dir1.Path = Drive1.Drive
'设置目录显示路径
Exit Sub
DriveErrs:
'错误处理
Select Case Err
Case 68
MsgBox prompt:="驱动器未准备好。请在驱动器内插入磁盘。", _
buttons:=vbExclamation
' 将路径重置为先前使用的驱动器。
Drive1.Drive = Dir1.Path
Exit Sub
Case Else
MsgBox prompt:="应用程序错误。", buttons:=vbExclamation
End Select
End Sub
Private Sub File1_Click()
Form1.Text2.Text = ""
If Mid(File1.Path, Len(File1.Path)) = "\" Then
Fname = File1.Path & File1.FileName
'如果被单击的文件在根目录,就追加文件名。
Else
Fname = File1.Path & "\" & File1.FileName
'如果被单击的文件不在根目录,就追加 "\" 和文件名。
End If
Form1.Text1.Text = FileLen(Fname) & "Byte"
If (GetAttr(Fname) And 32) <> 0 Then
Form1.Text2.Text = Form1.Text2.Text & " 存档"
End If
If (GetAttr(Fname) And 2) <> 0 Then
Form1.Text2.Text = Form1.Text2.Text & " 隐藏"
End If
If (GetAttr(Fname) And 1) <> 0 Then
Form1.Text2.Text = Form1.Text2.Text & " 只读"
End If
If (GetAttr(Fname) And 4) <> 0 Then
Form1.Text2.Text = Form1.Text2.Text & " 系统"
End If
Form1.Text3.Text = FileDateTime(Fname)
End Sub
Private Sub File1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
File1.DragIcon = Drive1.DragIcon
File1.Drag
End Sub
文件是计算机的基本概念,也是计算机进行运算和存储的基础,在Visual Basic 6.0中处理文件可以通过以下几个途径,如通过文件处理控件、调用系统函数和利用Visual Basic 6.0自定义文件函数等方式。
在本章中将会通过几个示例程序向读者介绍有关Visual Basic 中处理文件的基本技术和常用技巧,如利用文件类控件处理文件、制作文件查询程序和利用文件函数处理文件等。