#Region ;**** Anweisungen an die AutoIt-GUI ****
#AutoIt3Wrapper_Icon=icon\law.ico
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Compile_Both=n
#AutoIt3Wrapper_UseX64=Y
#AutoIt3Wrapper_UseUpx=N
#AutoIt3Wrapper_Res_Language=1031

#EndRegion ;**** Erstellt von AutoIt-GUI ****

#include <APIShellExConstants.au3>
#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
#include <WinAPIFiles.au3>
#include <WinAPIIcons.au3>
#include <WinAPIRes.au3>
#include <WinAPIShellEx.au3>
#include <WinAPIShPath.au3>
#include <WindowsConstants.au3>
#include <Excel.au3>
#include <Word.au3>
#include <GuiToolbar.au3>

Global $aSize, $g_hToolbar, $g_idMemo, $g_iItem
Global Enum $e_idNew = 1000, $e_idOpen, $e_idSave, $e_idHelp, $e_idClose

Global Const $sSearchDir = @ScriptDir
Global Const $sFileExtensions = '*.pdf;*.docx;*.xlsm'
Global Const $bLargeIcons = False

Global $mPathMap[]
_WinAPI_FileIconInit()
Global $hSystemImgList = _WinAPI_ShellGetImageList(Not $bLargeIcons)
Global $hBusyCur = _WinAPI_LoadCursorFromFile(@WindowsDir & '\Cursors\aero_busy_xl.ani')

Global $hMainGui = GUICreate('Konzernregelwerke', 1700, 1000, -1, -1)
Global $g_hToolbar = _GUICtrlToolbar_Create($hMainGui)
Global $aSize = _GUICtrlToolbar_GetMaxSize($g_hToolbar)

_GUICtrlToolbar_AddBitmap($g_hToolbar, 1, -1, $IDB_STD_LARGE_COLOR)

_GUICtrlToolbar_AddButton($g_hToolbar, $e_idOpen, $STD_FILEOPEN)
_GUICtrlToolbar_AddButton($g_hToolbar, $e_idNew, 5)

Global Const $oIE = ObjCreate("Shell.Explorer.2")
Global Const $GUIActiveX = GUICtrlCreateObj ($oIE, 372, 50, 1300, 920)

Global $idTreeview = GUICtrlCreateTreeView(6, 50, 361, 920, $GUI_SS_DEFAULT_TREEVIEW, $WS_EX_CLIENTEDGE)
GUICtrlSetFont(-1, 10 + $bLargeIcons, 400, 0, 'Verdana')
GUICtrlSetResizing(-1, $GUI_DOCKBOTTOM + $GUI_DOCKTOP + $GUI_DOCKLEFT)
_GUICtrlTreeView_SetNormalImageList($idTreeview, $hSystemImgList)

Global $hRoot = _CreateRoot($idTreeview, $sSearchDir, $bLargeIcons)

GUISetState(@SW_SHOW, $hMainGui)

GUIRegisterMsg($WM_NOTIFY, '_WM_NOTIFY')

Global $hOldCur = _WinAPI_SetCursor($hBusyCur ? $hBusyCur : 9)
_GUICtrlTreeView_BeginUpdate($idTreeview)
_ReadDirectory($idTreeview, $hRoot, $sSearchDir, $bLargeIcons)
_GUICtrlTreeView_Expand($idTreeView)
_GUICtrlTreeView_EndUpdate($idTreeview)
_WinAPI_SetCursor($hOldCur)
$oIE.navigate(@ScriptDir & "\startseite.pdf")
_SendMessage(GUICtrlGetHandle($idTreeview), $TVM_EXPAND, $TVE_EXPAND, $hRoot, 0, 'wparam', 'handle')

     While 1
          Switch GUIGetMsg()
                     Case $GUI_EVENT_CLOSE
                             ExitLoop
		  EndSwitch
	WEnd

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $tNMTREEVIEW, $tNMHDR, $hItem, $hWndFrom, $sPath
    Local $sSepChar = Opt('GUIDataSeparatorChar')
	       $iCode = DllStructGetData($tNMHDR, "Code")
    $tNMTREEVIEW = DllStructCreate($tagNMTREEVIEW, $ilParam)
    $hWndFrom = DllStructGetData($tNMTREEVIEW, 'hWndFrom')
    Switch $hWndFrom
        Case GUICtrlGetHandle($idTreeview)
            Switch DllStructGetData($tNMTREEVIEW, 'Code')
                Case $TVN_SELCHANGEDA, $TVN_SELCHANGEDW
                    $hItem = DllStructGetData($tNMTREEVIEW, 'NewhItem')
                    If $hItem Then
                        $sPath = $mPathMap[$hItem]
                        If StringRight($sPath, 3) = "pdf" Then
		                   $oIE.navigate($sPath)
                        ElseIf  StringRight($sPath, 4) = "xlsx" Or StringRight($sPath, 4) = "xlsm" Then
							    Local $oExcel = _Excel_Open()
    	                        Local $sWorkbook = $sPath
                                _Excel_BookOpen($oExcel, $sWorkbook)
                        ElseIf  StringRight($sPath, 4) = "docx" Or StringRight($sPath, 4) = "docm" Then
		                        Local $oWord = _Word_Create()
    	                        Local $sWordDoc = $sPath
                                _Word_DocOpen($oWord, $sWordDoc)
	                    EndIf
                    EndIf
			EndSwitch

	EndSwitch

        Local $tNMHDR, $hWndFrom, $iCode, $iNew, $iFlags, $iOld
        Local $tNMTBHOTITEM
        $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
        $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
        $iCode = DllStructGetData($tNMHDR, "Code")
        Switch $hWndFrom
                Case $g_hToolbar
                        Switch $iCode
                                Case $NM_LDOWN
									 If $g_iItem = "1001"  Then
										Global $sFile = FileOpenDialog("Wähle eine PDF-Datei", @ScriptDir, "PDF Files(*.pdf)", 3) ; put your own start folder here
                                           If @error Then
                                              MsgBox("", "Hinweis", "Es wurde keine Datei ausgewählt")
										   Else
                                              $oIE.navigate($sFile)
						                   EndIf
										ElseIf $g_iItem = "1000" Then
										   Exit
										EndIf
                                Case $TBN_HOTITEMCHANGE
                                        $tNMTBHOTITEM = DllStructCreate($tagNMTBHOTITEM, $ilParam)
                                        $iOld = DllStructGetData($tNMTBHOTITEM, "idOld")
                                        $iNew = DllStructGetData($tNMTBHOTITEM, "idNew")
                                        $g_iItem = $iNew
                                        $iFlags = DllStructGetData($tNMTBHOTITEM, "dwFlags")
                        EndSwitch
        EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

