Attribute VB_Name = "mod_Explorer"
Option Explicit

Public itemMode As Boolean
Public FileLabel As String
Public Folderr As Folder 'a folder variable
Dim Item As listItem 'an item in de filelist
Dim oldSortkey As Integer 'in the Load_lvFilesIcon we change the sortkey, this variable is needed to return the sortkey
Dim oldSortorder As Integer 'same as sortkey
Dim hSIcon As Long 'SmallIcon
Dim hLIcon As Long 'LargeIcon
Dim r As Long 'just a common variable so an action can happen
Dim imgObj As ListImage 'Image in ImageList
Public Exclude As String
Public Excludebut As String
Public Const MAX_PATH = 260
Public SelectedList As Long
Public booDragCargo As Boolean
Public Loadcur As Long
Public Loadmax As Long
Public Loadsize As Long
Public lastDir As String


Public Declare Function ShellExecuteEX Lib "shell32.dll" Alias "ShellExecuteEx" (SEI As SHELLEXECUTEINFO) As Long
Public Const SEE_MASK_FLAG_NO_UI = &H400
Public Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Type SHELLEXECUTEINFO 'Api Type, contains all file information
        cbSize As Long
        fMask As Long
        hWnd As Long
        lpVerb As String
        lpFile As String
        lpParameters As String
        lpDirectory As String
        nShow As Long
        hInstApp As Long
        '  Optional fields
        lpIDList As Long
        lpClass As String
        hkeyClass As Long
        dwHotKey As Long
        hIcon As Long
        hProcess As Long
End Type
Public Type SHFILEINFO
        hIcon As Long                      '  out: icon
        iIcon As Long                      '  out: icon index
        dwAttributes As Long               '  out: SFGAO_ flags
        szDisplayName As String * MAX_PATH '  out: display name (or path)
        szTypeName As String * 80          '  out: type name
End Type

Public SHInfo As SHFILEINFO
Public Const SHGFI_SMALLICON = &H1                      '  get small icon
Public Const SHGFI_LARGEICON = &H0                      '  get large icon
Public Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long






Public Const LVI_NOITEM = &HFFFFFFFF
Public Const SHGFI_DISPLAYNAME = &H200
Public Const SHGFI_EXETYPE = &H2000
Public Const SHGFI_SYSICONINDEX = &H4000  ' System icon index
Public Const SHGFI_SHELLICONSIZE = &H4
Public Const SHGFI_TYPENAME = &H400
Public Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME _
   Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX _
   Or SHGFI_DISPLAYNAME Or SHGFI_EXETYPE
Public Const ILD_TRANSPARENT = &H1        ' Display transparent
Global Const HKEY_CURRENT_USER = &H80000001
Global Const KEY_ALL_ACCESS = &H3F
Global Const ERROR_NONE = 0
Global Const REG_SZ As Long = 1
Global Const REG_DWORD As Long = 4
Global Const ERROR_BADKEY = 2


Public m_hwndLV As Long   ' ListView1.hWnd
Public m_hwndTV As Long   ' TreeView1.hWnd
Public rootDir As String
Public strDragFile As String
Public strDragDir As String
Public strDragDest As String

Public Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Public fso As New FileSystemObject
Public file As file

Public Declare Function ImageList_Draw Lib "comctl32.dll" (ByVal himl As Long, ByVal I As Long, ByVal hDCDest As Long, ByVal X As Long, ByVal Y As Long, ByVal flags As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
    "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
    ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _
    Long) As Long
Private Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
   String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
   As Long, lpcbData As Long) As Long
Private Declare Function RegQueryValueExString Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
   String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
   As String, lpcbData As Long) As Long
Private Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
   String, ByVal lpReserved As Long, lpType As Long, lpData As _
   Long, lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" _
    (ByVal hKey As Long) As Long

Public Sub EXPLORER_Cargo_Add(strFileDir As String)
    Dim lngloop As Long

    If Mid(strFileDir, 3, 1) <> "\" Then
        Exit Sub
    End If
    If strFileDir = "" Or strFileDir = "NULL" Then
        Exit Sub
    End If

    For lngloop = 0 To frmMain.List1.ListCount - 1
        If UCase(frmMain.List1.List(lngloop)) = UCase(strFileDir) Then
            Exit Sub
        End If
    Next
    frmMain.List1.AddItem strFileDir
    EXPLORER_Cargo_Read
End Sub


Public Sub EXPLORER_Cargo_Clear()
    frmMain.List1.Clear
    EXPLORER_Cargo_Read
End Sub

Public Sub EXPLORER_Cargo_Delete()
    Dim lngloop As Long
    Dim strDelFile As String
    
    If frmMain.List1.ListCount >= 1 Then
        If MsgBox("Are you sure to delete " & frmMain.List1.ListCount & " item(s)?", vbQuestion + vbYesNo, "LCARS Terminal") = vbYes Then
            For lngloop = 0 To frmMain.List1.ListCount - 1
                strDelFile = frmMain.List1.List(lngloop)
                If fso.FileExists(strDelFile) Then
                    fso.DeleteFile strDelFile
                ElseIf fso.FolderExists(strDelFile) Then
                    fso.DeleteFolder strDelFile
                End If
            Next
            frmMain.List1.Clear
            EXPLORER_Cargo_Read
        End If
    End If
End Sub

Public Sub EXPLORER_Cargo_Read()
    Dim listcnt As Long
    Dim I As Long
    Dim FindPoint As Long
    Dim sLaunch As String
    Dim sValue As String
    Dim MyName
    Dim MyPath
    Dim hImgSmall As Long     ' The handle to the system image list
    Dim hImgLarge As Long     ' The handle to the system image list
    Dim fileName As String    ' The file name to get icon from
    Dim r As Long
    Dim tempKey As String
    Dim imgX
    Dim itmX
    Dim filenumber
    
    
    'filelist en fileview wissen!
    frmMain.LvFiles(1).ListItems.Clear
    frmMain.LvFiles(1).Icons = frmMain.ilCargoFake
    frmMain.LvFiles(1).Arrange = 2
    frmMain.LvFiles(1).SortOrder = 0
    
    frmMain.ILFiles32(1).ListImages.Clear
    For I = 0 To frmMain.List1.ListCount - 1
        fileName = frmMain.List1.List(I)
        hImgLarge& = SHGetFileInfo(fileName$, 0&, SHInfo, Len(SHInfo), BASIC_SHGFI_FLAGS Or SHGFI_LARGEICON)
        frmMain.PicFiles32.Picture = LoadPicture()
        ' Draw the associated icons into the picture boxes
        r& = ImageList_Draw(hImgLarge&, SHInfo.iIcon, frmMain.PicFiles32.hDC, 0, 0, ILD_TRANSPARENT)
        Set imgX = frmMain.ILFiles32(1).ListImages.Add(, , frmMain.PicFiles32.Image)
        imgX.Key = "P" & CStr(I) & "|" & Left$(SHInfo.szDisplayName, InStr(SHInfo.szDisplayName, Chr$(0)) - 1) & "|" & Mid$(Mid(frmMain.List1.List(I), 2), Len(Left$(SHInfo.szDisplayName, InStr(SHInfo.szDisplayName, Chr$(0)))))
        frmMain.LvFiles(1).Icons = frmMain.ILFiles32(1)
        Set itmX = frmMain.LvFiles(1).ListItems.Add()
        itmX.Icon = imgX.Key
        tempKey = Mid(imgX.Key, InStr(imgX.Key, "|") + 1)
        itmX.Text = Mid(tempKey, 1, InStr(tempKey, "|") - 1)
    Next
    'frmMain.cmdFake.SetFocus
End Sub

