#include <GuiConstantsEx.au3>
#include <TreeviewConstants.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
Local $TreeItem[1]
$GUI = GUICreate("树视图(TreeView) UDF 函数演示", 400, 300)
$hTreeView = GUICtrlCreateTreeView(2, 2, 396, 268, $TVS_CHECKBOXES+$TVS_HASLINES, $TVS_NOSCROLL)
GUISetState()
$hItem = GUICtrlCreateTreeViewItem("主项", $hTreeView)
_ArrayAdd($TreeItem, $hItem)
For $y = 1 To 10
$Item = GUICtrlCreateTreeViewItem("子项" & $y, $hItem)
_ArrayAdd($TreeItem, $Item)
Next
$TreeItem[0] = UBound($TreeItem)-1
GUICtrlSetState($hItem, $GUI_EXPAND+$GUI_DEFBUTTON) ;展开父项
$TreePos = ControlGetPos($GUI, "", $hTreeView) ;取TREE控件坐标
; 循环到用户退出
While 1
$nMsg = GUIGetMsg(1)
Switch $nMsg[0]
Case $TreeItem[1] To $TreeItem[$TreeItem[0]]
$x = _GUICtrlTreeView_DisplayRect($hTreeView, $nMsg[0], True) ;取点击项文本框的坐标
If $nMsg[3]-$TreePos[0] >= $x[0] Then ;如果点击的坐标小于文本框X坐标,说明点击在CHECKBOX上,反之点击在文本上
If BitAnd(GUICtrlRead($nMsg[0]), $GUI_CHECKED) Then
GUICtrlSetState($nMsg[0], $GUI_UNCHECKED)
Else
GUICtrlSetState($nMsg[0], $GUI_CHECKED)
EndIf
EndIf
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
|