#include <GUIConstantsEx.au3>
#include <GUIImageList.au3>
#include <GUITreeView.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPIEx.au3>
Opt('MustDeclareVars', 1)
Global $hForm, $hTreeView, $hImageList, $hItem = 0, $hSelect = 0, $hInput, $Input, $Dummy1, $Dummy2
Global $X, $Y, $sPath, $sRoot = "c:\myfile"
$hForm = GUICreate('MyGUI', 600, 600)
$Input = GUICtrlCreateInput('', 20, 20, 560, 19)
$hInput = GUICtrlGetHandle(-1)
GUICtrlSetState(-1, $GUI_DISABLE)
;$hTreeView = _GUICtrlTreeView_Create($hForm, 20, 50, 560, 530, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
;_GUICtrlTreeView_SetHeight($hTreeView, 18)
GUICtrlCreateTreeView(20, 50, 560, 530, -1, $WS_EX_CLIENTEDGE)
$hTreeView = GUICtrlGetHandle(-1)
$Dummy1 = GUICtrlCreateDummy()
$Dummy2 = GUICtrlCreateDummy()
If _WinAPI_GetVersion() >= '6.0' Then
_WinAPI_SetWindowTheme($hTreeView, 'Explorer')
EndIf
$hImageList = _GUIImageList_Create(16, 16, 5, 1)
_GUIImageList_AddIcon($hImageList, @SystemDir & '\shell32.dll', 3)
_GUIImageList_AddIcon($hImageList, @SystemDir & '\shell32.dll', 4)
_GUIImageList_AddIcon($hImageList, @SystemDir & '\shell32.dll', 51)
_GUICtrlTreeView_SetNormalImageList($hTreeView, $hImageList)
$sRoot = StringRegExpReplace(FileGetLongName($sRoot), '\\+\Z', '')
$sPath = StringRegExpReplace($sRoot, '^.*\\', '')
If StringInStr($sPath, ':') Then
$sRoot &= '\'
$sPath &= '\'
EndIf
If _WinAPI_PathIsDirectory($sRoot) Then
$hItem = _GUICtrlTreeView_AddChild($hTreeView, 0, $sPath, 0, 0)
If FileClose(FileFindFirstFile($sRoot & '\*')) Then
_GUICtrlTreeView_AddChild($hTreeView, $hItem, '', 2, 2)
EndIf
EndIf
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
GUISetState()
If $hItem Then
_GUICtrlTreeView_Expand($hTreeView, $hItem)
EndIf
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
Case $Dummy1 ; Update
GUISetCursor(1, 1)
_TVUpdate($hTreeView, GUICtrlRead($Dummy1))
GUISetCursor(2, 0)
Case $Dummy2 ; Menu
$hItem = GUICtrlRead($Dummy2)
$sPath = _TVGetPath($hTreeView, $hItem, $sRoot)
ConsoleWrite('-------------------------------' & @CR)
ConsoleWrite('Handle: ' & $hItem & @CR)
ConsoleWrite('Path: ' & $sPath & @CR)
If _WinAPI_PathIsDirectory($sPath) Then
ConsoleWrite('Type: ' & 'Directory' & @CR)
Else
ConsoleWrite('Type: ' & 'File' & @CR)
EndIf
ConsoleWrite('X: ' & MouseGetPos(0) & @CR)
ConsoleWrite('Y: ' & MouseGetPos(1) & @CR)
ConsoleWrite('-------------------------------' & @CR)
EndSwitch
WEnd
Func _TVGetPath($hTV, $hItem, $sRoot)
Local $Path = StringRegExpReplace(_GUICtrlTreeView_GetTree($hTV, $hItem), '([|]+)|(\\[|])', '\\')
If Not $Path Then
Return ''
EndIf
If Not StringInStr($Path, ':') Then
Return StringRegExpReplace($sRoot, '(\\[^\\]*(\\|)+)\Z', '\\') & $Path
EndIf
Return $Path
EndFunc ;==>_TVGetPath
Func _TVSetPath($hTV, $hItem, $sRoot)
GUICtrlSetData($Input, _WinAPI_PathCompactPath($hInput, _TVGetPath($hTV, $hItem, $sRoot), 554))
$hSelect = $hItem
EndFunc ;==>_TVSetPath
Func _TVUpdate($hTV, $hItem)
Local $hImageList = _SendMessage($hTV, $TVM_GETIMAGELIST)
Local $Path = StringRegExpReplace(_TVGetPath($hTV, $hItem, $sRoot), '\\+\Z', '')
Local $hIcon, $hSearch, $hSubitem
Local $Index, $File
_WinAPI_LockWindowUpdate($hTV)
_GUICtrlTreeView_Delete($hTV, _GUICtrlTreeView_GetFirstChild($hTV, $hItem))
$hSearch = FileFindFirstFile($Path & '\*')
If $hSearch = -1 Then
If Not @error Then
If FileExists($Path) Then
; If _WinAPI_PathIsDirectory($Path) Then
; ; Access denied
; EndIf
Else
_GUICtrlTreeView_Delete($hTV, $hItem)
_WinAPI_LockWindowUpdate(0)
Return 0
EndIf
EndIf
Else
While 1
$File = FileFindNextFile($hSearch)
If @error Then
ExitLoop
EndIf
If @extended Then
$hSubitem = _GUICtrlTreeView_AddChild($hTV, $hItem, $File, 0, 0)
If FileClose(FileFindFirstFile($Path & '\' & $File & '\*')) Then
_GUICtrlTreeView_AddChild($hTV, $hSubitem, '', 2, 2)
EndIf
EndIf
WEnd
FileClose($hSearch)
EndIf
$hSearch = FileFindFirstFile($Path & '\*')
If $hSearch = -1 Then
Else
While 1
$File = FileFindNextFile($hSearch)
If @error Then
ExitLoop
EndIf
If Not @extended Then
$hIcon = _WinAPI_ShellExtractAssociatedIcon($Path & '\' & $File, 1)
$Index = _GUIImageList_ReplaceIcon($hImageList, -1, $hIcon)
_GUICtrlTreeView_AddChild($hTV, $hItem, $File, $Index, $Index)
_WinAPI_DestroyIcon($hIcon)
EndIf
WEnd
FileClose($hSearch)
EndIf
_GUICtrlTreeView_SetItemParam($hTV, $hItem, 0x7FFFFFFF)
_WinAPI_LockWindowUpdate(0)
Return 1
EndFunc ;==>_TVUpdate
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
Local $tNMTREEVIEW = DllStructCreate($tagNMTREEVIEW, $lParam)
Local $hItem = DllStructGetData($tNMTREEVIEW, 'NewhItem')
Local $iState = DllStructGetData($tNMTREEVIEW, 'NewState')
Local $hTV = DllStructGetData($tNMTREEVIEW, 'hWndFrom')
Local $ID = DllStructGetData($tNMTREEVIEW, 'Code')
Local $tTVHTI, $tPoint
Switch $hTV
Case $hTreeView
Switch $ID
Case $TVN_ITEMEXPANDEDW
If Not FileExists(_TVGetPath($hTV, $hItem, $sRoot)) Then
_GUICtrlTreeView_Delete($hTV, $hItem)
If BitAND($iState, $TVIS_SELECTED) Then
_TVSetPath($hTV, _GUICtrlTreeView_GetSelection($hTV), $sRoot)
EndIf
Else
If Not BitAND($iState, $TVIS_EXPANDED) Then
_GUICtrlTreeView_SetSelectedImageIndex($hTV, $hItem, 0)
_GUICtrlTreeView_SetImageIndex($hTV, $hItem, 0)
Else
_GUICtrlTreeView_SetSelectedImageIndex($hTV, $hItem, 1)
_GUICtrlTreeView_SetImageIndex($hTV, $hItem, 1)
If Not _GUICtrlTreeView_GetItemParam($hTV, $hItem) Then
GUICtrlSendToDummy($Dummy1, $hItem)
EndIf
EndIf
EndIf
Case $TVN_SELCHANGEDW
If BitAND($iState, $TVIS_SELECTED) Then
If Not FileExists(_TVGetPath($hTV, $hItem, $sRoot)) Then
_GUICtrlTreeView_Delete($hTV, $hItem)
$hItem = _GUICtrlTreeView_GetSelection($hTV)
EndIf
If $hItem <> $hSelect Then
_TVSetPath($hTV, $hItem, $sRoot)
EndIf
EndIf
Case $NM_RCLICK
$tPoint = _WinAPI_GetMousePos(1, $hTV)
$tTVHTI = _GUICtrlTreeView_HitTestEx($hTV, DllStructGetData($tPoint, 1), DllStructGetData($tPoint, 2))
$hItem = DllStructGetData($tTVHTI, 'Item')
If BitAND(DllStructGetData($tTVHTI, 'Flags'), $TVHT_ONITEM) Then
_GUICtrlTreeView_SelectItem($hTreeView, $hItem)
If Not FileExists(_TVGetPath($hTV, $hItem, $sRoot)) Then
_GUICtrlTreeView_Delete($hTV, $hItem)
$hItem = _GUICtrlTreeView_GetSelection($hTV)
Else
GUICtrlSendToDummy($Dummy2, $hItem)
EndIf
If $hItem <> $hSelect Then
_TVSetPath($hTV, $hItem, $sRoot)
EndIf
EndIf
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY