#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListBox.au3>
#include <GuiTreeView.au3>
#Include <Array.au3>
Global $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 428, 320, -1, -1)
$TvStrat = GUICtrlCreateTreeView(8, 8, 185, 279, $iStyle, $WS_EX_CLIENTEDGE)
Dim $Father[1], $Son[1],$Number
Dim $FatherArray[6] = [4, "程序列表类", "Windows Update 链接", "用户文件夹类", "系统应用类"]
Dim $SonArray[5][10] = [[4, "附加的程序列表", "常用程序列表", "更多程序列表", "公用程序列表"],[1, "Windows Update 链接"], _
[7, "我的文档", "图片收藏", "我的音乐", "最近使用的文档", "我的电脑", "网上邻居", "用户名"],[3, "控制面板", _
"网络连接", "打印机传真"]]
For $i = 1 To 4
$hFatherItem = GUICtrlCreateTreeViewItem($FatherArray[$i], $TvStrat)
_ArrayAdd($Father, $hFatherItem)
For $j = 1 To $SonArray[$i - 1][0]
$hSonItem = GUICtrlCreateTreeViewItem($SonArray[$i-1][$j], $hFatherItem)
_ArrayAdd($Son, $hSonItem)
Next
Next
$LsStrat = GUICtrlCreateList("", 224, 8, 193, 279)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
GUIDelete($Form1)
Exit
Case $Father[1] To $Father[UBound($Father) - 1], $Son[1] To $Son[UBound($Son) - 1]
For $i = 0 To UBound($Father) - 1 ;父项目操作
If $nMsg = $Father[$i] And BitAND(GUICtrlRead($Father[$i]), $GUI_CHECKED) Then
$Number = Number(_GUICtrlTreeView_GetChildCount($TvStrat, $Father[$i])) ;全选
For $m = $Father[$i] To $Father[$i] + $Number
GUICtrlSetState($m, $GUI_CHECKED)
Next
ElseIf $nMsg = $Father[$i] And BitAND(GUICtrlRead($Father[$i]), $GUI_UNCHECKED) Then ;反选
$Number = Number(_GUICtrlTreeView_GetChildCount($TvStrat, $Father[$i]))
For $n = $Father[$i] To $Father[$i] + $Number
GUICtrlSetState($n, $GUI_UNCHECKED)
Next
EndIf
Next
For $z = 0 To UBound($Son) - 1 ;子项目操作
If $nMsg = $Son[$z] And BitAND(GUICtrlRead($Son[$z]), $GUI_CHECKED) Then ;子项目勾选,父项目跟着勾选
$Handle = _GUICtrlTreeView_GetParentParam($TvStrat, $Son[$z])
GUICtrlSetState($Handle, $GUI_CHECKED)
ElseIf $nMsg = $Son[$z] And BitAND(GUICtrlRead($Son[$z]), $GUI_UNCHECKED) Then
$Handle = _GUICtrlTreeView_GetParentParam($TvStrat, $Son[$z])
$Number = Number(_GUICtrlTreeView_GetChildCount($TvStrat, $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
_TreeViewToListBox($TvStrat, $LsStrat)
EndSwitch
WEnd
Func _TreeViewToListBox($TreeView, $ListBox)
;说明
;能把TreeView中复选框选中的项目显示到指定的ListBox中
;参数
;$TreeView -----TreeView句柄
; $ListBox -----ListBox句柄
Local $TVItemString = "" ;获取TreeView中选择的Text
Local $LsIndex ;ListBox的项目数量
Local $TVSubItemString = _GUICtrlTreeView_GetFirstItem($TreeView) ;获取TreeView中第一个项目的句柄
Local $TreeViewIndexs = _GUICtrlTreeView_GetCount($TreeView) ;获取TreeView中项目的数量
For $i = 0 To $TreeViewIndexs - 1 ;循环项目数量
If _GUICtrlTreeView_GetChecked($TreeView, $TVSubItemString) = True Then ;如果TreeView该项目的复选框被选择
$TVItemString = _GUICtrlTreeView_GetText($TreeView, $TVSubItemString) ;获取TreeView被选择的项目的文本
If _GUICtrlListBox_FindInText($ListBox, $TVItemString) = -1 Then
_GUICtrlListBox_AddString($ListBox, $TVItemString)
EndIf
Else ;如果该项目的复选框不被选择
$TVItemString = _GUICtrlTreeView_GetText($TreeView, $TVSubItemString) ;获取TreeView该项目的文本
If _GUICtrlListBox_FindInText($ListBox, $TVItemString) <> -1 Then
_GUICtrlListBox_DeleteString($ListBox, _GUICtrlListBox_FindInText($ListBox, $TVItemString)) ;删除掉存在的字符串
EndIf
EndIf
$TVSubItemString = _GUICtrlTreeView_GetNext($TreeView, $TVSubItemString) ;吧TreeView句柄移至下一个
Next
EndFunc ;==>_TreeViewToListBox
如何才能让父项目勾选时不加进ListBox?