Public Sub EXPLORER_DropItem(strFileDir As String)
    Dim strDestPath As String
    Dim strDestFile As String
    Dim strFile As String
    Dim booRemove As Boolean
    Dim lngloop As Long
    
    If booDragCargo = False Then
        Exit Sub
    End If
    If frmMain.LvFiles(0).Tag = "" Then
        Exit Sub
    End If
    If Mid(strFileDir, 3, 1) <> "\" Then
        Exit Sub
    End If
    If strFileDir = "" Or strFileDir = "NULL" Then
        Exit Sub
    End If
    
    strDestPath = frmMain.LvFiles(0).Tag
    strFile = Mid(strFileDir, InStrRev(strFileDir, "\") + 1)
    If (Right(strDestPath, 1) = "\") Then
        strDestFile = strDestPath & strFile
    Else
        strDestFile = strDestPath & "\" & strFile
    End If
    
    FileLabel = strFileDir & vbCrLf & "copy / move / shortcut to" & vbCrLf & strDestFile
    frmFile.Show 1
    Select Case FileLabel
        Case "Copy"
            If fso.FileExists(strDestFile) = False Then
                If fso.FileExists(strFileDir) = True Then
                    fso.CopyFile strFileDir, strDestFile, False
                End If
                booRemove = True
            Else
                'Okay, file bestaat al, dus dan moet er een COPY worden aangemaakt!
                lngloop = InStrRev(strDestFile, ".")
                If lngloop > 0 Then
                    strDestFile = Mid(strDestFile, 1, lngloop - 1) & "_copy" & Mid(strDestFile, lngloop)
                    If fso.FileExists(strDestFile) = False Then
                        If fso.FileExists(strFileDir) = True Then
                            fso.CopyFile strFileDir, strDestFile, False
                        End If
                        booRemove = True
                    End If
                End If
            End If
        Case "Move"
            If fso.FileExists(strDestFile) = False Then
                If fso.FileExists(strFileDir) = True Then
                    fso.MoveFile strFileDir, strDestFile
                End If
                booRemove = True
            End If
        Case "Shortcut"
            lngloop = InStrRev(strDestFile, ".")
            If lngloop > 0 Then
                strDestFile = Mid(strDestFile, 1, lngloop - 1)
            End If
            If fso.FileExists(strDestFile & ".lnk") = False And fso.FileExists(strFileDir) = True Then
                CreateShortcut strDestFile, strFileDir
            End If
            booRemove = True
    End Select
    
    If booRemove = True Then
        For lngloop = 0 To frmMain.List1.ListCount - 1
            If frmMain.List1.List(lngloop) = strFileDir Then
                frmMain.List1.RemoveItem (lngloop)
                lngloop = frmMain.List1.ListCount + 1
            End If
        Next
        EXPLORER_Cargo_Read
    End If
    'MsgBox strFileDir & " -> " & strDestFile
End Sub


Public Sub EXPLORER_Item_Properties(Path As String)
  Dim SEI As SHELLEXECUTEINFO
  With SEI
    .cbSize = Len(SEI)
    .fMask = SEE_MASK_NOCLOSEPROCESS Or SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI
    .hWnd = frmMain.hWnd
    .lpVerb = "properties" 'commando so the right pop shows up
    .lpFile = Path
    .lpParameters = vbNullChar
    .lpDirectory = vbNullChar
    .nShow = 0
    .hInstApp = 0
    .lpIDList = 0
  End With
    
  Call ShellExecuteEX(SEI) 'executes the information
  
End Sub

Public Sub EXPLORER_Readitem(intIndex As Integer)
    Dim strLine As String
    Dim pidlDesktop As Integer  ' desktop's pidl, the TreeView's root (see below)
    
    Select Case intIndex
        Case 0
            'Quicklaunch
            rootDir = RegistryGet("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "Appdata", HKEY_CURRENT_USER) & "\Microsoft\Internet Explorer\Quick Launch"
            Load_lvFiles 0, rootDir, Exclude, Excludebut
        Case 1
            'Start Menu
            rootDir = RegistryGet("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "Start Menu", HKEY_CURRENT_USER)
            Load_lvFiles 0, rootDir, Exclude, Excludebut
        Case 2
            'Desktop
'            Call InsertRootFolder(frmMain.TreeView1, VarPtr(pidlDesktop))
            rootDir = ""
            Load_lvFiles 0, rootDir, Exclude, Excludebut
        Case 3
            'Favorites
            rootDir = RegistryGet("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "Favorites", HKEY_CURRENT_USER)
            Load_lvFiles 0, rootDir, Exclude, Excludebut
        Case 4
            'History
            rootDir = RegistryGet("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "Recent", HKEY_CURRENT_USER)
            Load_lvFiles 0, rootDir, Exclude, Excludebut
        Case 5
            'Directory Back
            'On Error Resume Next
            'frmMain.TreeView1.SelectedItem.Parent.Selected = True
            'On Error GoTo 0
    End Select
    
    If intIndex >= 65 And intIndex <= 90 Then
        If Check_Shareware = "" Then
            MsgBox RegisterText, vbInformation, "LCARS Terminal"
        End If
        On Error GoTo nodrive
        rootDir = Chr(intIndex) + ":"
        Dir rootDir & "\*.*"
        If Right(rootDir, 1) = "\" Then
            rootDir = Left(rootDir, Len(rootDir) - 1)
        End If
        'EXPLORER_TreeFind rootDir
        Exit Sub
nodrive:
        MsgBox "No Disk in Drive", vbInformation, "LCARS Terminal"
    End If
End Sub

Public Sub EXPLORER_RefreshList()
    Screen.MousePointer = vbHourglass
  
    Load_lvFiles 0, frmMain.LvFiles(0).Tag, Exclude, Excludebut
    Screen.MousePointer = vbDefault
End Sub

Public Sub EXPLORER_TreeCheck(nodRoot As Node, nodString As String)
    Dim nodChild As Node
    
    Do While ((nodRoot Is Nothing) = False)
        nodRoot.Expanded = True
        Set nodChild = nodRoot.Child
        Do While ((nodChild Is Nothing) = False)
            If (InStr(UCase(nodChild), nodString) > 0) Or UCase(nodChild) = UCase(nodString) Then
                nodChild.Expanded = True
                nodChild.Selected = True
                Exit Sub
            End If
            Set nodChild = nodChild.Next
        Loop
        nodRoot.Expanded = False
        Set nodRoot = nodRoot.Next
    Loop
End Sub


Public Sub Load_lvFiles(Index As Integer, Path As String, Exclude As String, Excludebut As String)
    Dim lngloop As Long
    
    If (Path <> "") Then
        On Error GoTo NoDiskInDrive
        ChDir Path
    End If
    
    frmMain.LvFiles(Index).ListItems.Clear
    frmMain.LvFiles(Index).SmallIcons = Nothing
    frmMain.LvFiles(Index).Icons = Nothing
    frmMain.ILFiles16(Index).ListImages.Clear
    frmMain.ILFiles32(Index).ListImages.Clear
    'the above code makes sure all lists are clear and unlinked to all objects
  
    Loadmax = 0
    Loadcur = 0
    lastDir = frmMain.LvFiles(Index).Tag
    If (Path <> "") Then
        ChDir Path 'change the current path of a drive
  
        frmMain.LvFiles(Index).Tag = CurDir(Path) 'get the real path (not c: but c:\program files\myprogram, for example)
        'add the active path to the lvfiles extra info
        'so we can always see it's location
  
        frmMain.lblDirInfo.Caption = frmMain.LvFiles(Index).Tag
  
        Dim subFolder As Folder 'make a second folder variable
        Set Folderr = fso.GetFolder(Path) 'set the variable so it contains the info for the path we want to explore
  
        For Each subFolder In Folderr.SubFolders
            Loadmax = Loadmax + 1
            Set Item = frmMain.LvFiles(Index).ListItems.Add
            Item.Text = "[" & subFolder.Name & "]"
            If frmMain.LvFiles(Index).View > 1 Then
                Item.Bold = True
            End If
            Item.ListSubItems.Add , , " "
            Item.ListSubItems.Add 2, , " "
            Item.ListSubItems.Add(3).Text = FileDateTime(subFolder.Path)
            Item.ListSubItems.Add(4).Text = GetAttributes(subFolder.Path)
            Item.Tag = subFolder.Path
        Next
        'the above code add's all subfolder to the list
        
        For Each file In Folderr.Files
            If Exclude <> "" Then 'if something must be excluded, exclude it
                If LCase(fso.GetExtensionName(file.Path)) <> LCase(Exclude) Then
                    Set Item = frmMain.LvFiles(Index).ListItems.Add
                    Item.Text = fso.GetBaseName(file.Path)
                    Item.Bold = False
                    Item.ListSubItems.Add 1, , fso.GetExtensionName(file.Path)
                    Item.ListSubItems.Add 2, , Round(file.Size / 1024, 0) & " kB"
                    Item.ListSubItems.Add 3, , FileDateTime(file.Path)
                    Item.ListSubItems.Add 4, , GetAttributes(file.Path)
                    Item.Tag = file.Path
                    Loadmax = Loadmax + 1
                End If
            ElseIf Excludebut <> "" Then 'same as exclude
                If LCase(fso.GetExtensionName(file.Path)) = LCase(Excludebut) Then
                    Set Item = frmMain.LvFiles(Index).ListItems.Add
                    Item.Text = fso.GetBaseName(file.Path)
                    Item.Bold = False
                    Item.ListSubItems.Add 1, , fso.GetExtensionName(file.Path)
                    Item.ListSubItems.Add 2, , Round(file.Size / 1024, 0) & " kB"
                    Item.ListSubItems.Add 3, , FileDateTime(file.Path)
                    Item.ListSubItems.Add 4, , GetAttributes(file.Path)
                    Item.Tag = file.Path
                    Loadmax = Loadmax + 1
                End If
            Else 'if nothing must be excluded then just load all
                Set Item = frmMain.LvFiles(Index).ListItems.Add
                Item.Text = fso.GetBaseName(file.Path)
                Item.Bold = False
                Item.ListSubItems.Add 1, , fso.GetExtensionName(file.Path)
                Item.ListSubItems.Add 2, , Round(file.Size / 1024, 0) & " kB"
                Item.ListSubItems.Add 3, , FileDateTime(file.Path)
                Item.ListSubItems.Add 4, , GetAttributes(file.Path)
                Item.Tag = file.Path
                Loadmax = Loadmax + 1
            End If
        Next
        'the above code add's all files to the list
        
        
        If Folderr.IsRootFolder = False Then
            Set Item = frmMain.LvFiles(Index).ListItems.Add(1)
            If frmMain.LvFiles(Index).View > 1 Then
                Item.Bold = True
            End If
            Item.Text = "[..]"
            Item.Tag = Left(Folderr.Path, Len(Folderr.Path) - Len(Folderr.Name))
            Item.ListSubItems.Add , , " "
            Item.ListSubItems.Add 2, , " "
            Loadmax = Loadmax + 1
        Else
            Set Item = frmMain.LvFiles(Index).ListItems.Add(1)
            If frmMain.LvFiles(Index).View > 1 Then
                Item.Bold = True
            End If
            Item.Text = "[..]"
            Item.Tag = ""
            Item.ListSubItems.Add , , " "
            Item.ListSubItems.Add 2, , " "
            Loadmax = Loadmax + 1
        End If
        'the above code checks if the folder is the rootfolder
        'if not then it puts an extra "[..]" item to list with index 1 so it shows
        'on top off the list.
        Loadmax = Loadmax * 4
        frmMain.LvFiles(0).Visible = False
        Load_lvFilesIcons (Index) 'go to phase 2; adding the icons
        frmMain.LvFiles(0).Visible = True
        
    Else
        frmMain.lblDirInfo.Caption = "Main Panel"
        frmMain.LvFiles(Index).Tag = ""
        Dim strTemp As String
        For lngloop = 0 To frmMain.Drive1.ListCount - 1
            strTemp = frmMain.Drive1.List(lngloop)
            Set Item = frmMain.LvFiles(Index).ListItems.Add
            Item.Text = strTemp
            'item.Text = strTemp
            If frmMain.LvFiles(Index).View > 1 Then
                Item.Bold = True
            End If
            Item.ListSubItems.Add , , " "
            Item.ListSubItems.Add 2, , " "
            'item.ListSubItems.Add(3).Text = FileDateTime(strTemp)
            'item.ListSubItems.Add(4).Text = GetAttributes(strTemp)
            Item.Tag = Left(strTemp, 2) & "\"
            Loadmax = Loadmax + 1
        Next
  
        strTemp = RegistryGet("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "Personal", HKEY_CURRENT_USER)
        Set Item = frmMain.LvFiles(Index).ListItems.Add
        Item.Text = "zz[" & strTemp & "]"
        'item.Text = strTemp
        If frmMain.LvFiles(Index).View > 1 Then
            Item.Bold = True
        End If
        Item.ListSubItems.Add , , " "
        Item.ListSubItems.Add 2, , " "
        'item.ListSubItems.Add(3).Text = FileDateTime(strTemp)
        'item.ListSubItems.Add(4).Text = GetAttributes(strTemp)
        Item.Tag = strTemp
        Loadmax = Loadmax + 1
  
        frmMain.LvFiles(0).Visible = False
        Loadmax = Loadmax * 4
        Load_lvFilesIcons (Index) 'go to phase 2; adding the icons
        frmMain.LvFiles(0).Visible = True
    End If
    Exit Sub
NoDiskInDrive:
    MsgBox "Error accessing disk", vbCritical, "LCARS Terminal"
End Sub

Private Sub Load_lvFilesBar()
    Loadcur = Loadcur + 1
    If Loadcur > Loadmax Then Loadcur = Loadmax
    'loadcur : loadmax = x : loadsize
    frmMain.picBar.Width = Int((Loadcur * Loadsize) / Loadmax)
    DoEvents
End Sub

Private Sub Load_lvFilesIcons(Index As Integer)
On Error Resume Next
With frmMain.LvFiles(Index)
  oldSortkey = .SortKey
  oldSortorder = .SortOrder
  .SortOrder = lvwAscending
  .SortKey = 1
  .Sorted = True
End With
'make sure the lists is sorted on fileextension
'this is needed to make sure we don't get double
'icons in the imagelists (there is a max to the amount of icons to the list)

Dim strExt As String 'Variable that contains the file's extension (like "exe")
Dim strLastExt As String 'Variable that contains the previous file's extension

  For Each Item In frmMain.LvFiles(Index).ListItems
    Load_lvFilesBar
    If fso.FileExists(Item.Tag) Then 'first we iconize (is that a word?) all files
      Set file = fso.GetFile(Item.Tag)
      strExt = LCase(fso.GetExtensionName(Item.Tag))
      If strExt <> strLastExt Then 'compare the currentfile extension with last
        'if they don't match then add a new icon to the list
        
        hSIcon = SHGetFileInfo(file.Path, 0&, SHInfo, Len(SHInfo), BASIC_SHGFI_FLAGS Or SHGFI_SMALLICON)
        hLIcon = SHGetFileInfo(file.Path, 0&, SHInfo, Len(SHInfo), BASIC_SHGFI_FLAGS Or SHGFI_LARGEICON)
        With frmMain.PicFiles16
          Set .Picture = LoadPicture("")
          .AutoRedraw = True
          r = ImageList_Draw(hSIcon, SHInfo.iIcon, .hDC, 0, 0, ILD_TRANSPARENT)
          .Refresh
        End With
        With frmMain.PicFiles32
          Set .Picture = LoadPicture("")
          .AutoRedraw = True
          r = ImageList_Draw(hLIcon, SHInfo.iIcon, .hDC, 0, 0, ILD_TRANSPARENT)
          .Refresh
        End With
        'the above code draw's the icon's in the pictureboxes
        'for details look at the Load_tbQuickLaunch procedure
        
        If strExt <> "exe" And strExt <> "ico" And strExt <> "lnk" Then
          'make sure it isn't an exe or ico file (they must be different each file)
          Set imgObj = frmMain.ILFiles16(Index).ListImages.Add(, , frmMain.PicFiles16.Image)
          Set imgObj = frmMain.ILFiles32(Index).ListImages.Add(, , frmMain.PicFiles32.Image)
          strLastExt = strExt
        Else
          Set imgObj = frmMain.ILFiles16(Index).ListImages.Add(, "exe" & Item.Index, frmMain.PicFiles16.Image)
          Set imgObj = frmMain.ILFiles32(Index).ListImages.Add(, "exe" & Item.Index, frmMain.PicFiles32.Image)
          strLastExt = "anything_but_exe_or_ico"
          'the strLastExt should be not a valid extension so every exe and ico and lnk will
          'get it's own private image
        End If
      End If
    ElseIf fso.FolderExists(Item.Tag) = True Then 'now handle the folders
      'all folders should have different icons as well
      'since they don't have extensionname's we just hope there aren't more
      'than 200 subfolders or so in a folder
      hSIcon = SHGetFileInfo(Item.Tag, 0&, SHInfo, Len(SHInfo), BASIC_SHGFI_FLAGS Or SHGFI_SMALLICON)
      hLIcon = SHGetFileInfo(Item.Tag, 0&, SHInfo, Len(SHInfo), BASIC_SHGFI_FLAGS Or SHGFI_LARGEICON)
      With frmMain.PicFiles16
        Set .Picture = LoadPicture("")
        .AutoRedraw = True
        r = ImageList_Draw(hSIcon, SHInfo.iIcon, .hDC, 0, 0, ILD_TRANSPARENT)
        .Refresh
      End With
      With frmMain.PicFiles32
        Set .Picture = LoadPicture("")
        .AutoRedraw = True
        r = ImageList_Draw(hLIcon, SHInfo.iIcon, .hDC, 0, 0, ILD_TRANSPARENT)
        .Refresh
      End With
      Set imgObj = frmMain.ILFiles16(Index).ListImages.Add(, Item.Tag, frmMain.PicFiles16.Image)
      Set imgObj = frmMain.ILFiles32(Index).ListImages.Add(, Item.Tag, frmMain.PicFiles32.Image)
    End If
  Next
  
  Dim strBadIconPath As String
  
  strBadIconPath = strProgramdir & strThemedir & "Explorer\noicon.ico"
  
  If fso.FileExists(strBadIconPath) = False Then
    Exit Sub
  End If
  
      hSIcon = SHGetFileInfo(strBadIconPath, 0&, SHInfo, Len(SHInfo), BASIC_SHGFI_FLAGS Or SHGFI_SMALLICON)
      hLIcon = SHGetFileInfo(strBadIconPath, 0&, SHInfo, Len(SHInfo), BASIC_SHGFI_FLAGS Or SHGFI_LARGEICON)
      With frmMain.PicFiles16
        Set .Picture = LoadPicture("")
        .AutoRedraw = True
        r = ImageList_Draw(hSIcon, SHInfo.iIcon, .hDC, 0, 0, ILD_TRANSPARENT)
        .Refresh
      End With
      With frmMain.PicFiles32
        Set .Picture = LoadPicture("")
        .AutoRedraw = True
        r = ImageList_Draw(hLIcon, SHInfo.iIcon, .hDC, 0, 0, ILD_TRANSPARENT)
        .Refresh
      End With
      Set imgObj = frmMain.ILFiles16(Index).ListImages.Add(, , frmMain.PicFiles16.Image)
      Set imgObj = frmMain.ILFiles32(Index).ListImages.Add(, , frmMain.PicFiles32.Image)
  
  'in the above text we put the badicon.ico in the imagelists
  
  frmMain.LvFiles(Index).SmallIcons = frmMain.ILFiles16(Index)
  frmMain.LvFiles(Index).Icons = frmMain.ILFiles32(Index)
  'link the imagelists to the filelists
  
  strLastExt = ""
  Dim Image As ListImage
  For Each Item In frmMain.LvFiles(Index).ListItems
    Load_lvFilesBar
    If fso.FileExists(Item.Tag) Then
      strExt = LCase(fso.GetExtensionName(Item.Tag))
      If strExt <> strLastExt Then
        If Item.Index = 1 Then
          Item.Icon = 1
          Item.SmallIcon = 1
          strLastExt = strExt
        Else
          Item.Icon = frmMain.LvFiles(Index).ListItems(Item.Index - 1).Icon + 1
          Item.SmallIcon = Item.Icon
          strLastExt = strExt
        End If
      Else
        If strExt <> "exe" And strExt <> "ico" And strExt <> "lnk" Then
          Item.Icon = frmMain.LvFiles(Index).ListItems(Item.Index - 1).Icon
          Item.SmallIcon = Item.Icon
        Else
          For Each Image In frmMain.ILFiles16(Index).ListImages
            If Image.Key = "exe" & Item.Index Then
              Item.Icon = Image.Index
              Item.SmallIcon = Image.Index
            End If
          Next
        End If
      End If
    ElseIf fso.FolderExists(Image.Tag) Then
      For Each Image In frmMain.ILFiles16(Index).ListImages
        If Image.Key = Item.Tag Then
          Item.Icon = Image.Index
          Item.SmallIcon = Image.Index
        End If
      Next
    End If
  Next
  
  'The abovetext pics the right image for each file
  'again using the strLastExt and strExt to make sure
  'the correct image's go to the correct files
  
  For Each Item In frmMain.LvFiles(Index).ListItems
    If Item.Icon = 0 Then
      Item.Icon = frmMain.ILFiles32(Index).ListImages.Count
      Item.SmallIcon = frmMain.ILFiles16(Index).ListImages.Count
    End If
  Next
  
  'For each item without an icon we'll give 'm the noicon
  'icon file, this is just the emptyfile icon included in the
  'directory.
  'If frmMain.LvFiles(Index).Tag <> "" Then
    Load_SortList Index, oldSortkey, oldSortorder 'goto phase three, sorting the list
  'End If
    
  

End Sub

Private Sub Load_SortList(Index As Integer, oldSortkey As Integer, oldSortorder As Integer)
On Error Resume Next
'This procedure is called both when a new folder is explored
'and if a columnheader is clicked
  
  For Each Item In frmMain.LvFiles(Index).ListItems
    Load_lvFilesBar
    If fso.FolderExists(Item.Tag) = True Then
      If Item.Text <> "[..]" Then
        Item.Text = Space(10) & Item.Text
        Item.ListSubItems(1).Text = Item.Text
        Item.ListSubItems(2).Text = Item.Text
        Item.ListSubItems(3).Text = Item.Text
      Else
        If oldSortorder <> lvwAscending Then
          Item.Text = "ZZZ" & Item.Text
          Item.ListSubItems(3).Text = "9999999"
        Else
          Item.Text = Space(10) & Item.Text
          Item.ListSubItems(3).Text = Item.Text
        End If
          Item.ListSubItems(1).Text = Item.Text
          Item.ListSubItems(2).Text = Item.Text
      End If
    ElseIf fso.FileExists(Item.Tag) = True Then
      Set file = fso.GetFile(Item.Tag)
      Item.ListSubItems(2).Text = Left("000000000000000", Len("000000000000000") - Len(file.Size)) & file.Size
      Item.ListSubItems(3).Text = Year(FileDateTime(file.Path)) & Month(FileDateTime(file.Path)) & Day(FileDateTime(file.Path)) & Hour(FileDateTime(file.Path)) & Minute(FileDateTime(file.Path))
    Else
        Item.Text = Space(10) & Item.Text
        'item.ListSubItems(1).Text = item.Text
        'item.ListSubItems(2).Text = item.Text
        'item.ListSubItems(3).Text = item.Text
    End If
  Next
  
  'the above code makes sure the folders always stick together
  'it also makes sure the "[..]" always stays on top of the list
  
  With frmMain.LvFiles(Index)
    .SortOrder = oldSortorder
    .SortKey = oldSortkey
    .Sorted = True
  End With
  'this is the default sortingcode
  'no comment actually
  
  
  For Each Item In frmMain.LvFiles(Index).ListItems
    Load_lvFilesBar
      If Right(Item.Text, 2) = ".]" Then
        Item.Text = "[..]"
        Item.ListSubItems(1).Text = " "
        Item.ListSubItems(2).Text = " "
        Item.ListSubItems(3).Text = " "
      Else
        If fso.FolderExists(Item.Tag) = True Then
          Set Folderr = fso.GetFolder(Item.Tag)
          If Folderr.Name = "" Then
            Item.Text = Trim(Item.Text)
          Else
            Item.Text = "[" & Folderr.Name & "]"
          End If
          Item.ListSubItems(1).Text = " "
          Item.ListSubItems(2).Text = " "
          Item.ListSubItems(3).Text = FileDateTime(Folderr.Path)
        ElseIf fso.FileExists(Item.Tag) = True Then
          Set file = fso.GetFile(Item.Tag)
          Item.ListSubItems(2).Text = Round(file.Size / 1024) & " kB"
          Item.ListSubItems(3).Text = FileDateTime(file.Path)
        Else
          Item.Text = Trim(Item.Text)
        End If
          
      End If
      Item.Selected = False
    Next
  'this code looks at every item, decides if there file or folder
  'and then returns the correct info to there textboxes again.
  'this code works fast enough so that users won't see the changing of the text
  
  'I bet there are more ways to sort lists this way, I choose this one
  'if you have a better one yourself... good job!
  'anyway that was the process of loading a folder with icons
  
End Sub


Private Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As _
   String, vValue As Variant) As Long
       Dim cch As Long
       Dim lrc As Long
       Dim lType As Long
       Dim lValue As Long
       Dim sValue As String

       On Error GoTo QueryValueExError

       ' Determine the size and type of data to be read
       lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
       If lrc <> ERROR_NONE Then Error 5

       Select Case lType
           ' For strings
           Case REG_SZ:
               sValue = String(cch, 0)
   lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, _
   sValue, cch)
               If lrc = ERROR_NONE Then
                   vValue = Left$(sValue, cch - 1)
               Else
                   vValue = Empty
               End If
           ' For DWORDS
           Case REG_DWORD, 3:
   lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, _
   lValue, cch)
               If lrc = ERROR_NONE Then vValue = lValue
           Case Else
               'all other data types not supported
               lrc = -1
       End Select

QueryValueExExit:
       QueryValueEx = lrc
       Exit Function
QueryValueExError:
       Resume QueryValueExExit
End Function

Public Function RegistryGet(sKeyName As String, sValueName As String, lPredefinedKey As Long) As String
    Dim lRetVal As Long         'result of the API functions
    Dim hKey As Long         'handle of opened key
    Dim vValue As Variant      'setting of queried value
    
    lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
    lRetVal = QueryValueEx(hKey, sValueName, vValue)
    RegCloseKey (hKey)
    If lRetVal = ERROR_BADKEY Then
        RegistryGet = ""
    Else
        RegistryGet = vValue
    End If
End Function

Private Function GetAttributes(Path As String) As String
Dim intAttributes As Integer
Dim strAttributes As String
Dim attFile As file
Dim attFolder As Folder
  
  If fso.FileExists(Path) Then
    Set attFile = fso.GetFile(Path)
    intAttributes = attFile.Attributes
  ElseIf fso.FolderExists(Path) Then
    Set attFolder = fso.GetFolder(Path)
    intAttributes = attFolder.Attributes - 16
    'we directly get 16 off the attributes of a folder
    'because that's the value of a folderattribute
    'and we already know it's a folder
  End If
  
  If intAttributes >= 32 Then '32 = archive
    intAttributes = intAttributes - 32
    strAttributes = "a"
  Else
    strAttributes = "-"
  End If
  If intAttributes >= 4 Then '4 = system
    intAttributes = intAttributes - 4
    strAttributes = strAttributes & "s"
  Else
    strAttributes = strAttributes & "-"
  End If
  If intAttributes >= 2 Then '2 = hidden
    intAttributes = intAttributes - 2
    strAttributes = strAttributes & "h"
  Else
    strAttributes = strAttributes & "-"
  End If
  If intAttributes = 1 Then '1 = read-only
    strAttributes = strAttributes & "r"
  Else
    strAttributes = strAttributes & "-"
  End If
  GetAttributes = strAttributes
End Function

