Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Explorer-Like TreeView slightly modified from AHKHelp


  • Please log in to reply
5 replies to this topic
ABCYourWay
  • Guests
  • Last active:
  • Joined: --
I wouldn't even try to make this witout the original script from AHK Help.
It's a TreeView example script in AHK Help.

As mentioned in AHK help

The following folder will be the root folder for the TreeView. Note that loading might take a long
time if an entire drive such as C:\ is specified:


Yeah, It took really huge amount of time to show Treeview.

So I wondered how Windows Explorer can be that fast.
well, we don't need to go though all the deeper level, we just need only two level deeper items from the selected item.

When the program starts we just add two-level subitems from the rootitem, and then everytime new items is selected we can repeat that task.
That way it works smooth.

If somebody can refine my crude script(logic or coding style), It'll be great.


Maybe we may need another GuiEvnet like .
That'll make it more robust.
I remember seeing something like that event somewhere but not exactly what it was.

I Put notes where the original script is modifed or added.

[Moderator's note: Pasted here corrected code]
; The following is a working script that is more elaborate than the one near the top of this page.
; It creates and displays a TreeView containing all folders in the all-users Start Menu.  When the
; user selects a folder, its contents are shown in a ListView to the right (like Windows Explorer).
; In addition, a StatusBar control shows information about the currently selected folder.

; The following folder will be the root folder for the TreeView. Note that loading might take a long
; time if an entire drive such as C:\ is specified:
;TreeRoot = %A_StartMenuCommon%

; TreeRoot = %A_StartMenuCommon%
TreeRoot = c:\  ; <--------------- modified

TreeViewWidth := 280

ListViewWidth := A_ScreenWidth - TreeViewWidth - 30

; Allow the user to maximize or drag-resize the window:
Gui +Resize

; Create an ImageList and put some standard system icons into it:
ImageListID := IL_Create(5)
Loop 5  ; Below omits the DLL's path so that it works on Windows 9x too:
    IL_Add(ImageListID, "shell32.dll", A_Index)

; Create a TreeView and a ListView side-by-side to behave like Windows Explorer:
Gui, Add, TreeView, vMyTree r20 w%TreeViewWidth% gMyTree ImageList%ImageListID%
Gui, Add, ListView, vMyList r20 w%ListViewWidth% x+10, Name|Modified

; Set the ListView's column widths (this is optional):
Col2Width = 70  ; Narrow to reveal only the YYYYMMDD part.
LV_ModifyCol(1, ListViewWidth - Col2Width - 30)  ; Allows room for vertical scrollbar.
LV_ModifyCol(2, Col2Width)

; Create a Status Bar to give info about the number of files and their total size:
Gui, Add, StatusBar
SB_SetParts(60, 85)  ; Create three parts in the bar (the third part fills all the remaining width).

; Add folders and their subfolders to the tree. Display the status in case loading takes a long time:
SplashTextOn, 200, 25, TreeView and StatusBar Example, Loading the tree...
AddSubFoldersToTree(TreeRoot)
SplashTextOff

; Display the window and return. The OS will notify the script whenever the user performs an eligible action:
Gui, Show,, %TreeRoot%  ; Display the source directory (TreeRoot) in the title bar.
return


;~ AddSubFoldersToTree(Folder, ParentItemID = 0)
;~ {
;~     ; This function adds to the TreeView all subfolders in the specified folder.
;~     ; It also calls itself recursively to gather nested folders to any depth.
;~     Loop %Folder%\*.*, 2  ; Retrieve all of Folder's sub-folders.
;~         AddSubFoldersToTree(A_LoopFileFullPath, TV_Add(A_LoopFileName, ParentItemID, "Icon4"))
;~ }

; ---------> modified to add item only two-level deeper items so that we can reduce the time to load Treeview greatly.

AddSubFoldersToTree(Folder, ParentItemID = 0)
{
    ; This function adds to the TreeView just two-level down folders
    Loop %Folder%\*.*, 2  ; in case of folders
	{
		P1 := TV_Add(A_LoopFileName, ParentItemID, "Icon4")
		loop, %A_LoopFileFullPath%\*.*, 2
		{
			TV_Add(A_LoopFileName, P1, "Icon4")
		}

	}

	loop, %folder%\*.*, 1                ; in case of Files
	{
		if a_loopfileattrib not contains D,H,S
		{
			P1 := TV_Add(A_loopFileName, ParentItemD, "")
		}
	}


}

MyTree:  ; This subroutine handles user actions (such as clicking).
if A_GuiEvent <> S  ; i.e. an event other than "select new tree item".
    return  ; Do nothing.
; Otherwise, populate the ListView with the contents of the selected folder.
; First determine the full path of the selected folder:

P2 := TV_GetSelection()    ; <--------- added to the original script
CH := TV_GetChild(P2)      ; <--------- added to the original script

; just add item only first child item
if not CH                  ; <--------- added to the original script
{
	TV_GetText(ChildText, CH)
	Loop, %SelectedFullPath%\%ChildText%\*.*, 2
	{
		TV_Add(A_LoopFileName, CH, "Icon4")
	}

}

TV_GetText(SelectedItemText, A_EventInfo)
ParentID := A_EventInfo
Loop  ; Build the full path to the selected folder.
{
    ParentID := TV_GetParent(ParentID)
    if not ParentID  ; No more ancestors.
        break
    TV_GetText(ParentText, ParentID)
    SelectedItemText = %ParentText%\%SelectedItemText%
}
SelectedFullPath = %TreeRoot%\%SelectedItemText%


Loop ; in case of child folders   <----------- added to the original script
{
	CH := TV_GetNext(CH)
	if not CH
		Break
		
	TV_GetText(ChildText, CH)
	Loop, %SelectedFullPath%\%ChildText%\*.*, 2
	{
		TV_Add(A_LoopFileName, CH, "Icon4")
	}
}

CH := TV_GetChild(P2)

loop,   ; in case of the same-level files   <---- added to the original script.
{
	CH := TV_GetNext(CH)
	if not CH
		Break
	TV_GetText(ChildText, CH)
	Loop, %SelectedFullPath%\%ChildText%\*.*, 1
	{
		if A_LoopFileAttrib not Contains D,H,S
		{
			TV_Add(A_LoopFileName, CH, "")
		}
	}
}



; Put the files into the ListView:
LV_Delete()  ; Clear all rows.
GuiControl, -Redraw, MyListView  ; Improve performance by disabling redrawing during load.
FileCount = 0  ; Init prior to loop below.
TotalSize = 0
Loop %SelectedFullPath%\*.*  ; For simplicity, this omits folders so that only files are shown in the ListView.
{
    LV_Add("", A_LoopFileName, A_LoopFileTimeModified)
    FileCount += 1
    TotalSize += A_LoopFileSize
}
GuiControl, +Redraw, MyListView

; Update the three parts of the status bar to show info about the currently selected folder:
SB_SetText(FileCount . " files", 1)
SB_SetText(Round(TotalSize / 1024, 1) . " KB", 2)
SB_SetText(SelectedFullPath, 3)
return

GuiSize:  ; Expand/shrink the ListView and TreeView in response to user's resizing of window.
if A_EventInfo = 1  ; The window has been minimized.  No action needed.
    return
; Otherwise, the window has been resized or maximized. Resize the controls to match.
GuiControl, Move, MyTree, % "H" . (A_GuiHeight - 30)  ; -30 for StatusBar and margins.
GuiControl, Move, MyList, % "H" . (A_GuiHeight - 30) . " W" . (A_GuiWidth - TreeViewWidth - 30)
return

GuiClose:  ; Exit the script when the user closes the TreeView's GUI window.
ExitApp



Return


ABCYourWay
  • Guests
  • Last active:
  • Joined: --
I'm sorry, I put the wrong code above.
The code above has some bugs, maybe this one, too. :wink:
In case I make mistakes, I should register in the forum.
I'm sorry again for the mess.

[Moderator's note: Cut & pasted above in place of faulty code]

Precise
  • Members
  • 31 posts
  • Last active: May 23 2011 01:55 AM
  • Joined: 30 Dec 2007
You my friend are a genius, thanks a million. This probably saves me several hours of tinkering and I'm totally in the dark here. I'm surprised no one else has commented on this yet. I really like the treeview control, the only thing that was turning me off, was the slow loading time when working with a low level directory. It seems to be working flawlessly. I'm using just the code that loads the folders and files for the treeview. I have added this to my basic treeview configuration script, which represents the base of all my future usage of treeview controls. Thanks again for posting this.

@ anyone who understands the specifics of how this works:
1) How exactly does this optimize the process of gathering the folders?

2) What exactly is the difference between this and the way it's handled in the help docs?

