Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#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, "File/Folder Browser",300,300,370,330, %WS_OverlappedWindow To hDlg
Control Add ListBox, hDlg, %IDC_ListBox, , 190, 15, 170, 320
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
DisplayFiles
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
Sub DisplayFiles
Local temp As String
Local hNode As DWord
ListBox Reset hDlg, %IDC_ListBox
ListBox Add hDlg, %IDC_ListBox, "<no files>"
TreeView Get Select hDlg, %IDC_Treeview To hNode
temp = Dir$(FullTreePath(hNode)+"\*.*")
If Len(temp) Then ListBox Reset hDlg, %IDC_ListBox
Do
If Len(temp) Then ListBox Add hDlg, %IDC_ListBox, temp
temp = Dir$
Loop While Len(temp)
End Sub
'gbs_01001
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm