#include <GUIListBox.au3>
#include <GuiTreeView.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
$Path = @ScriptDir & '\SWInstall.ini'
$aRegFitem = StringRegExp(FileRead($Path), '\[\d+\]', 3)
$aRegSitem = StringRegExp(FileRead($Path), '\[\d+-\d+\]', 3)
Local $aFitem[UBound($aRegFitem)][2], $aSitem[UBound($aRegSitem)][2]
Local $sItemText
Local $z = 1, $f = -1, $s = -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, 290, 380, $iStyle, $WS_EX_CLIENTEDGE)
$List1 = GUICtrlCreateList('', 300, 5, 295, 380)
$But1 = GUICtrlCreateButton("读取被选项目", 100, 400, 100, 30, $WS_GROUP)
$But2 = GUICtrlCreateButton("开始模拟安装", 400, 400, 100, 30, $WS_GROUP)
$var = IniReadSectionNames($Path)
If Not @error Then
For $i = 1 To $var[0]
If StringIsDigit($var[$i]) Then
$f += 1
$sFtext = IniRead($Path, $var[$i], 'ParentName', '')
$aFitem[$f][0] = GUICtrlCreateTreeViewItem($sFtext, $TreeView1)
$aFitem[$f][1] = $sFtext
Else
$s += 1
$sStext = IniRead($Path, $var[$i], 'ChildName', '')
$aSitem[$s][0] = GUICtrlCreateTreeViewItem($sStext, $aFitem[$f][0])
$aSitem[$s][1] = $sStext
EndIf
Next
EndIf
GUISetState()
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case - 3
Exit
Case $aFitem[0][0] To $aFitem[UBound($aFitem) - 1][0] ;父项目
If BitAND(GUICtrlRead($nMsg), $GUI_CHECKED) Then ;如果父项目勾选,其下所有子项目全选。
$iSitemNumber = Number(_GUICtrlTreeView_GetChildCount($TreeView1, $nMsg))
For $i = $nMsg To $nMsg + $iSitemNumber
GUICtrlSetState($i, $GUI_CHECKED)
Next
Else
$iSitemNumber = Number(_GUICtrlTreeView_GetChildCount($TreeView1, $nMsg))
For $i = $nMsg To $nMsg + $iSitemNumber
GUICtrlSetState($i, $GUI_UNCHECKED)
Next
EndIf
ContinueCase
Case $aSitem[0][0] To $aSitem[UBound($aSitem) - 1][0] ;子项目
If BitAND(GUICtrlRead($nMsg), $GUI_CHECKED) Then ;如果子项目勾选,其父项目也跟着勾选。
$hFitemHandle = _GUICtrlTreeView_GetParentParam($TreeView1, $nMsg)
GUICtrlSetState($hFitemHandle, $GUI_CHECKED)
Else ;父项目下的所有子项目全部取消勾选,父项目也跟着取消勾选。
$hFitemHandle = _GUICtrlTreeView_GetParentParam($TreeView1, $nMsg)
$iSitemNumber = Number(_GUICtrlTreeView_GetChildCount($TreeView1, $hFitemHandle))
For $i = $hFitemHandle To $hFitemHandle + $iSitemNumber
If GUICtrlRead($i) = $GUI_CHECKED Then ExitLoop
If $i = $hFitemHandle + $iSitemNumber Then GUICtrlSetState($hFitemHandle, $GUI_UNCHECKED)
Next
EndIf
$x = 1
$sItemText = ''
GUICtrlSetData($List1, '') ;清空List内所有数据
For $i = 0 To UBound($aSitem) - 1
If _GUICtrlTreeView_GetChecked($TreeView1, $aSitem[$i][0]) Then
GUICtrlSetData($List1, StringFormat("[%02d]", $x) & $aSitem[$i][1]) ;如果项目被勾选则写入List
$sItemText &= $aSitem[$i][1] & '|'
$x += 1
EndIf
Next
Case $But1
$sText = ''
For $i = 0 To UBound($aSitem) - 1
If _GUICtrlTreeView_GetChecked($TreeView1, $aSitem[$i][0]) Then
$sText &= '被选项目句柄:' & $aSitem[$i][0] & ' ' & '项目文本:' & $aSitem[$i][1] & @CRLF
EndIf
Next
If $sText <> '' Then MsgBox(0, '获取被选项目句柄+项目文本', $sText)
Case $But2
begin()
EndSwitch
WEnd
Func begin()
$aLines = StringSplit($sItemText, '|', 2)
For $i = 0 To UBound($aLines) - 1
_GUICtrlListBox_ReplaceString($List1, $i, '[→]' & $aLines[$i])
_GUICtrlListBox_SetCurSel($List1, $i)
Sleep(800)
_GUICtrlListBox_ReplaceString($List1, $i, '[OK]' & $aLines[$i])
_GUICtrlListBox_SetCurSel($List1, $i)
Next
MsgBox(0, '', '安装完成!')
EndFunc ;==>begin