Func _CreateRoot(ByRef $idTreeview, $sPath, $bLargeIcons)
    Local $iIndex, $hItem, $sName
    $iIndex = _GUIImageList_GetFileIconIndex($sPath, $bLargeIcons, True)
    $sName = StringRegExpReplace($sPath, '.+\\(.+)', '$1')
    $hItem = _GUICtrlTreeView_AddFirst($idTreeview, 0, $sName, $iIndex, $iIndex)
    $mPathMap[$hItem] = $sPath ; den Pfad anhand des Item-Handle speichern
    Return $hItem
EndFunc   ;==>_CreateRoot

Func _ReadDirectory(ByRef $idTreeview, $hItem, $sPath, $bLargeIcons)
    Local $iIndex, $aFolder, $aFiles, $hChild = $hItem, $sName
    If StringRight($sPath, 1) <> '\' Then $sPath &= '\'
    $aFolder = _WinAPI_EnumFiles($sPath, 2) ; nur Verzeichnisse einlesen
    If Not @error Then
        For $i = 1 To $aFolder[0][0]
            If Not BitAND($aFolder[$i][6], $FILE_ATTRIBUTE_REPARSE_POINT) And _
                    Not BitAND($aFolder[$i][6], $FILE_ATTRIBUTE_HIDDEN) Then
                $sName = StringRegExpReplace($sPath & $aFolder[$i][0], '.+\\(.+)', '$1')
                $iIndex = _GUIImageList_GetFileIconIndex($sPath & $aFolder[$i][0], $bLargeIcons, True)
                $hChild = _GUICtrlTreeView_AddChild($idTreeview, $hItem, $sName, $iIndex, $iIndex)
                $mPathMap[$hChild] = $sPath & $aFolder[$i][0] ; den Pfad anhand des Item-Handle speichern
                _ReadDirectory($idTreeview, $hChild, $sPath & $aFolder[$i][0], $bLargeIcons)
            EndIf
        Next
    EndIf
    _WinAPI_SetCursor($hBusyCur ? $hBusyCur : 9)
    $aFiles = _WinAPI_EnumFiles($sPath, 1, $sFileExtensions) ; nur Dateien einlesen
    If Not @error Then
        For $i = 1 To $aFiles[0][0]
            If Not BitAND($aFiles[$i][6], $FILE_ATTRIBUTE_HIDDEN) Then
                $sName = StringRegExpReplace($sPath & $aFiles[$i][0], '.+\\(.+)\..+', '$1')
                $iIndex = _GUIImageList_GetFileIconIndex($sPath & $aFiles[$i][0], $bLargeIcons, True)
                $hChild = _GUICtrlTreeView_AddChild($idTreeview, $hItem, $sName, $iIndex, $iIndex)
                $mPathMap[$hChild] = $sPath & $aFiles[$i][0] ; den Pfad anhand des Item-Handle speichern
            EndIf
        Next
    EndIf
EndFunc   ;==>_ReadDirectory

Func _GUIImageList_GetFileIconIndex($sFilePath, $bLargeIcons = False, $bForceLoadFromDisk = False)
    Local $tFileInfo = DllStructCreate($tagSHFILEINFO)
    Local $iFlags = BitOR($SHGFI_SYSICONINDEX, _
            $bLargeIcons ? $SHGFI_LARGEICON : $SHGFI_SMALLICON, _
            $bForceLoadFromDisk ? 0 : $SHGFI_USEFILEATTRIBUTES)
    Local $ret = _WinAPI_ShellGetFileInfo($sFilePath, $iFlags, $FILE_ATTRIBUTE_NORMAL, $tFileInfo)
    If $ret Then Return DllStructGetData($tFileInfo, 'iIcon')
    Return SetError(1, 0, -1)
EndFunc   ;==>_GUIImageList_GetFileIconIndex