3) What are the disadvantages of doing it like this?

Edit: OK, well I found some simple bugs with this after testing it out a little more. When you open a directory, its folders are appended to the treeview. I also noticed what you meant about only going two levels down, for some reason I misunderstood that at first. Maybe with some optimization this could be perfected or even just improved?
Posted Image

Zaelia
  • Members
  • 754 posts
  • Last active: Jan 17 2015 02:38 AM
  • Joined: 31 Oct 2008
Hi :) ; sry for language error, I'm french...

I work under a similar project (and it's my first project, I'm noob too)

1/ use SetBatchLines -1 for "high speed" but it's an option (usefull with help doc example script )
2/ script in help doc is bad for C: (f*cking loading), we need only 1 subfolder level... SetBatchLines -1 is good, the first loading is very slow ( before "ROM" ) with allsubfolder of system drive...
3/ But we need 2 subfolder level for show "+" or "-" (treeview option +/-buttons)
4/ we need to show root and manage string with text of this root
5/ don't use a_guievent <> S, if you use + or - they don't run correctly. Then option altsubmit in treeview, after this you can use a_guievent = normal or a_guievent = + or a_guievent = -
6/ We need to delete child subfolder but build 2 level subfolder if a folder (thing := tv_getselection() ) is select ( or + is select -> use TV_Modify(a_eventinfo) )
7/ ... not idea at moment

