#include <GuiTreeView.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>
#include <StaticConstants.au3>
#include <MsgBoxConstants.au3>
#include <Excel.au3>
#include <Word.au3>

Global $splash, $bgcolor = 0x404040, $txtcolor = 0xff8800, $extcolor = 0xffcc00, $items[4000], $names[4000], $StartDir, $TreevieItemNow
$Main = GUICreate("Konzern-Regelwerke", 1700, 1000, -1, -1)
$Tree =  GUICtrlCreateTreeView(6, 0, 361, 940)
$StartDir = @ScriptDir 
$TrreViewOfprograms = GUICtrlCreateTreeViewItem(@ScriptDir, $Tree)
$TimerStamp = TimerInit()
MakeList($StartDir, $TrreViewOfprograms, 0)
MakeList($StartDir, $TrreViewOfprograms, 1)
ReDim $items[$items[0] + 1]
ReDim $names[$names[0] + 1]

_GUICtrlTreeView_Expand($Tree)



$oIE = ObjCreate("Shell.Explorer.2")

$GUIActiveX = GUICtrlCreateObj ($oIE, 372, 0, 1300, 980)


 Local $idButton_Read = GUICtrlCreateButton("Öffnen", 2,950,100,30)
 Local $idButton_Close = GUICtrlCreateButton("Beenden", 268,950,100,30)
 $oIE.navigate("C:\Users\reneh\Desktop\KRWD-Offline\Fahrrad\Blinklichtanlage am Pedelec.pdf") 

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            If $msg >= $items[1] And $msg <= $items[UBound($items) - 1] Then ishidden(_GUICtrlTreeView_GetTree($Tree, "\"))
            Sleep(20)
            GUICtrlSetState($names, BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) 
    EndSwitch
WEnd

Func MakeList($dir, $TreeViewOfprograms, $mode = 0)
    FileChangeDir($dir)
    $search = FileFindFirstFile("*.*")
    While 1
        $file = FileFindNextFile($search);
        If @error Then ExitLoop
        $GetExt = GetExt($dir & "\" & $file)
        If $items[0] > 3998 Then ExitLoop
        If $GetExt <> "folder" And $mode = 1 And $GetExt <> "ini" Then
            $items[0] += 1
            $names[0] += 1
            $items[ $items[0] ] = GUICtrlCreateTreeViewItem($file, $TreeViewOfprograms)
            $names[ $names[0] ] = $file
            GUICtrlSetColor($items[ $items[0]], $txtcolor)
        ElseIf $GetExt = "folder" And $mode = 0 Then
            $TreevieItemNow = GUICtrlCreateTreeViewItem($file, $TreeViewOfprograms)
            $items[0] += 1
            $names[0] += 1
            $items[ $items[0] ] = $TreevieItemNow
            $names[ $names[0] ] = $file
            GUICtrlSetColor($items[ $items[0]], $extcolor)
            MakeList($dir & "\" & $file, $TreevieItemNow, 0)
            MakeList($dir & "\" & $file, $TreevieItemNow, 1)
        EndIf
    WEnd
    FileClose($search)

EndFunc 

Func GetExt($file)
    If StringInStr(FileGetAttrib($file), "D") Then Return "folder"
    Return StringUpper(StringTrimLeft($file, StringInStr($file, ".", 0, -1)))
EndFunc  

Func ishidden($file)
    Local $attrib
    $attrib = FileGetAttrib($file)
    $file = StringReplace($File, "|","\")
    If StringRight($file, 3) = "pdf" Then 
		$oIE.navigate($file)
    ElseIf  StringRight($file, 4) = "xlsx" Or StringRight($file, 4) = "xlsm" Then 
		Local $oExcel = _Excel_Open()
    	Local $sWorkbook = $file
        Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook)
    ElseIf  StringRight($file, 4) = "docx" Or StringRight($file, 4) = "docm" Then
    	Local $sWordDoc = $file
        Local $oWordDoc = _Word_DocOpen($oWord, $sWordDoc)	
	
	EndIf	
	If StringInStr($attrib, "H") Then Return 1
    Return 0
EndFunc
