#include <Array.au3>
#include <GuiTreeView.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
$Path = @ScriptDir & '\SWInstall.ini'
Local $aFitem[1], $aSitem[1], $z = 1
$iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
GUICreate("TreeView项目勾选操作", 600, 450)
$TreeView1 = GUICtrlCreateTreeView(5, 5, 590, 440, $iStyle, $WS_EX_CLIENTEDGE)
$var = IniReadSectionNames($Path)
If Not @error Then
For $i = 1 To $var[0]
If StringIsDigit($var[$i]) Then
$hFitem = GUICtrlCreateTreeViewItem(IniRead($Path, $var[$i], 'ParentName', ''), $TreeView1)
_ArrayAdd($aFitem, $hFitem)
Else
$hSitem = GUICtrlCreateTreeViewItem(IniRead($Path, $var[$i], 'ChildName', ''), $hFitem)
_ArrayAdd($aSitem, $hSitem)
GUICtrlSetState($hFitem, $GUI_EXPAND)
EndIf
Next
EndIf
GUISetState()
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case - 3
Exit
Case $aFitem[1] To $aFitem[UBound($aFitem) - 1], $aSitem[1] To $aSitem[UBound($aSitem) - 1] ;父项目操作
For $i = 0 To UBound($aFitem) - 1
If $nMsg = $aFitem[$i] And BitAND(GUICtrlRead($aFitem[$i]), $GUI_CHECKED) Then
;MsgBox(0, '你勾选了:' & $aFitem[$i] - 3 & '号项目', '该项目下有 ' & _GUICtrlTreeView_GetChildCount($TreeView1, $aFitem[$i]) & ' 个子项目')
$Number = Number(_GUICtrlTreeView_GetChildCount($TreeView1, $aFitem[$i])) ;全选
For $m = $aFitem[$i] To $aFitem[$i] + $Number
GUICtrlSetState($m, $GUI_CHECKED)
Next
ElseIf $nMsg = $aFitem[$i] And BitAND(GUICtrlRead($aFitem[$i]), $GUI_UNCHECKED) Then ;反选
$Number = Number(_GUICtrlTreeView_GetChildCount($TreeView1, $aFitem[$i]))
For $n = $aFitem[$i] To $aFitem[$i] + $Number
GUICtrlSetState($n, $GUI_UNCHECKED)
Next
EndIf
Next
For $z = 0 To UBound($aSitem) - 1 ;子项目操作
If $nMsg = $aSitem[$z] And BitAND(GUICtrlRead($aSitem[$z]), $GUI_CHECKED) Then ;子项目勾选,父项目跟着勾选
;MsgBox(0, 0, '你勾选了:' & $aSitem[$z] - 3 & '号项目')
$Handle = _GUICtrlTreeView_GetParentParam($TreeView1, $aSitem[$z])
GUICtrlSetState($Handle, $GUI_CHECKED)
ElseIf $nMsg = $aSitem[$z] And BitAND(GUICtrlRead($aSitem[$z]), $GUI_UNCHECKED) Then
$Handle = _GUICtrlTreeView_GetParentParam($TreeView1, $aSitem[$z])
$Number = Number(_GUICtrlTreeView_GetChildCount($TreeView1, $Handle))
For $o = $Handle To $Handle + $Number
If GUICtrlRead($o) = $GUI_CHECKED Then ExitLoop
If $o = $Handle + $Number Then GUICtrlSetState($Handle, $GUI_UNCHECKED) ;子项目全部取消勾选,父项目也跟着取消勾选
Next
EndIf
Next
EndSwitch
WEnd