Code : Tout sélectionner
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
Global $idx = 0
Global $level = 0
Dim $TreeViewItems[$idx + 1]
global $hTreeView
$Form1 = GUICreate("Form1", 314, 332, 192, 114)
$TreeView1 = GUICtrlCreateTreeView(8, 8, 297, 281, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_CHECKBOXES), $WS_EX_CLIENTEDGE)
$hTreeView = GUICtrlGetHandle($TreeView1) ; on prend le handle du treeview
GUISetState(@SW_SHOW)
$button1=GUICtrlCreateButton("list",10,291,75,20)
For $i = 1 To 2
_FilltreeView($TreeView1)
Next
_GUICtrlTreeView_Sort($hTreeView) ; on trie le tableau (pour le pb de la petite croix)
GUIRegisterMsg($WM_NOTIFY, "_Check") ; on appelle la fonction qui gere les check boxes
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
case $button1
DisplaySelected()
EndSwitch
WEnd
Func DisplaySelected()
Local $selectedlist
For $i = 1 To $idx
If _IsChecked($TreeViewItems[$i]) Then
$selectedlist = $selectedlist & GUICtrlRead($TreeViewItems[$i], 1) & @CRLF
EndIf
Next
MsgBox(0, '', $selectedlist)
EndFunc ;==>DisplaySelected
Func _FilltreeView($parent)
$level = $level + 1
$idx = $idx + 1
ReDim $TreeViewItems[$idx + 1]
$TreeViewItems[$idx] = GUICtrlCreateTreeViewItem($level & " Item " & $idx, $parent)
If $level < 3 Then
_FilltreeView($parent)
_FilltreeView($TreeViewItems[$idx])
EndIf
$level = $level - 1
EndFunc ;==>_FilltreeView
Func _IsChecked($item)
If BitAND(GUICtrlRead($item), $GUI_CHECKED) = $GUI_CHECKED Then
Return True
Else
Return False
EndIf
EndFunc ;==>_IsChecked
; ici les 3 fonctions qui gèrent les checkboxes
; _check qui identifie la case en cours, et qui apelle _checkchild pour la descendance, et _checkparents pour les ascendants
Func _Check($hWnd, $Msg, $wParam, $lParam)
Local $hWndFrom, $idFrom, $iCode, $tNMHDR = DllStructCreate("hwnd hwndFrom;int idFrom;int code", $lParam)
$hWndFrom = DllStructGetData($tNMHDR, "hwndFrom")
$iCode = DllStructGetData($tNMHDR, "code")
If $hWndFrom = $hTreeView Then
Switch $iCode
Case $NM_CLICK
Local $tMPos = _WinAPI_GetMousePos(True, $hWndFrom), $tHit = _GUICtrlTreeView_HitTestEx($hWndFrom, DllStructGetData($tMPos, 1), DllStructGetData($tMPos, 2))
Local $hItem = DllStructGetData($tHit, "Item"), $iFlags = DllStructGetData($tHit, "Flags")
If $hItem <> 0 And BitAND($iFlags, $TVHT_ONITEMSTATEICON) Then
_CheckChildren($hWndFrom, $hItem, 1)
_CheckParents($hWndFrom, $hItem)
EndIf
Case $TVN_KEYDOWN
Local $tNMTVKEY = DllStructCreate("hwnd;int;int;short key;uint", $lParam)
Local $hSelected = _GUICtrlTreeView_GetSelection($hWndFrom)
If DllStructGetData($tNMTVKEY, "key") = 0x20 And $hSelected Then ;;space
_CheckChildren($hWndFrom, $hSelected, 1)
_CheckParents($hWndFrom, $hSelected)
EndIf
Case Else
EndSwitch
EndIf
EndFunc ;==>_Check
Func _CheckChildren($hWnd, $hItem, $fTop = 0)
$Update = True
Local $fChecked = _GUICtrlTreeView_GetChecked($hWnd, $hItem)
If $fTop Then $fChecked = Not $fChecked
If _GUICtrlTreeView_GetChildren($hWnd, $hItem) Then
Local $hChild = _GUICtrlTreeView_GetFirstChild($hWnd, $hItem)
Do
_GUICtrlTreeView_SetChecked($hWnd, $hChild, $fChecked)
If _GUICtrlTreeView_GetChildren($hWnd, $hChild) Then
_CheckChildren($hWnd, $hChild)
EndIf
$hChild = _GUICtrlTreeView_GetNextChild($hWnd, $hChild)
Until $hChild = 0
EndIf
EndFunc ;==>_CheckChildren
Func _CheckParents($hWnd, $hItem, $fTop = False)
$Update = True
Local $fChecked = False
If $fTop = False Then $fChecked = _GUICtrlTreeView_GetChecked($hWnd, $hItem)
If $fChecked = False Then
Local $hParent = _GUICtrlTreeView_GetParentHandle($hWnd, $hItem)
If $hParent <> GUICtrlGetHandle($hTreeView) Then
_GUICtrlTreeView_SetChecked($hWnd, $hParent, True)
_CheckParents($hWnd, $hParent, True)
EndIf
EndIf
EndFunc ;==>_CheckParents