Date: 02-16-2022
Return to Index
created by gbSnippets
'Viewing the directory structure within a application is a feature that
'many programmers want to include in their application. It's fairly simple
'to create a file explorer using the TreeView control.
'Rather than read the entire drive and then populate a treeview control,
'this snippet gets subfolders only when a node is expanded in the tree.
'This speeds startup significantly and ensures that content is updated
'each time a node is expanded.
'Primary Code:
'Starting with a tree containing a root node of c:\, child nodes are added for
'each subfolder under c:\. If a child is also a parent, it is given a
'place holder node so that it will be displayed with a + sign (available for
'expansion) in the treeview control.
'As nodes are expanded, the tree content is created real time (the place holder
'removed and subfolder nodes added as available).
'As nodes are collapsed, no action is taken. The next time a node is opened, its
'its content will be refreshed real time.
'Here's the primary code for handling expansion of a node:
'get text of parent node that is is expanding
TreeView Get Text hDlg, %IDC_TreeView, hParent To ParentDIR
ParentDIR = Trim$(ParentDIR, "\") 'this turns c:\ into c:
'remove all children to ensure display matches current directory structure
Do
TreeView Get Child hDlg, %IDC_TreeView, hParent to hChild
If hChild Then TreeView Delete hDlg, %IDC_TreeView, hChild
Loop While hChild
'add nodes for each subfolder (real time addition of child nodes)
ChildDir = Dir$(FullTreePath(hParent) + "\*.*", Only %SubDir)
While Len(ChildDIR)
TreeView Insert Item hDlg, %IDC_TreeView, hParent, %TVI_Last, 0, 0, ChildDIR To hTemp
ChildDIR = Dir$
Wend
'add dummy child to each node whose folder has subfolders (causes TreeView to display + sign)
TreeView Get Child hDlg, %IDC_TreeView, hParent to hChild
While hChild
FullPath = FullTreePath(hChild)
temp = Dir$(FullPath + "\*.*", Only %SubDir)
If Len(temp) Then TreeView Insert Item hDlg, %IDC_TreeView, hChild, %TVI_Last, 0, 0, "n/a" To hTemp
TreeView Get Next hDlg, %IDC_TreeView, hChild to hChild
Wend
'Compilable Example: (Jose Includes)
'Starting with c:\, this example displays all subfolders. Each subfolder is
'checked to see if it has children. If so, it is given a a place-holder child
'node so that the + sign is displayed in front of the subfolder node.
'When a node is expanded, the place-holder is removed and the tree is populated
'real-time with subfolders of the node. When a folder is collapsed, no action
'is taken. The next time it is expanded, the subfolder list be be freshened.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "CommCtrl.inc"
%IDC_TreeView = 500 : %IDC_ListBox = 501
Global hDlg as DWord
Function PBMain() As Long
Local hItem As DWord
Dialog New Pixels, 0, "Test Code",300,300,200,350, %WS_OverlappedWindow To hDlg
Control Add TreeView, hDlg, %IDC_TreeView, "TopLeft", 10, 15, 170, 300
TreeView Insert Item hDlg, %IDC_TreeView, 0, %TVI_Last, 1,1,"c:\" To hItem
TreeView Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 1,4,"n/a" To hItem
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case %WM_InitDialog
Local hNode As DWord
TreeView Get Root hDlg, %IDC_TreeView To hNode
DisplayChildren hNode
TreeView Set Expanded hDlg, %IDC_TreeView, hNode, %True
Case %WM_NOTIFY
Select Case CB.NmID
Case %IDC_TreeView
Select Case CB.Nmcode
Case %TVN_ItemExpanding
Local N as NM_TreeView PTR
N = CB.lParam
DisplayChildren @N.ItemNew.hItem
Case %TVN_SelChanged
'Do something when a node is selected
End Select
End Select
End Select
End Function
Sub DisplayChildren (hParent As DWord)
Local hChild, hTemp As DWord
Local ChildDIR, ParentDIR, temp As String
Local FullPath as String
'get text of parent node that is is expanding
TreeView Get Text hDlg, %IDC_TreeView, hParent To ParentDIR
ParentDIR = Trim$(ParentDIR, "\") 'this turns c:\ into c:
'remove all children to ensure currency of display and directory structure
Do
TreeView Get Child hDlg, %IDC_TreeView, hParent to hChild
If hChild Then TreeView Delete hDlg, %IDC_TreeView, hChild
Loop While hChild
'add nodes for each subfolder
ChildDir = Dir$(FullTreePath(hParent) + "\*.*", Only %SubDir)
While Len(ChildDIR)
TreeView Insert Item hDlg, %IDC_TreeView, hParent, %TVI_Last, 0, 0, ChildDIR To hTemp
ChildDIR = Dir$
Wend
'add dummy child to each tree element whose folder has subfolders (causes TreeView to display + sign)
TreeView Get Child hDlg, %IDC_TreeView, hParent to hChild
While hChild
FullPath = FullTreePath(hChild)
temp = Dir$(FullPath + "\*.*", Only %SubDir)
If Len(temp) Then TreeView Insert Item hDlg, %IDC_TreeView, hChild, %TVI_Last, 0, 0, "n/a" To hTemp
TreeView Get Next hDlg, %IDC_TreeView, hChild to hChild
Wend
End Sub
Function FullTreePath(ByVal hNode As DWord) As String
'get full directory path for hNode
Local hRoot As DWord
Local FullPath, temp As String
Do
TreeView Get Text hDlg, %IDC_TreeView, hNode To temp
FullPath = "\" + temp + FullPath
TreeView Get Parent hDlg, %IDC_TreeView, hNode To hNode
Loop Until hNode = 0
Function = LTrim$(FullPath, "\")
End Function
'gbs_00525
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm