真皮沙发布套:快速全盘文件查找的VB源代码如下,编译用户定义类型未通过,不知道如何修改,请赐教.谢谢

来源:百度文库 编辑:高考问答 时间:2024/05/04 20:05:49
Option Explicit '声明函数
Dim lhwnd As String
Dim dirs, Dir$, files As Integer
Dim isrun As Boolean
Dim WFD As WIN32_FIND_DATA, hItem&, hFile& (错误提示在此)
Private Sub Form_Load()
lhwnd = List1.hWnd
SendMessage lhwnd, LB_INITSTORAGE, 30000&, ByVal 30000& * 200
End Sub
Private Sub Form_Activate() '设定默认路径
Dir1.Path = App.Path
Drive1.Drive = Left(Dir1.Path, 3)
End Sub
Private Sub Dir1_Change() '选择文件夹
Text1.Text = Dir1.Path & "\"
End Sub
Private Sub Drive1_Change() '选择驱动器
Dir1.Path = Drive1.Drive
End Sub
Private Sub SearchDirs(filepath$)
Dim dircount, i As Integer
Dim dirarray()
DoEvents
If Not isrun Then Exit Sub
hItem& = FindFirstFile(filepath$ & "*.*", WFD) '查找文件
If hItem& <> INVALID_HANDLE_VALUE Then
Do
If (WFD.dwFileAttributes And vbDirectory) Then
If Asc(WFD.cFileName) <> 46 Then
dirs = dirs + 1
If (dircount Mod 10) = 0 Then ReDim Preserve dirarray(dircount + 10)
dircount = dircount + 1
dirarray(dircount) = Left$(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
End If
Else
files = files + 1
End If
Loop While FindNextFile(hItem&, WFD)
Call FindClose(hItem&) '关闭FindFirstFile
End If
SendMessage lhwnd, WM_SETREDRAW, 0, 0
hFile& = FindFirstFile(filepath$ & Dir$, WFD)
If hFile& <> INVALID_HANDLE_VALUE Then
Do
DoEvents
If Not isrun Then Exit Sub
SendMessage lhwnd, LB_ADDSTRING, 0, _
ByVal filepath$ & Left$(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
Label3.Caption = "文件个数: " & List1.ListCount & " 个"
Loop While FindNextFile(hFile&, WFD)
Call FindClose(hFile&)
End If
SendMessage lhwnd, WM_VSCROLL, SB_BOTTOM, 0
SendMessage lhwnd, WM_SETREDRAW, 1, 0
For i = 1 To dircount: SearchDirs filepath$ & dirarray(i) & "\": Next i
End Sub
Private Sub Text1_Change() '
If Len(Text1.Text) = 4 Then Text1.Text = Left(Text1.Text, 3) '去掉路径中的\
End Sub
Private Sub Command1_Click() '查找文件
On Error Resume Next
If isrun Then: isrun = False: Exit Sub
Dir$ = Text2.Text
MousePointer = 11
isrun = True
List1.Clear '清空列表
If isrun Then Call SearchDirs(Text1.Text) '调用函数查找文件
Label3.Caption = "文件个数: " & List1.ListCount & " 个"
isrun = False
MousePointer = 0
End Sub
Private Sub Command2_Click() '停止查找
isrun = False
MousePointer = 0
End Sub
Private Sub Command3_Click()
End
End Sub
最好能把重要语句注释一下,谢谢

正确定义应该是这样
'先定义 WIN32_FIND_DATA 类型

Public Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type

'定义 WFD 变量为 WIN32_FIND_DATA 类型
Dim WFD As WIN32_FIND_DATA