GUIウィンドウにツリービューコントロールを追加する。項目の操作を行う組み込み関数も用意されている。
Gui, Add, TreeView [, Options, Text]
| 引数名 | 説明 |
|---|---|
| Options |
オプションを半角スペース区切りで列挙。 共通のオプションについては、Gui,Addの項参照。 固有のオプションは後述。 「V」オプションで変数を関連付けても、変数に情報が格納されることはない。ただし、GuiControlなどのコマンドでコントロールを指定するために利用できる。 Gでラベル名を指定すると、各種の操作が行われるごとにサブルーチンが実行される。詳しくはイベントの節を参照。 |
| Text | 不使用。 |
ツリービューを操作するために、多数の機能が関数として用意されている。
TV系の関数が操作対象とするツリービューは、デフォルトGUIウィンドウ上のカレントツリービューである。
カレントツリービューは、通常は最後に項目を追加したツリービューだが、「Gui,TreeView,TVName」のようにして変更できる。
GUIウィンドウが存在しなかったり、デフォルトGUIウィンドウ上にツリービューコントロールが存在しなかった場合、TV系関数は「0」を返す。
「G」オプションでラベル名を指定していると、下記のイベントが発生するたびにサブルーチンが実行される。
このとき、A_GuiEvent変数にイベント名が格納される。
「E」と「e」など大文字と小文字と小文字の違うイベント名は別物である。
なお、イベントは今後も追加される可能性がある。
AltSubmitオプションを指定すると、上記のイベントに加え、下記のイベント時にもサブルーチンが呼び出される。
内部に格納されているテキストの長さに関わらず、最初の260文字までしか表示されない。
保持できる項目の最大数は65536である。
ツリービューコントロールはEnterキーが押されたことを知ることができない。
Enterキーが押されたときに特定の動作をさせたいときは、下記の例のようにウィンドウにデフォルトボタンを設定する。
デフォルトボタンは非表示にすることも可能である。
Gui,Add,TreeView,gTVEvent
Gui,Add,Button,gOnEnter Hidden Default
;その他の初期化動作
return
OnEnter:
GuiControlGet, FocusedControl, Focus
if FocusedControl = SysTreeView321
Selected(TV_GetSelection())
return
LVEvent:
if A_GuiEvent = DoubleClick
Selected(A_EventInfo)
return
Selected(item)
{
;項目決定時の動作
}
ListView Gui, GuiContextMenu, GuiControl, GuiControlGet, ウィンドウスタイル
; 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 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%
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(10)
Loop 10 ; 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, 90) ; 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%
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"))
}
MyTree:
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:
TV_GetText(SelectedItemText, A_EventInfo)
ParentID := A_EventInfo
Loop ; Build the full path to the selected folder.
{
ParentID := TV_GetParent(ParentID) ; Parent itself can be a parent of another child.
if not ParentID ; No more ancestors.
break
TV_GetText(ParentText, ParentID)
SelectedItemText = %ParentText%\%SelectedItemText%
}
SelectedFullPath = %TreeRoot%\%SelectedItemText%
; Put the files and folders 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:
SetFormat, float, 0.2 ; Show total file size with only 2 decimal places vs. 6.
SB_SetText(FileCount . " files", 1)
SB_SetText((TotalSize / 1024) . " 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