VB6.0 treeview显示文件夹及文件

VB6.0 treeview显示文件夹及文件,第1张

桌面上放置一个treeview,名称Tv1,代码如下悄游首:

Public Function GetExtName(strFileName As String) As String

Dim strTmp As String

Dim strByte As String

Dim i As Long

For i = Len(strFileName) To 1 Step -1

strByte = Mid(strFileName, i, 1)

If strByte <>"." Then

strTmp = strByte + strTmp

Else

Exit For

End If

Next i

GetExtName = strTmp

End Function

Public Function search(ByVal strPath As String, Optional strSearch As String) As Boolean

Dim strFile As String

Dim i As Long

Dim lDirCount As Long

On Error GoTo MyErr

If Right(strPath, 1) <>"\" Then strPath = strPath + "\"

Tv1.Nodes.Add , , strPath, strPath, ImageCollapse, ImageExpand

strFile = Dir(strPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)

While strFile <>"启数"

DoEvents

If (GetAttr(strPath + strFile) And vbDirectory) <>vbDirectory Then

If LCase(GetExtName(strPath + strFile)) = LCase(GetExtName(strSearch)) Then

Tv1.Nodes.Add strPath, tvwChild, strPath + strFile, strFile, ImageCollapse, ImageExpand

End If

End If

strFile = Dir

Wend

search = True

Exit Function

MyErr:

search = False

End Function

Private Sub Form_Load()

Tv1.Style = 7

search "磨笑D:\Documents\新建文件1", ".xls"

search "D:\Documents\新建文件夹2", ".xls"

End Sub

'****************************************************

'*组名称:SearchFiles

'*组说明:歼册遍历某个目录中指定文件。

'*语法说明:SearchFiles(Path , FileType, ListObj, ImageIndex)

'*语法说明:[Path]:要遍历的路径;[FileType]:文件类型;[ListObj]:指定树形列表控件

'*语法说明:[ImageIndex]:指定树形列表控件图像Index

'*返回值:

'*注意事项:使悄坦用树形控件前必须将其初始化。

'*组作者:gaochongjun1

'****************************************************

Function SearchFiles(Path As String, FileType As String, ListObj, ImageIndex As Integer)

Dim Files() As String '文件路径

Dim Folder() As String '文件夹路径

Dim AB, BA, c As Long

Dim sPath As String

sPath = Dir(Path &FileType) '查找第一个文件

Do While Len(sPath) '循环氏运宏到没有文件为止

AB = AB + 1

ReDim Preserve Files(1 To AB)

Files(AB) = sPath '将文件目录和文件名组合,并存放到数组中

'加入树形列表中

With ListObj

With .Nodes

.Add "a0", 4, Files(AB), Files(AB), "Menu"

End With

End With

sPath = Dir '查找下一个文件

DoEvents '让出控制权

Loop

sPath = Dir(Path &"\", vbDirectory) '查找第一个文件夹

Do While Len(sPath) '循环到没有文件夹为止

If Left(sPath, 1) <>"." Then '为了防止重复查找

If GetAttr(Path &"\" &sPath) And vbDirectory Then '如果是文件夹则。。。。。。

BA = BA + 1

ReDim Preserve Folder(1 To BA)

Folder(BA) = Path &sPath &"\" '将目录和文件夹名称组合形成新的目录,并存放到数组中

End If

End If

sPath = Dir '查找下一个文件夹

DoEvents '让出控制权

Loop

For c = 1 To BA '使用递归方法,遍历所有目录

SearchFiles Folder(c), FileType, ListObj, ImageIndex

Next

End Function

建一个ImageList控件,右击,属性,在“图像”里插入图片(你的1.gif),看好他们的索引(Index,默认是1)。然后,代码如下晌稿消。 ========宴知==== Private Sub Command1_Click() TreeView1.ImageList = ImageList1 '关联Treeview1与Imagelist1 TreeView1.LineStyle = tvwRootLines Dim nodX As Node TreeView1.Nodes.Add , , "a1", "teachername" Set nodX = TreeView1.Nodes.Add("a1", tvwChild, , "教师姓名", 1) '这个敬告1的意思是刚才设置的图片在ImageList里的索引 End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12164554.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存