MyTetra Share
Делитесь знаниями!
Пример TreeView дерево файлов
Время создания: 14.06.2017 11:29
Текстовые метки: read
Раздел: AutoHotkey - gui - TreeView
Запись: xintrea/mytetra_db_mcold/master/base/1497428989s84bw6jfzs/text.html на

;Opens a file explorer containing files in a_scriptdir

;Using just me's code:


#SingleInstance force

; Set the root folder for the TreeView. Loading may take a long time if an entire drive such as C:\ is specified:

TreeRoot = %A_ScriptDir%

TreeViewWidth := 280

ListViewWidth := A_ScreenWidth - TreeViewWidth - 450

Gui +Resize

; Create an ImageList and put some standard system icons into it:

ImageListID := IL_Create(5)

Loop 5

IL_Add(ImageListID, "shell32.dll", A_Index)

; Create a TreeView and a ListView side-by-side :

Gui, Add, TreeView, vMyTreeView r20 w%TreeViewWidth% gMyTreeView ImageList%ImageListID%

Gui, Add, ListView, vMyListView r20 w%ListViewWidth% x+10 gMyListView AltSubmit, Name|Modified

; Set the ListView's column widths:

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:

Gui, Add, StatusBar

SB_SetParts(60, 85) ; Create three parts in the bar (the third part fills the remaining width).

; Add folders and their subfolders to the tree. Display the status in case loading takes a long time:

SplashTextOn, 200, 25, Please wait, Loading the tree...

TreeRoot := RTrim(TreeRoot, "\") ; Remove trailing backslashes, if any.

SplitPath, TreeRoot, TreeRootName, TreeRootDir ; Split TreeRoot into TreeRootName and TreeRootDir.

If (TreeRootName) ; If TreeRootName is not empty

TreeRootID := TV_Add(TreeRootName, 0, "Expand Icon4") ; add it to the TreeView

Else ; Else

TreeRootID := 0 ; set TreeRootId to zero.

AddSubFoldersToTree(TreeRoot, TreeRootID) ; Call AddSubFolders() passing TreeRoot and TreeRootID

TreeRoot := TreeRootDir ; Set TreeRoot to TreeRootDir.


Gui, Show, h550, %TreeRoot%


; This function adds to the TreeView all subfolders in the specified folder.

AddSubFoldersToTree(Folder, ParentItemID = 0)


; 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"))



; What to do when a folder is clicked in treeview


if A_GuiEvent <> S ; if 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:

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.


TV_GetText(ParentText, ParentID)

SelectedItemText = %ParentText%\%SelectedItemText%


SelectedFullPath = %TreeRoot%\%SelectedItemText%


; Put the files into the ListView:

LV_Delete() ; Clear all rows.

GuiControl, -Redraw, MyListView ; Improve performance by disabling redrawing during load.

FileCount = 0

TotalSize = 0

Loop %SelectedFullPath%\*.* ; For simplicity, this omits folders so that only files are shown in the ListView.


;LV_Add("", A_LoopFileName, A_LoopFileTimeModified)

LV_Add("", A_LoopFileName, A_LoopFileSize . " KB")

FileCount += 1

TotalSize += A_LoopFileSize


GuiControl, +Redraw, MyListView

; Populate 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)



; What to do when a file is double/right-clicked in listview


if (A_GuiEvent = "DoubleClick")


LV_GetText(RowText, A_EventInfo) ; Get the text from the row's first field and store it in the var RowText.

run %SelectedFullPath%\%RowText%

;exitapp ;exit the script after selection is made


else if (A_GuiEvent = "RightClick")


LV_GetText(RowText, A_EventInfo) ; Get the text from the row's first field and store it in the var RowText.

run C:\PortableApps\Notepad++\notepad++.exe "%SelectedFullPath%\%RowText%"

;exitapp ;exit the script after selection is made




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.


; Otherwise, the window has been resized or maximized. Resize the controls to match.

GuiControl, Move, MyTreeView, % "H" . (A_GuiHeight - 30) ; -30 for StatusBar and margins.

GuiControl, Move, MyListView, % "H" . (A_GuiHeight - 30) . " W" . (A_GuiWidth - TreeViewWidth - 30)


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


Так же в этом разделе:
MyTetra Share v.0.58
Яндекс индекс цитирования