实例说明
在本实例中,我们将制作一个文件查询系统的应用程序。运行程序时,选择要查询的目录并输入文件名后,就可以开始查找了。运行结果可以以不同的形式显示。程序运行结果如图69-1所示。
图69-1 运行结果
技术要点
l ListView控件的显示方式
l 取得文件信息
l 文件查询API函数FindFirstFile()和FindNextFile()
l 打开文件目录对话框函数SHBrowseForFolder()
实现过程
■ 新建项目
打开Visual Studio.NET,选择"新建项目",在项目类型窗口中选择"Visual Basic项目",在模板窗口中选择"Windows应用程序",在名称域中输入"FindFile",然后选择保存路径。单击"确认"。
■ 添加控件
添加三个Button按钮,用于执行相应的操作;相对应的两个Label控件和TextBox控件,分别用于说明和输入文件名和目录;一个Group控件和四个RadioButton控件,用于显示不同的显示方式;一个ListView控件,用于显示查询的结果。
■ 设置属性
将Option控件做成控件数组,命名为optview,将其他控件的Text属性改为和界面一致。其余属性保持默认值。
由于版面有限,在此仅列出主要的函数及其声明,主要的功能都已经列出,其余代码见光盘。
Public Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME,lpSystemTime As SYSTEMTIME) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String,lpFindFileData As WIN32_FIND_DATA) As Long
Public Const SHGFI_SYSICONINDEX = &H4000 'system icon index
Public shinfo As SHFILEINFO
Public Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End Type
'主窗体函数
Private Function BrowDir() As Object
Dim bi As BROWSEINFO
Dim pidl As Long
Dim rtn As String
Dim path As String
Dim pos As Short
Dim mypath As String
' 打开目录对话框选择文件夹
pidl = SHBrowseForFolder(bi)
path = Space(512)
' 转化为文件系统路径
rtn = CStr(SHGetPathFromIDList(pidl,path))
If CBool(rtn) Then
pos = InStr(path,Chr(0))
BrowDir = VB.Left(path,pos - 1)
End If
End Function
' 选择路径
Private Sub Button3_Click(ByVal eventSender As System.Object,ByVal eventArgs As System.EventArgs) Handles Button3.Click
Text2.Text = BrowDir()
End Sub
Sub FindFile(ByRef dirpath As String,ByRef filespec As String)
Dim finddata As WIN32_FIND_DATA
Dim findnexthandle As Integer
Dim filestring As String
Dim dirfile As String
Dim n As Short
Dim fileinfo As Integer
dirpath = Trim(dirpath)
' 判断路径的有效性
If VB.Right(dirpath,1) = "/" Then
dirfile = dirpath & filespec
Else
dirfile = dirpath & "/" & filespec
End If
' 每次查询前,清空ListView
ListView1.ListItems.Clear()
findhandle = FindFirstFile(dirfile,finddata)
If findhandle <> 0 Then
filestring = Trim(finddata.cFileName)
n = n + 1
' 返回文件的相关信息,
fileinfo = SHGetFileInfo(dirfile,shinfo,Len(shinfo),SHGFI_TYPENAME)
' 将取得文件信息加入到ListView控件中
With ListView1.ListItems.Add(,filestring)
.SubItems(1) = vbGetFileSizeKBStr(finddata.nFileSizeHigh + finddata.nFileSizeLow)
.SubItems(2) = vbGetFileDate(finddata.ftCreationTime)
' .SubItems(3) = shinfo.szTypeName
End With
End If
If findhandle <> 0 Then
Do
System.Windows.Forms.Application.DoEvents()
findnexthandle = FindNextFile(findhandle,finddata)
If VB.Right(dirpath,1) = "/" Then
dirfile = dirpath & finddata.cFileName
Else
dirfile = dirpath & "/" & finddata.cFileName
End If
fileinfo = SHGetFileInfo(dirfile,BASIC_SHGFI_FLAGS)
If findnexthandle <> 0 Then
filestring = Trim(finddata.cFileName)
With ListView1.ListItems.Add(,filestring)
.SubItems(1) = vbGetFileSizeKBStr(finddata.nFileSizeHigh + finddata.nFileSizeLow)
.SubItems(2) = vbGetFileDate(finddata.ftCreationTime)
' .SubItems(3) = shinfo.szTypeName
End With
Else
Exit Do
End If
Loop
End If
Call FindClose(findhandle)
End Sub
' 程序启动,默认为C:
Private Sub Form1_Load(ByVal eventSender As System.Object,ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Text1.Text = "*.*"
Text2.Text = "C:/"
optarrange(3).Checked = True
ListView1.SortKey = 0
End Sub
Private Function vbGetFileSizeKBStr(ByRef fsize As Integer) As String
vbGetFileSizeKBStr = VB6.Format(((fsize) / 1000) + 0.5,"#,###,###") & "KB"
End Function
Private Function vbGetFileDate(ByRef CT As FILETIME) As String
Dim ST As SYSTEMTIME
Dim r As Integer
Dim ds As Single
r = FileTimeToSystemTime(CT,ST)
If r Then
ds = DateSerial(ST.wYear,ST.wMonth,ST.wDay).ToOADate
vbGetFileDate = VB6.Format(ds,"long Date")
Else : vbGetFileDate = ""
End If
End Function
Private Sub optarrange_CheckedChanged(ByVal eventSender As System.Object,ByVal eventArgs As System.EventArgs) Handles optarrange.CheckedChanged
If eventSender.Checked Then
Dim Index As Short = optarrange.GetIndex(eventSender)
Select Case Index
Case 0
ListView1.View = MSComctlLib.ListViewConstants.lvwIcon
Case 1
ListView1.View = MSComctlLib.ListViewConstants.lvwSmallIcon
Case 2
ListView1.View = MSComctlLib.ListViewConstants.lvwList
Case 3
ListView1.View = MSComctlLib.ListViewConstants.lvwReport
End Select
End If
End Sub
■ 运行程序
单击菜单"调试|启动"或单击 图标运行程序。
小结
通过本实例的学习,我们掌握了几个很有用的API函数,具体有下面几个:SHGetPathFromIDList,SHBrowseForFolder,FileTimeToSystemTime,FindClose,FindFirstFile,FindNextFile,SHGetFileInfo。至于这些函数的详细用法,读者可以自行查看有关API函数的书籍。
原文链接:https://www.f2er.com/vb/264320.html