My last version of script (under construction) if they can do some idea to you, it's just my work ad logic is not very good ( I have list view of drive to refresh usb key , and get a small treeview with not all drive and folder. At bottom you can select multi file with double click, button to select at bottom right)
#NoEnv
#NoTrayIcon
#SingleInstance Force
SetBatchLines -1

TreeRoot = %A_scriptdir%

gui, add, picture, Section Icon239 w16 h-1 grefresh, shell32.dll
gui, add, text, ys grefresh, Refresh drive

Gui, Add, ListView, xs w100 r7 vMyDrive gmydrive -hdr -Multi altsubmit, |
LV_ModifyCol(1, 79)

ImageListID := IL_Create(12)
LV_SetImageList(ImageListID)
Loop 12
    IL_Add(ImageListID, "shell32.dll", A_Index)

LV_Add( "icon10", "Directory")
LV_Add( "icon3", "Desktop")
LV_Add( "icon4", "Documents")
LV_Add( "icon4", "Programs")

DriveGet, list, list, FIXED
StringSplit, Drivenumber, List,,
Loop, %Drivenumber0%
{
	letter := Drivenumber%a_index%
	LV_Add( "icon9", Drivenumber%a_index% . ":")
}
DriveGet, list, list, CDROM
StringSplit, Drivenumber, List,,
Loop, %Drivenumber0%
{
	letter := Drivenumber%a_index%
	LV_Add( "icon12", Drivenumber%a_index% . ":")
}
DriveGet, list, list, REMOVABLE
StringSplit, Drivenumber, List,,
Loop, %Drivenumber0%
{
	letter := Drivenumber%a_index%
	LV_Add( "icon7", Drivenumber%a_index% . ":")
}

Gui, Add, TreeView, ys r10 w180 vMyTree gMyTree ImageList%ImageListID% Altsubmit
Gui, Add, ListView, ys r9 w340 vMyList gMyList -hdr -multi AltSubmit, |Modified|Size
LV_ModifyCol(1, 169)
LV_ModifyCol(2, 90)
LV_ModifyCol(3, 60)

ImageListID1 := IL_Create(10)
ImageListID2 := IL_Create(10, 10, true)
LV_SetImageList(ImageListID1)
LV_SetImageList(ImageListID2)

Gui, Add, ListView, xs r7 w600 vMyRun gMyRun -hdr -multi AltSubmit, FileRun
LV_ModifyCol(1, 679)

Gui, Add, StatusBar, gMyStatusBar
SB_SetParts(450)
SB_SetIcon("Shell32.dll", 69, 2)
SB_SetText("Ok", 2)

SplashTextOn, 200, 25, Custom GUI Explorer, Loading...
AddSubFoldersToTree(TreeRoot)
SplashTextOff

Gui, Show,, Custom GUI Explorer

AddSubFoldersToTree(folder, ID = 0)
{
ID := TV_Add(folder, ID, "Icon10 expand")
	Loop, %folder%\*.*, 2
	{
	IX := TV_Add(A_LoopFileName, ID, "Icon4")
	TV_gettext(FileNameX, IX)
		loop, %folder%\%FileNameX%\*.*, 2
		TV_Add(A_LoopFileName, IX, "Icon4")
	}
}

MyTree:

if a_guievent = normal
{
TV_Modify(a_eventinfo)

TV_GetText(SelectedItemText, A_EventInfo)
ParentID := A_EventInfo
Loop
{
    ParentID := TV_GetParent(ParentID)
    if not ParentID
	break
    TV_GetText(ParentText, ParentID)
    SelectedItemText = %ParentText%\%SelectedItemText%
}

SelectedFullPath = %TreeRoot%\%SelectedItemText%

StringReplace, SelectedFullPath, SelectedFullPath, \\ , \
StringRight, LastChar, SelectedFullPath, 1
if LastChar <> \
SelectedFullPath = %SelectedFullPath%\
StringReplace, SelectedFullPath, SelectedFullPath, %treeroot%\ ,

FilenameforSB =
SB_SetText(SelectedFullPath . FilenameforSB, 1)

IY := tv_getselection()
Loop
{
IYC := TV_GetChild(IY)
if not IYC
break
TV_delete(IYC)
}
Loop %SelectedFullPath%\*.*, 2
{
IX := TV_Add(A_LoopFileName, IY , "Icon4")
TV_gettext(FileNameX, IX)
loop %SelectedFullPath%\%FileNameX%\*.*, 2
TV_Add(A_LoopFileName, IX , "Icon4")
}

ToolTip, Loading...,,, 2

gui,listview,mylist

LV_Delete()

VarSetCapacity(Filename, 260)
sfi_size = 352
VarSetCapacity(sfi, sfi_size)

Loop %SelectedFullPath%*.*
{
    FileName := A_LoopFileFullPath 

    SplitPath, FileName,,, FileExt
    if FileExt in EXE,ICO,ANI,CUR,LNK
    {
        ExtID := FileExt
        IconNumber = 0
    }
    else 
    {
        ExtID = 0 
        Loop 7 
        {
            StringMid, ExtChar, FileExt, A_Index, 1
            if not ExtChar 
                break
            
            ExtID := ExtID | (Asc(ExtChar) << (8 * (A_Index - 1)))
        }
        IconNumber := IconArray%ExtID%
    }
    if not IconNumber
    {
        if not DllCall("Shell32\SHGetFileInfoA", "str", FileName, "uint", 0, "str", sfi, "uint", sfi_size, "uint", 0x101)
            IconNumber = 9999999 
        else
        {
            hIcon = 0
            Loop 4
                hIcon += *(&sfi + A_Index-1) << 8*(A_Index-1)
            IconNumber := DllCall("ImageList_ReplaceIcon", "uint", ImageListID1, "int", -1, "uint", hIcon) + 1
            DllCall("ImageList_ReplaceIcon", "uint", ImageListID2, "int", -1, "uint", hIcon)
            DllCall("DestroyIcon", "uint", hIcon)
            IconArray%ExtID% := IconNumber
        }
    }
modified := A_LoopFileTimeModified
StringSplit, modified, modified
modified =  %modified7%%modified8%/%modified5%%modified6%/%modified3%%modified4% %modified9%%modified10%:%modified11%%modified12%

size := a_loopfilesize . " Bytes"
if a_loopfilesize > 998
size := round(a_loopfilesize/1000,3) . " KB"
if a_loopfilesize > 9998
size := round(a_loopfilesize/1000,2) . " KB"
if a_loopfilesize > 99998
size := round(a_loopfilesize/1000,1) . " KB"
if a_loopfilesize > 999998
size := round(a_loopfilesize/1000000,3) . " MB"
if a_loopfilesize > 9999998
size := round(a_loopfilesize/1000000,2) . " MB"
if a_loopfilesize > 99999998
size := round(a_loopfilesize/1000000,1) . " MB"
if a_loopfilesize > 999999998
size := round(a_loopfilesize/1000000000,3) . " GB"

    LV_Add("Icon" . IconNumber, A_LoopFileName, modified, size)
}

ToolTip,,,, 2

}
return

MyList:

if A_GuiEvent = Normal
{
    LV_GetText(FileNameforSB, A_EventInfo, 1)
}

if a_guievent = doubleclick
{
gui,listview,myrun
LV_Add("", SelectedFullPath . FilenameforSB)
gui,listview,mylist
}

SB_SetText(SelectedFullPath . FilenameforSB, 1)
return

mydrive:

