#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <file.au3>
#include <array.au3>
Global $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
;~ Global $item1 = ''
$GUI = GUICreate("(UDF Created) TreeView Create", 400, 640)
$hTreeView = _GUICtrlTreeView_Create($GUI, 2, 2, 396, 568,$iStyle, $WS_EX_CLIENTEDGE)
$hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, "shell32.dll", 4)
_GUICtrlTreeView_SetNormalImageList($hTreeView, $hImage)
_GUICtrlTreeView_BeginUpdate($hTreeView)
Recursion(@AppDataCommonDir,'')
_GUICtrlTreeView_EndUpdate($hTreeView)
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While 1
$msg = GUIGetMsg()
If $msg = -3 Then ExitLoop
;~ If $msg = _GUICtrlTreeView_GetChecked($hTreeView,$item1) Then MsgBox(0,1,1)
WEnd
;~ _GUICtrlTreeView_SetChecked($hTreeView,$item1)
;~ Do
;~ Until GUIGetMsg() = -3
Func Recursion($current,$flag)
If $flag = '' Then $item1 = _GUICtrlTreeView_Add($hTreeView,0,$current,$hImage,$hImage)
Local $search = FileFindFirstFile($current & "\*.*")
While 1
Dim $file = FileFindNextFile($search)
If @error Or StringLen($file) < 1 Then ExitLoop
If StringInStr(FileGetAttrib($current & "" & $file), "D") And ($file <> "." Or $file <> "..") Then
If $flag <> '' Then $item1 = $flag
$item2 = _GUICtrlTreeView_AddChild($hTreeView,$item1,$file);,$hImage,$hImage)
Recursion($current & "" & $file,$item2)
Else
If $flag <> '' Then $item1 = $flag
$item3 = _GUICtrlTreeView_AddChild($hTreeView,$item1,$file,$hImage,$hImage)
EndIf
WEnd
Return
EndFunc ;==>Search
Func MY_WM($hWnd, $Msg, $wParam, $lParam)
Local $tNMHdr = DllStructCreate($tagNMHDR, $lParam), $tNM_TREEVIEW
Local $hWndFrom = DllStructGetData($tNMHdr, 'hWndFrom')
Local $iIDFrom = DllStructGetData($tNMHdr, 'IDFrom')
Local $iCode = DllStructGetData($tNMHdr, 'Code')
If $iIDFrom = $hTreeView Then
Switch $iCode
Case $TVN_SELCHANGEDA, $TVN_SELCHANGEDW
;~ If GUICtrlRead($hTreeView) > 0 Then _
MsgBox(0, 0, 'ID: ' & GUICtrlRead($hTreeView) & @CRLF & 'Text: ' & GUICtrlRead($hTreeView, 1))
EndSwitch
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>_OnNotify
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
$hWndTreeview = $hTreeView
If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeview
Switch $iCode
Case $NM_CLICK ; 控件失去输入焦点
;~ MsgBox(0,1, 1);_GUICtrlTreeView_GetChecked($hTreeView,$item1))
;~ memowrite("$NM_CLICK" & @LF)
;~ memowrite("-->hWndFrom:" & @TAB & $hWndFrom & @LF)
;~ memowrite("-->IDFrom:" & @TAB & $iIDFrom & @LF)
;~ memowrite("-->Code:" & @TAB & $iCode)
; 无返回值
;~ Return 1 ; 非0为不允许默认操作
Return 0 ; 0 为允许默认操作
Case $NM_DBLCLK ; 用户在控件内双击鼠标左键
;~ memowrite("$NM_DBLCLK" & @LF)
;~ memowrite("-->hWndFrom:" & @TAB & $hWndFrom & @LF)
;~ memowrite("-->IDFrom:" & @TAB & $iIDFrom & @LF)
;~ memowrite("-->Code:" & @TAB & $iCode)
;~ Return 1 ; 非0为不允许默认操作
Return 0 ; 0 为允许默认操作
Case $NM_RCLICK ; 用户在控件内点击鼠标右键
;~ memowrite("$NM_RCLICK" & @LF)
;~ memowrite("-->hWndFrom:" & @TAB & $hWndFrom & @LF)
;~ memowrite("-->IDFrom:" & @TAB & $iIDFrom & @LF)
;~ memowrite("-->Code:" & @TAB & $iCode)
;~ Return 1 ; 非0为不允许默认操作
Return 0 ; 0 为允许默认操作
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func memowrite($s_text)
;~ GUICtrlSetData($edit, $s_text & @CRLF, 1)
MsgBox(0,123,$s_text & @CRLF ,5)
EndFunc ;==>memowrite