Datei-Explorer mit TreeView Ordnerüberblick und ListView Datailansicht

  • Ich habe im Zusammenhang mit einem anderen Projekt einen Datei-Explorer geschrieben.
    Navigiert wird über eine TreeView, über die auf alle Laufwerke und Unterordner zugegriffen werden kann. Mit Klick auf ein Item wird der jeweilige Ordner in der Listview angezeigt.
    Dabei werden erst die Ordner und dann die Dateien aufgelistet. Ganz Oben befindet sich ein Ordner mit der Beschriftung "..", um in das übergeordnete Verzeichnis zu wechseln.
    Zur Verschönerung werden auch Icons angezeigt ;) .

    Hier das Script:

    Spoiler anzeigen
    [autoit]

    #include <GuiListView.au3>
    #include <GUIConstantsEx.au3>
    #include <GUIConstants.au3>
    #include <GuiTreeView.au3>
    #include <StructureConstants.au3>
    #include <TreeViewConstants.au3>
    #include <array.au3>
    #include <file.au3>
    Opt("GUIOnEventMode", 1)

    [/autoit] [autoit][/autoit] [autoit]

    global $ExplorerGui,$ExplorerGui_ListTree1,$ExplorerGui_ListView1
    global $aktTreePath="",$FoldersCount=0
    global $newServerGui,$newServerAdress,$newServerUser,$newServerPassword,$newServerPort

    [/autoit] [autoit][/autoit] [autoit]

    CreateExplorerGui()

    [/autoit] [autoit][/autoit] [autoit]

    GUIRegisterMsg($WM_NOTIFY, '_WM_NOTIFY')
    ;Icons in eine Liste speichern
    Global $hImage = _GUIImageList_Create(16, 16, 5, 1)
    _GUIImageList_AddIcon($hImage, 'shell32.dll', 3) ; Verzeichnis-Icon
    _GUIImageList_AddIcon($hImage, 'shell32.dll', 110) ; Verzeichnis-Icon mit Haken
    _GUIImageList_AddIcon($hImage, 'shell32.dll', 1) ; Datei-Icon
    _GUIImageList_AddIcon($hImage, 'shell32.dll', 5) ; Diskette
    _GUIImageList_AddIcon($hImage, 'shell32.dll', 7) ; Wechseldatenträger
    _GUIImageList_AddIcon($hImage, 'shell32.dll', 8) ; Festplatte
    _GUIImageList_AddIcon($hImage, 'shell32.dll', 11) ; CDROM
    _GUIImageList_AddIcon($hImage, 'shell32.dll', 12) ; Netzwerklaufwerk
    _GUIImageList_AddIcon($hImage, 'shell32.dll', 53) ; Unbekannt
    _GUICtrlTreeView_SetNormalImageList($ExplorerGui_ListTree1, $hImage)
    _GUICtrlListView_SetImageList($ExplorerGui_ListView1,$hImage,1)
    ;Icons für TreeView und ListView gesetzt

    [/autoit] [autoit][/autoit] [autoit]

    ;Laufwerke werden aufgelistet
    If ToolTip('Please wait...', Default, Default, 'Read Directory', 1) Then Local $aDrives = DriveGetDrive('ALL'), $iLWindex, $hRoot
    For $i = 1 To $aDrives[0]
    $iLWindex = 0
    Switch DriveGetType($aDrives[$i])
    Case 'Fixed'
    $iLWindex = 5
    Case 'CDROM'
    $iLWindex = 6
    Case 'RAMDisk'
    $iLWindex = 7
    Case 'Removable'
    $iLWindex = 4
    If StringLeft($aDrives[$i], 2) = 'a:' Or StringLeft($aDrives[$i], 2) = 'b:' Then $iLWindex = 3
    Case Else
    $iLWindex = 8
    EndSwitch
    $hRoot = _GUICtrlTreeView_Add(GUICtrlGetHandle($ExplorerGui_ListTree1), 0, StringUpper($aDrives[$i]), $iLWindex, $iLWindex)
    DirInTree("C:",$ExplorerGui_ListTree1,$hRoot,true,true)
    _GUICtrlTreeView_AddChild(GUICtrlGetHandle($ExplorerGui_ListTree1), $hRoot, "test", 0, 0)
    If DriveStatus($aDrives[$i]) <> 'READY' Then ContinueLoop
    Next
    ToolTip('')

    [/autoit] [autoit][/autoit] [autoit]

    ;ein bestimmter Pfad wird aufgerufen (Desktop)
    openTreePath(@DesktopDir,$ExplorerGui_ListTree1)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;Dauerschleife bis zum Ende des Programms
    while 1
    sleep(100)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func CreateExplorerGui()
    ;Fenster wird erzeut
    $ExplorerGui=GUICreate("",800,600)
    $ExplorerGui_ListTree1=GUICtrlCreateTreeView(10,10,780,280)
    ;Auf die Style der ListView achten!!!!
    $ExplorerGui_ListView1=GUICtrlCreateListView("Dateiname |Größe |Erstelldatum (YYYY/MM/DD HH:MM:SS) ",10,300,780,290,BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT),BitOR( $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    GUISetOnEvent($GUI_EVENT_CLOSE, "_ExplorerGui_Close",$ExplorerGui)
    GUISetState(@SW_SHOW,$ExplorerGui)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _ExplorerGui_Close()
    exit
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hChild, $hITEM, $tNMTREEVIEW = DllStructCreate($tagNMTREEVIEW, $ilParam)
    ;Notify Event für die Treeview
    If DllStructGetData($tNMTREEVIEW, 'hWndFrom') = GUICtrlGetHandle($ExplorerGui_ListTree1) Then
    Switch DllStructGetData($tNMTREEVIEW, 'Code')
    Case $TVN_ITEMEXPANDINGA, $TVN_ITEMEXPANDINGW
    ;Eintrag wird expandiert
    Switch DllStructGetData($tNMTREEVIEW, 'Action')
    Case $TVE_EXPAND
    $hITEM = DllStructGetData($tNMTREEVIEW, 'NewhItem')
    DirInTree("",$ExplorerGui_ListTree1,$hITEM,true,true)
    EndSwitch
    Case $TVN_SELCHANGEDW
    ;Selektion wurde geändert
    $sel=_GUICtrlTreeView_GetSelection(GUICtrlGetHandle($ExplorerGui_ListTree1))
    if $sel<>0 then
    Content(getPathFromTreeView(GUICtrlGetHandle($ExplorerGui_ListTree1),$sel)&"",$ExplorerGui_ListView1)
    endif
    EndSwitch
    EndIf
    ;Notify Event für die ListView
    If DllStructGetData($tNMTREEVIEW, 'hWndFrom') = GUICtrlGetHandle($ExplorerGui_ListView1) Then
    Switch DllStructGetData($tNMTREEVIEW, 'Code')
    Case $NM_DBLCLK
    ;doppelklick auf ein ListViewItem
    $selIndex=int(_GUICtrlListView_GetSelectedIndices($ExplorerGui_ListView1))
    if $selIndex>0 and $selIndex<$FoldersCount+1 then ;wechsle zum angeklickten Ordner $Folderscount, damit keine Dateien angeklickt werden können
    $selText=_GUICtrlListView_GetItemText($ExplorerGui_ListView1,$selIndex,0)
    openTreePath($selText,$ExplorerGui_ListTree1,_GUICtrlTreeView_GetSelection(GUICtrlGetHandle($ExplorerGui_ListTree1)))
    DirInTree("C:",$ExplorerGui_ListTree1,_GUICtrlTreeView_GetSelection(GUICtrlGetHandle($ExplorerGui_ListTree1)),true,true)
    elseif $selIndex=0 then ;erster Eintrag in der Listview angeklickt: in Übergeordneten Ordner wechseln
    $path=getPathFromTreeView($ExplorerGui_ListTree1,_GUICtrlTreeView_GetSelection(GUICtrlGetHandle($ExplorerGui_ListTree1)))
    $ar=StringSplit($path,"")
    $string=""
    for $i=1 to UBound($ar)-2 step 1
    $string&=$ar[$i]&""
    next
    openTreePath($string,$ExplorerGui_ListTree1,_GUICtrlTreeView_GetSelection(GUICtrlGetHandle($ExplorerGui_ListTree1)),true)
    DirInTree("C:",$ExplorerGui_ListTree1,_GUICtrlTreeView_GetSelection(GUICtrlGetHandle($ExplorerGui_ListTree1)),true,true)
    endif
    EndSwitch
    EndIf
    Return $GUI_RUNDEFMSG
    EndFunc ;==>_WM_NOTIFY

    [/autoit] [autoit][/autoit] [autoit]

    Func DirInTree($sSearchPath,$Tree,$hItem=-1,$SearchAll=true,$ext=false) ;rekursiver Aufruf, um jeweils die nächste Ebenen anzuzeigen
    if $SearchAll then
    ToolTip('Please wait...', Default, Default, 'Read Directory', 1)
    _GUICtrlTreeView_BeginUpdate($Tree)
    endif
    local $hItemNew,$parent
    if $ext then
    $sSearchPath=getPathFromTreeView($Tree,$hItem)
    _GUICtrlTreeView_DeleteChildren($Tree,$hItem)
    endif
    $search=FileFindFirstFile($sSearchPath&""&"*.*")
    $ChildDirs=false
    While 1
    $path=FileFindNextFile($search)
    ;msgbox(48,"",$path)
    if @extended then
    if $hItem=-1 then
    $hItemNew=_GUICtrlTreeView_Add($Tree,0,$path,0,0)
    else
    $hItemNew=_GUICtrlTreeView_AddChild($Tree,$hItem,$path,0,0)
    endif
    if $SearchAll then
    if not DirInTree($sSearchPath&""&$path,$Tree,$hItemNew,false) then
    _GUICtrlTreeView_Delete($Tree,$hItemNew)
    if $hItem=-1 then
    $hItemNew=_GUICtrlTreeView_AddChild($Tree,0,$path,0,0)
    else
    $hItemNew=_GUICtrlTreeView_AddChild($Tree,$hItem,$path,0,0)
    endif
    endif
    endif
    $ChildDirs=true
    endif
    if @error=1 then
    #cs
    if not $ChildDirs then
    if $hItem=-1 then
    $hItemNew=_GUICtrlTreeView_Add($Tree,0,$path,0,0)
    else
    _GUICtrlTreeView_AddChild($Tree,$hItem,$path,0,0)
    endif
    endif
    #ce
    ExitLoop 1
    endif
    WEnd
    FileClose($search)
    if $SearchAll then
    _GUICtrlTreeView_EndUpdate($Tree)
    ToolTip('')
    endif
    return $ChildDirs
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func getPathFromTreeView($Tree,$hItem)
    return StringReplace(_GUICtrlTreeView_GetTree($Tree,$hItem),"|","")
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func Content($directory,$ListView) ;ListView mit Dateien und Ordnern füllen
    _GUICtrlListView_DeleteAllItems($ListView)
    $item = GUICtrlCreateListViewItem("..||", $ListView)
    _GUICtrlListView_AddSubItem( $ListView, 0, "..",0,0)
    $FileList=_FileListToArray($directory,'*.*', 2) ; Only files
    if @error then
    $FoldersCount=0
    else
    $FoldersCount=UBound($FileList)-1
    endif
    for $n = 1 to UBound($FileList)-1
    $File_Name = $FileList[$n]
    $File_size = ""
    $File_t = FileGetTime($directory & "" & $File_Name, 1) ; creation Time
    $File_time = $File_t[0] & "/" & $File_t[1] & "/" & $File_t[2] & " " & $File_t[3] & ":" & $File_t[4] & ":" & $File_t[5] ; detailed
    $item = GUICtrlCreateListViewItem($File_Name & "|" & $File_size & "|" & $File_time, $ListView)
    ;_GUICtrlListView_SetItemImage ( $ListView, $n-1, 0,0)
    _GUICtrlListView_AddSubItem( $ListView, $n, $File_Name,0,0)
    Next
    $FileList=_FileListToArray($directory,'*.*', 1) ; Only files
    for $n = 1 to UBound($FileList)-1
    $File_Name = $FileList[$n]
    $File_size = int(FileGetSize ( $directory & "" & $File_Name)/100)&" KB"
    $File_t = FileGetTime($directory & "" & $File_Name, 1) ; creation Time
    $File_time = $File_t[0] & "/" & $File_t[1] & "/" & $File_t[2] & " " & $File_t[3] & ":" & $File_t[4] & ":" & $File_t[5] ; detailed
    $item = GUICtrlCreateListViewItem($File_Name & "|" & $File_size & "|" & $File_time, $ListView)
    _GUICtrlListView_AddSubItem( $ListView,$FoldersCount+$n, $File_Name,0,2)
    Next
    EndFunc ;==>Content

    [/autoit] [autoit][/autoit] [autoit]

    Func openTreePath($dir,$Tree,$current=0,$back=false) ;automatisch in ein bestimmtes Verzeichnis wechseln
    $text=""
    if $current=0 then
    $text=-1
    elseif $back then
    $ar=StringSplit(_GUICtrlTreeView_GetTree($Tree,$current),"|")
    $dir=""
    for $i=1 to UBound($ar)-3 step 1
    $dir&=$ar[$i]&""
    next
    $dir&=$ar[UBound($ar)-2]
    $text=$ar[UBound($ar)-3]
    ;msgbox(48,"",$dir&"&"&$text)
    else
    $text=_GUICtrlTreeView_GetText($Tree,$current)
    endif
    $ar=StringSplit($dir,"")
    ;$current=0
    for $i=1 to UBound($ar)-1 step 1
    if $ar[$i]<>"" then
    $ready=false
    while not $ready
    if $current<>0 then
    $current=_GUICtrlTreeView_FindItem($Tree,$ar[$i],false,$current)
    else
    $current=_GUICtrlTreeView_FindItem($Tree,$ar[$i])
    endif
    $str=_GUICtrlTreeView_GetTree($Tree,$current)
    $ar1=StringSplit($str,"|")
    ;msgbox(48,"",$ar1[UBound($ar1)-2]&"&"&$text)
    if $i<>UBound($ar)-1 or $text=-1 or $ar1[UBound($ar1)-2]=$text then
    _GUICtrlTreeView_Expand_self($Tree,$current,true)
    $ready=true
    else
    $current=_GUICtrlTreeView_GetNext ( $Tree, $current)
    endif
    WEnd
    endif
    next
    if UBound($ar)-1<3 then
    _GUICtrlTreeView_Expand_self($Tree,$current,true)
    endif
    ;_GUICtrlTreeView_SetSelected($Tree,$current)
    _GUICtrlTreeView_SelectItem($Tree,$current)
    Content(getPathFromTreeView($Tree,$current),$ExplorerGui_ListView1)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Author ........: Holger Kotsch
    ; Modified.......: Gary Frost
    ; ===============================================================================================================================
    Func _GUICtrlTreeView_Expand_self($hWnd, $hItem = 0, $fExpand = True)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)

    [/autoit] [autoit][/autoit] [autoit]

    If $hItem = 0 Then $hItem = 0x00000000

    [/autoit] [autoit][/autoit] [autoit]

    If $hItem = 0x00000000 Then
    $hItem = $TVI_ROOT
    Else
    If Not IsHWnd($hItem) Then
    Local $hItem_tmp = GUICtrlGetHandle($hItem)
    If $hItem_tmp <> 0x00000000 Then $hItem = $hItem_tmp
    EndIf
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $fExpand Then
    __GUICtrlTreeView_ExpandItem_self($hWnd, $TVE_EXPAND, $hItem)
    Else
    __GUICtrlTreeView_ExpandItem_self($hWnd, $TVE_COLLAPSE, $hItem)
    EndIf
    EndFunc ;==>_GUICtrlTreeView_Expand

    [/autoit] [autoit][/autoit] [autoit]

    ; #INTERNAL_USE_ONLY# ===========================================================================================================
    ; Name...........: __GUICtrlTreeView_ExpandItem($hWnd, $iExpand, $hItem)
    ; Description ...: Expands/Collapes the item and child(ren), if any
    ; Syntax.........: __GUICtrlTreeView_ExpandItem ( $hWnd, $iExpand, $hItem )
    ; Parameters ....: $hWnd - Handle to the control
    ; Return values .:
    ; Author ........: Holger Kotsch
    ; Modified.......: Gary Frost (gafrost), Kanashius
    ; Remarks .......:
    ; Related .......:
    ; Link ..........:
    ; Example .......:
    ; ===============================================================================================================================
    Func __GUICtrlTreeView_ExpandItem_self($hWnd, $iExpand, $hItem)
    If Not IsHWnd($hWnd) Then

    [/autoit] [autoit][/autoit] [autoit]

    If $hItem = 0x00000000 Then
    $hItem = $TVI_ROOT
    Else
    $hItem = GUICtrlGetHandle($hItem)
    If $hItem = 0 Then Return
    EndIf
    $hWnd = GUICtrlGetHandle($hWnd)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    _SendMessage($hWnd, $TVM_EXPAND, $iExpand, $hItem, 0, "wparam", "handle")

    [/autoit] [autoit][/autoit] [autoit]

    If $iExpand = $TVE_EXPAND And $hItem > 0 Then _SendMessage($hWnd, $TVM_ENSUREVISIBLE, 0, $hItem, 0, "wparam", "handle")

    [/autoit] [autoit][/autoit] [autoit]

    $hItem = _SendMessage($hWnd, $TVM_GETNEXTITEM, $TVGN_CHILD, $hItem, 0, "wparam", "handle")
    EndFunc ;==>__GUICtrlTreeView_ExpandItem

    [/autoit]