if A_GuiEvent = normal
{
Gui, ListView, MyDrive
    LV_GetText(drive, a_eventinfo, 1)
gui, listview, mylist

TV_delete()
lv_delete()

TreeRoot = %drive%
SB_SetText(drive . "\", 1)
if drive = directory
{
treeroot = %a_scriptdir%
SB_SetText(a_scriptdir, 1)
}
if drive = desktop
{
treeroot = %a_desktop%
SB_SetText(a_desktop, 1)
}
if drive = documents
{
treeroot = %A_MyDocuments%
SB_SetText(A_MyDocuments, 1)
}
if drive = programs
{
treeroot = %A_ProgramFiles%
SB_SetText(A_ProgramFiles, 1)
}

Tooltip, Loading...,,, 1
AddSubFoldersToTree(TreeRoot)
Tooltip,,,, 1

}
return

MyStatusBar:

if A_GuiEvent = normal
{
if  a_eventinfo = 2
gui,listview,myrun
Loop % LV_GetCount()
{
    LV_GetText(RetrievedText, A_Index)
    Msgbox %RetrievedText%
}
gui,listview,mylist
}
return

myrun:

if A_GuiEvent = doubleclick
{
gui,listview,myrun
lv_delete(a_eventinfo)
gui,listview,mylist
}

return


;--------------------------------------------------------------------------------------------------
refresh:
gui,listview,mydrive
lv_delete()
LV_Add("icon10","Directory")
LV_Add("icon3","Desktop")
LV_Add("icon4","Documents")
LV_Add("icon4","Programs")
DriveGet,list,list,FIXED
StringSplit,Drivenumber,List
Loop, %Drivenumber0%
{letter:=Drivenumber%a_index%
LV_Add("icon9",Drivenumber%a_index% . ":")
}
DriveGet,list,list,CDROM
StringSplit,Drivenumber,List
Loop, %Drivenumber0%
{letter:=Drivenumber%a_index%
LV_Add("icon12",Drivenumber%a_index% . ":")
}
DriveGet,list,list,REMOVABLE
StringSplit,Drivenumber,List
Loop, %Drivenumber0%
{letter:=Drivenumber%a_index%
LV_Add("icon7",Drivenumber%a_index% . ":")
}
gui,listview,mylist
return

GuiClose:
ExitApp


soggos
  • Members
  • 129 posts
  • Last active: Nov 30 2012 10:35 AM
  • Joined: 27 Mar 2008

Hi Smile ; sry for language error, I'm french...

salutations, moi aussi...
c'est pas toujours facile quand on parle pas la langue :)
c'est sympa de partager ta version du treeview.. as-tu avancer sur ce programme?
J'adore cette rapidité, j'ai ajouter plusieurs bricole comme le dropFiles, (c'est génial ça, y sont fort), le GuiContextMenu, une ptite option pour afficher ou non les dossiers (et classer par poids)..

Toutefois, j'ai un probleme avec les reactions du treeView sur mon deuxième ecran..> gros bug y fait plus on travail ¿..
celui-çi est resolu en supprimant if a_guievent = normal { TV_Modify(a_eventinfo) .... (voir commentaires ds mon script ci-dessous)
As-tu 2ecrans/as-tu eu ce bug?

voiçi donc ma ptit contribution
; ORIGINAle source:		http://www.autohotkey.com/forum/viewtopic.php?p=231571#231571
; contribution personnelle de soggos le venredi 23juillet2009
; ===================================================================================
#NoEnv
;#NoTrayIcon
#SingleInstance off 				; permet plusieurs instance ; #SingleInstance force|ignore|off
SetBatchLines -1

; ===================================================================================

	SetTimer , UPDATEDSCRIPT,1000 	; superPratique ; http://www.autohotkey.com/forum/topic135.html ; par skrommel

TreeRoot = %A_ScriptDir%


Menu, MyContextRUN, Add, zzeffacer, ContextClearRows		; pour avoir un menu sur MyRun liste

Menu, MyContextMenu, Add, Open, ContextOpenFile
Menu, MyContextMenu, Add, Ouvrir son Dossier, ContextOpenDOSSIER
Menu, MyContextMenu, Add, Properties, ContextProperties
Menu, MyContextMenu, Add, Clear from ListView, ContextClearRows
Menu, MyContextMenu, Default, Open  ; Make "Open" a bold font to indicate that double-click does the same thing.








; gui, add, picture, Section Icon22 w16 h-1 grefresh, shell32.dll
; gui, add, text, ys grefresh, Refresh drive

Gui, Add, ListView, x300 w200 y0 h120 vMyDrive gmydrive -hdr -Multi altsubmit +BackgroundSilver, |
LV_ModifyCol(1, 79)

ImageListID := IL_Create(12)
LV_SetImageList(ImageListID)
Loop 12
    IL_Add(ImageListID, "shell32.dll", A_Index)

LV_Add( "icon8", "PERSO1")			; mettre des dossier perso (super facile d'accès)
LV_Add( "icon8", "PERSO2")			; mettre des dossier perso (super facile d'accès)
LV_Add( "icon10", "Directory")		; le dossier du script 
LV_Add( "icon4", "Documents")
LV_Add( "icon12", "Desktop")
LV_Add( "icon4", "Programs")

DriveGet, list, list, FIXED
StringSplit, Drivenumber, List,,
Loop, %Drivenumber0%
{
   letter := Drivenumber%a_index%
   LV_Add( "icon9", Drivenumber%a_index% . ":")
}
DriveGet, list, list, CDROM
StringSplit, Drivenumber, List,,
Loop, %Drivenumber0%
{
   letter := Drivenumber%a_index%
   LV_Add( "icon12", Drivenumber%a_index% . ":")
}
DriveGet, list, list, REMOVABLE
StringSplit, Drivenumber, List,,
Loop, %Drivenumber0%
{
   letter := Drivenumber%a_index%
   LV_Add( "icon7", Drivenumber%a_index% . ":")
}

; ===================================================================================

Gui, Add, TreeView, x300 y120 w200 h400 vMyTree gMyTree ImageList%ImageListID% Altsubmit +BackgroundSilver
Gui, Add, ListView, x0 y0 w300 h400 vMyList gMyList -multi AltSubmit LV0x1 LV0x8000 0x100 +0x200 +BackgroundSilver, Nom|Modifié|Poids|chemin
LV_ModifyCol(1, 150)
LV_ModifyCol(2, 90)
LV_ModifyCol(3, 50)

ImageListID1 := IL_Create(10)
ImageListID2 := IL_Create(10, 10, true)
LV_SetImageList(ImageListID1)
LV_SetImageList(ImageListID2)

Gui, Add, ListView, x0 y400 h160 w300 vMyRun gMyRun -hdr -multi AltSubmit LV0x1 LV0x8000 0x100 +0x200 +BackgroundSilver, FileRun
LV_ModifyCol(1, 679)


Gui, Add, Checkbox, x310 y525 w160 h40 vafficher_dossier gafficher_dossier ,  afficher les dossiers et classer par poids	; permet d'afficher ou non les dossiers du dossier

Gui, Add, StatusBar, gMyStatusBar
SB_SetParts(450)
SB_SetIcon("Shell32.dll", 69, 2)
SB_SetText("Ok", 2)

SplashTextOn, 200, 25, Custom GUI Explorer, Loading...
AddSubFoldersToTree(TreeRoot)
SplashTextOff

Gui, Show,, Custom GUI Explorer - soggos version




;===================================================================

AddSubFoldersToTree(folder, ID = 0)
{
ID := TV_Add(folder, ID, "Icon10 expand")
   Loop, %folder%\*.*, 2
   {
   IX := TV_Add(A_LoopFileName, ID, "Icon4")
   TV_gettext(FileNameX, IX)
      loop, %folder%\%FileNameX%\*.*, 2
      TV_Add(A_LoopFileName, IX, "Icon4")
   }
}

;===================================================================


MyTree:

; if a_guievent = normal
; {
; TV_Modify(a_eventinfo)	; vat savoir pourquoi mais quand la console est sur le deuxieme ecran le treeView ne reagit plus!
							; or je dégage cette condition (if a_guievent = normal) et cela fonctionne sur les duex ecrans!. humhum mais ça marche mieux.



if A_GuiEvent <> S  ; i.e. an event other than "select new tree item".	; cela evite de recharger un arbre avec rien(racine/script par  defauft) quand on clique sur rien dans le treeView
    return  ; Do nothing.

TV_GetText(SelectedItemText, A_EventInfo)
	if SelectedItemText=
	return
	
	MyTree0:					; permet d'acceder au label sans passer par> TV_GetText(SelectedItemText, A_EventInfo) ....(*pour le clique sur le checkbox afficher_dossier)

ParentID := A_EventInfo
Loop
{
    ParentID := TV_GetParent(ParentID)
    if not ParentID
   break
    TV_GetText(ParentText, ParentID)
    SelectedItemText = %ParentText%\%SelectedItemText%
}

SelectedFullPath = %TreeRoot%\%SelectedItemText%

StringReplace, SelectedFullPath, SelectedFullPath, \\ , \
StringRight, LastChar, SelectedFullPath, 1
if LastChar <> \
SelectedFullPath = %SelectedFullPath%\
StringReplace, SelectedFullPath, SelectedFullPath, %treeroot%\ ,

FilenameforSB =
SB_SetText(SelectedFullPath . FilenameforSB, 1)

IY := tv_getselection()
Loop
{
IYC := TV_GetChild(IY)
if not IYC
break
TV_delete(IYC)
}
Loop %SelectedFullPath%\*.*, 2
{
IX := TV_Add(A_LoopFileName, IY , "Icon4")
TV_gettext(FileNameX, IX)
loop %SelectedFullPath%\%FileNameX%\*.*, 2
TV_Add(A_LoopFileName, IX , "Icon5")
}

ToolTip, Loading...,,, 2

gui,listview,mylist

LV_Delete()

VarSetCapacity(Filename, 260)
sfi_size = 352
VarSetCapacity(sfi, sfi_size)

; ===================================================================================
GuiControlGet , afficher_dossier		; recup de la  variable du checkbox
if afficher_dossier=1
quoi= 1
Else
quoi=0
GuiControl, -Redraw, MyList ; Improve performance by disabling redrawing during load. ; cacher Mylist pendant la recherche (+rapide)
Loop %SelectedFullPath%*.*,%quoi%		
{
    FileName := A_LoopFileFullPath 

    SplitPath, FileName,,, FileExt
    if FileExt in EXE,ICO,ANI,CUR,LNK
    {
        ExtID := FileExt
        IconNumber = 0
    }
    else 
    {
        ExtID = 0 
        Loop 7 
        {
            StringMid, ExtChar, FileExt, A_Index, 1
            if not ExtChar 
                break
            
            ExtID := ExtID | (Asc(ExtChar) << (8 * (A_Index - 1)))
        }
        IconNumber := IconArray%ExtID%
    }
    if not IconNumber
    {
        if not DllCall("Shell32\SHGetFileInfoA", "str", FileName, "uint", 0, "str", sfi, "uint", sfi_size, "uint", 0x101)
            IconNumber = 9999999 
        else
        {
            hIcon = 0
            Loop 4
                hIcon += *(&sfi + A_Index-1) << 8*(A_Index-1)
            IconNumber := DllCall("ImageList_ReplaceIcon", "uint", ImageListID1, "int", -1, "uint", hIcon) + 1
            DllCall("ImageList_ReplaceIcon", "uint", ImageListID2, "int", -1, "uint", hIcon)
            DllCall("DestroyIcon", "uint", hIcon)
            IconArray%ExtID% := IconNumber
        }
    }
modified := A_LoopFileTimeModified
 StringSplit, modified, modified
 modified =  %modified3%%modified4%/%modified5%%modified6%/%modified7%%modified8% %modified9%%modified10%:%modified11%%modified12% ; pour ranger ds l'ordre veritable avec  LV_ModifyCol(3, "Sort") 
;; original ; modified =  %modified7%%modified8%/%modified5%%modified6%/%modified3%%modified4% %modified9%%modified10%:%modified11%%modified12%	; améliore la vision
size := a_loopfilesize . " Bytes"
if a_loopfilesize > 998
size := round(a_loopfilesize/1000,3) . " KB"
; if a_loopfilesize > 9998
; size := round(a_loopfilesize/1000,2) . " KB"
; if a_loopfilesize > 99998
; size := round(a_loopfilesize/1000,1) . " KB"
; if a_loopfilesize > 999998
; size := round(a_loopfilesize/1000000,3) . " MB"
; if a_loopfilesize > 9999998
; size := round(a_loopfilesize/1000000,2) . " MB"
; if a_loopfilesize > 99999998
; size := round(a_loopfilesize/1000000,1) . " MB"
; if a_loopfilesize > 999999998
; size := round(a_loopfilesize/1000000000,3) . " GB"
    
	
	; 1exemple LV_Add("Icon" . IconNumber, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, FileExt)
    LV_Add("Icon" . IconNumber, A_LoopFileName, modified, size, A_LoopFileFullPath)
}

ToolTip,,,, 2

if afficher_dossier=1
 LV_ModifyCol(3, "Sort") 	; classer par poids permet d'afficher les dossiers en premiers (0 octets) * si affichage dossiers demandés)
 
GuiControl, +Redraw, MyList ; Improve performance by disabling redrawing during load. ; recherhe terminée affichages des ibjets trouvés

 ; }
return

; =================================================================================== mydrive

mydrive:

if A_GuiEvent = normal
{
Gui, ListView, MyDrive
    LV_GetText(drive, a_eventinfo, 1)
gui, listview, mylist

TV_delete()
lv_delete()

TreeRoot = %drive%
SB_SetText(drive . "\", 1)
if drive = directory
{
treeroot = %a_scriptdir%
SB_SetText(a_scriptdir, 1)
}
if drive = desktop
{
treeroot = %a_desktop%
SB_SetText(a_desktop, 1)
}
if drive = documents
{
treeroot = %A_MyDocuments%
SB_SetText(A_MyDocuments, 1)
}
if drive = programs
{
treeroot = %A_ProgramFiles%
SB_SetText(A_ProgramFiles, 1)
}



if drive = PERSO1
{
treeroot = c:\		; cheminPErso1
SB_SetText(treeroot, 1)
}
if drive = PERSO2
{
treeroot = c:\		; cheminPErso2
SB_SetText(treeroot, 1)
}


; ........& d'autres (à voir pour une intégration/lecture d'une liste contenue dans un fichier ini )


Tooltip, Loading...,,, 1
AddSubFoldersToTree(TreeRoot)
Tooltip,,,, 1

}
return
; ===================================================================================


; =================================================================================== MyList

MyList:

if A_GuiEvent = Normal
{
    LV_GetText(FileNameforSB, A_EventInfo, 1)
}

if a_guievent = doubleclick
{
	gui,listview,myrun
	; LV_Add("", SelectedFullPath . FilenameforSB)
	LV_Add("", FilenameforSB)
	gui,listview,mylist
}

SB_SetText(SelectedFullPath . FilenameforSB, 1)
return
; ===================================================================================


; =================================================================================== MyStatusBar

MyStatusBar:

if A_GuiEvent = normal
{
if  a_eventinfo = 
	gui,listview,myrun
	; Loop % LV_GetCount()
	; {
		; LV_GetText(RetrievedText, A_Index)
	; }
	gui,listview,mylist
		LV_GetText(fichier, A_EventInfo, 4)
			clipboard= %fichier%
}
return

; ===================================
myrun:

if A_GuiEvent = doubleclick
{
	gui,listview,myrun
		lv_delete(a_eventinfo)
	gui,listview,mylist
}
if A_GuiEvent = Normal
{
	gui,listview,myrun
		LV_GetText(fichier, A_EventInfo, 1)
			clipboard= %fichier%
	gui,listview,mylist
 		
}
return


;-------------------------------------------------------------------------------------------------- refresh
refresh:
gui,listview,mydrive
lv_delete()
LV_Add("icon10","Directory")
LV_Add("icon3","Desktop")
LV_Add("icon4","Documents")
LV_Add("icon4","Programs")
DriveGet,list,list,FIXED
StringSplit,Drivenumber,List
Loop, %Drivenumber0%
{letter:=Drivenumber%a_index%
LV_Add("icon9",Drivenumber%a_index% . ":")
}
DriveGet,list,list,CDROM
StringSplit,Drivenumber,List
Loop, %Drivenumber0%
{letter:=Drivenumber%a_index%
LV_Add("icon12",Drivenumber%a_index% . ":")
}
DriveGet,list,list,REMOVABLE
StringSplit,Drivenumber,List
Loop, %Drivenumber0%
{letter:=Drivenumber%a_index%
LV_Add("icon7",Drivenumber%a_index% . ":")
}
gui,listview,mylist
return
; ===================================================================================

; =================================================================================== GuiClose
GuiClose:
ExitApp
; ===================================================================================


; =================================================================================== GuiDropFiles
	GuiDropFiles:  ; Support drag & drop directe sur la console :: 	!UN	REPERTOIRE!ou un fichier
; ===================================================================================

	;SoundBeep, 4050, 10
Loop, parse, A_GuiEvent, `n
{
    SelectedFileName = %A_LoopField%  ; Get the first file only (in case there's more than one).
    compte+=1
	;	break
}

	SplitPath, SelectedFileName, name, dir, ext, name_no_ext, drive
FileGetAttrib, attribs,%SelectedFileName%
	IfInString , attribs, D	; Directory
	TreeRoot = %SelectedFileName%		; car c'est un dossier
	else 
	TreeRoot = %dir%					; car c'est un fichier
	
Tooltip, Loading...,,, 1
AddSubFoldersToTree(TreeRoot)			; (note: pas effacer le treeView)
Tooltip,,,, 1

Return 
; ===================================================================================


; =================================================================================== GuiContextMenu> *MyContextMenu *MyContextRUN (*et prevoir un pour treeview)
GuiContextMenu:  ; Launched in response to a right-click or press of the Apps key.
if A_GuiControl = MyList  ; Display the menu only for clicks inside the ListView.
Menu, MyContextMenu, Show, %A_GuiX%, %A_GuiY%

if A_GuiControl = MyRun  ; Display the menu only for clicks inside the ListView.
Menu, MyContextRUN, Show, %A_GuiX%, %A_GuiY%
    ; return
; Show the menu at the provided coordinates, A_GuiX and A_GuiY.  These should be used
; because they provide correct coordinates even if the user pressed the Apps key:
return
; ===================================================================================




; =================================================================================== ACTION / MyContextMenu
ContextOpenDOSSIER:
ContextOpenFile:  ; The user selected "Open" in the context menu.
ContextProperties:  ; The user selected "Properties" in the context menu.
; For simplicitly, operate upon only the focused row rather than all selected rows:
FocusedRowNumber := LV_GetNext(0, "F")  ; Find the focused row.
if not FocusedRowNumber  ; No row is focused.
    return
; LV_GetText(FileName, FocusedRowNumber, 1) ; Get the text of the first field.
; LV_GetText(FileDir, FocusedRowNumber, 2)  ; Get the text of the second field.
LV_GetText(chemin, FocusedRowNumber, 4)  ; Get the text of the second field.

IfInString A_ThisMenuItem, Open  ; User selected "Open" from the context menu.
    Run %chemin%,, UseErrorLevel
	else
IfInString A_ThisMenuItem, DOSSIER  ; User selected "son DOSSIER" from the context menu.
    {
	SplitPath, chemin, name, dir, ext, name_no_ext, drive
FileGetAttrib, attribs,%chemin%
	IfInString , attribs, D	; Directory
	Run %chemin%,, UseErrorLevel
	Else
	Run %dir%,, UseErrorLevel
	
	}
else  ; User selected "Properties" from the context menu.
    Run Properties "%chemin%",, UseErrorLevel
if ErrorLevel
    MsgBox Could not perform requested action on "%chemin%".
return

ContextClearRows:  ; The user selected "Clear" in the context menu.
RowNumber = 0  ; This causes the first iteration to start the search at the top.
Loop
{
    ; Since deleting a row reduces the RowNumber of all other rows beneath it,
    ; subtract 1 so that the search includes the same row number that was previously
    ; found (in case adjacent rows are selected):
    RowNumber := LV_GetNext(RowNumber - 1)
    if not RowNumber  ; The above returned zero, so there are no more selected rows.
        break
    LV_Delete(RowNumber)  ; Clear the row from the ListView.
}
return
; ===================================================================================






; =================================================================================== afficher_dossier
; le clique sur chekbox pour refaire la liste du dossier automatiquement 
afficher_dossier:
goto MyTree0
Return

; ===================================================================================






; =======================================================================================
; ===================================================================================
;=================================================================== 
UPDATEDSCRIPT:
   FileGetAttrib,attribs,%A_ScriptFullPath%
   IfInString,attribs,A
   {
      FileSetAttrib,-A,%A_ScriptFullPath%
      posX = %A_CaretX%
      posY = %A_CaretY%
      ToolTip,Updated script,%posX%,%posY%
      Sleep,500
      Reload
   }
Return
; SetTimer , UPDATEDSCRIPT,1000 	; superPratique ; http://www.autohotkey.com/forum/topic135.html ; par the best skrommel
;===================================================================
; ===================================================================================
; =======================================================================================


je pense intégrer prochainement un slider pour le reglage largeur > listview/treeview..
et aussi un combobox pour une possibilité de recherche exclusive par extension...
bonne réception, et bonne avancé à tous
soggos
with ahk, all is different!...

TWmailrec
  • Members
  • 9 posts
  • Last active: Jul 26 2014 01:11 AM
  • Joined: 03 Jul 2007
Hi,
Ive been looking for a substitute for Folderselect as in my current program a List box exists & could be used to list files in a folder, - I had two problems:

1. I cannot extract fullpath from selected folder in FileSelectFolder, only the foldername
2. FileselectFolder (Windows function) cannot be resized and moved by the methods I tried!

The amazing ahk script from Zaelia is slighlty more than I needed, AND DOESNT SORT SUB-FOLDERS alphabetically.

Wouldnt a sorted list be more helpful.
I'm only a relative beginner so I have not tried to MOD script.