#include <GUIListBox.au3>
#include <GuiTreeView.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.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 = GUICtrlCreateListView("", 300, 5, 295, 380)
_GUICtrlListView_AddColumn($List1, "序号", 50)
_GUICtrlListView_AddColumn($List1, "信息", 230)
$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)
GUICtrlSetState(-1, $GUI_CHECKED)
$aFitem[$f][1] = $sFtext
Else
$s += 1
$sStext = IniRead($Path, $var[$i], 'ChildName', '')
$aSitem[$s][0] = GUICtrlCreateTreeViewItem($sStext, $aFitem[$f][0])
GUICtrlSetState(-1, $GUI_CHECKED)
$aSitem[$s][1] = $sStext
EndIf
Next
EndIf
AddItem()
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
AddItem()
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 AddItem()
$x = 1
$sItemText = ''
_GUICtrlListView_DeleteAllItems($List1) ;清空List内所有数据
For $i = 0 To UBound($aSitem) - 1
If _GUICtrlTreeView_GetChecked($TreeView1, $aSitem[$i][0]) Then
GUICtrlCreateListViewItem(StringFormat("[%02d]", _GUICtrlListView_GetItemCount($List1)+1) & "|" & $aSitem[$i][1], $List1) ;如果项目被勾选则写入List
$sItemText &= $aSitem[$i][1] & '|'
$x += 1
EndIf
Next
EndFunc
Func begin()
$aLines = StringSplit($sItemText, '|', 2)
GUICtrlSetState ($List1, $GUI_FOCUS) ;此处要将焦点设置在 $List1 ,否则被选项的背景是灰的,不是蓝色
For $i = 0 To UBound($aLines) - 1
_GUICtrlListView_SetItemText($List1, $i, '[→]')
_GUICtrlListView_SetItemSelected($List1, $i)
Sleep(800)
_GUICtrlListView_SetItemText($List1, $i, '[OK]')
_GUICtrlListView_SetItemSelected($List1, $i, False)
Next
MsgBox(0, '', '安装完成!')
EndFunc ;==>begin