1) - : node, ?
, :
procedure TForm5.SetTreeViewCheckState(Node: TTreeNode; StateIndex: Integer; Recursive: Boolean);
begin
Node.StateIndex := StateIndex;
if not Recursive then Exit;
for I := 0 to Node.Count-1 do
SetTreeViewCheckState(Node.Item[I], StateIndex, True);
end;
procedure TForm5.ToggleTreeViewCheckBoxes(Node: TTreeNode);
var
I: Integer;
begin
if Assigned(Node) then
begin
if Node.StateIndex = cStateUnCheck then
SetTreeViewCheckState(Node, cStateChecked, False);
else if Node.StateIndex = cStateChecked then
SetTreeViewCheckState(Node, cStateUnCheck, True);
end;
end;
2) - StateIndex ? ChildNode.StateIndex: = 1;
, . :
procedure TForm5.FormCreate(Sender: TObject);
var
RootNode: TTreeNode;
ParentNode: TTreeNode;
i: Integer;
begin
for i := Low(aRootList) to High(aRootList) do
begin
RootNode := TreeView1.Items.Add(nil, aRootList[i]);
ParentNode := AddChildNodeWithState(RootNode, 'Parent 1');
AddChildNodeWithState(ParentNode, 'Child 1');
AddChildNodeWithState(ParentNode, 'Child 2');
ParentNode := AddChildNodeWithState(RootNode, 'Parent 2');
AddChildNodeWithState(ParentNode, 'Child 1');
AddChildNodeWithState(ParentNode, 'Child 2');
end;
end;
function TForm5.AddChildNodeWithState(AParentNode: TTreeNode, const ACaption: String; AStateIndex: Integer = 1): TTreeNode;
begin
Result := TreeView1.Items.AddChild(AParentNode, ACaption);
Result.StateIndex := AStateIndex;
end;
( ):
type
TTreeNodeHelper = class helper for TTreeNode
public
function AddChildWithState(const ACaption: string; AStateIndex: Integer = 1): TTreeNode;
procedure SetCheckState(StateIndex: Integer; Recursive: Boolean);
procedure ToggleCheckState;
end;
function TTreeNodeHelper.AddChildWithState(const ACaption: string; AStateIndex: Integer = 1): TTreeNode;
begin
Result := Self.TreeView.Items.AddChild(Self, ACaption);
Result.StateIndex := AStateIndex;
end;
procedure TTreeNodeHelper.SetCheckState(StateIndex: Integer; Recursive: Boolean);
begin
Self.StateIndex := StateIndex;
if not Recursive then Exit;
for I := 0 to Self.Count-1 do
Self.Item[I].SetCheckState(StateIndex, True);
end;
procedure TTreeNodeHelper.ToggleCheckState;
var
I: Integer;
begin
if Self.StateIndex = cStateUnCheck then
SetCheckState(cStateChecked, False);
else if Self.StateIndex = cStateChecked then
SetCheckState(cStateUnCheck, True);
end;
end;
procedure TForm5.FormCreate(Sender: TObject);
var
RootNode: TTreeNode;
ParentNode: TTreeNode;
i: Integer;
begin
for i := Low(aRootList) to High(aRootList) do
begin
RootNode := TreeView1.Items.Add(nil, aRootList[i]);
ParentNode := RootNode.AddChildWithState('Parent 1');
ParentNode.AddChildWithState('Child 1');
ParentNode.AddChildWithState('Child 2');
ParentNode := RootNode.AddChildWithState('Parent 2');
ParentNode.AddChildWithState('Child 1');
ParentNode.AddChildWithState('Child 2');
end;
end;
procedure TForm5.TreeView1Click(Sender: TObject);
var
P: TPoint;
begin
GetCursorPos(P);
P := TreeView1.ScreenToClient(P);
if (htOnStateIcon in TreeView1.GetHitTestInfoAt(P.X, P.Y)) then
TreeView1.GetNodeAt(P.X, P.Y).ToggleCheckState;
end;
Delphi, , TTreeNode TreeView OnCreateNodeClass, :
type
TMyTreeNode = class(TTreeNode)
public
function AddChildWithState(const ACaption: string; AStateIndex: Integer = 1): TTreeNode;
procedure SetCheckState(StateIndex: Integer; Recursive: Boolean);
procedure ToggleCheckState;
end;
function TMyTreeNode.AddChildWithState(const ACaption: string; AStateIndex: Integer = 1): TTreeNode;
begin
Result := Self.TreeView.Items.AddChild(Self, ACaption);
Result.StateIndex := AStateIndex;
end;
procedure TMyTreeNode.SetCheckState(StateIndex: Integer; Recursive: Boolean);
begin
Self.StateIndex := StateIndex;
if not Recursive then Exit;
for I := 0 to Self.Count-1 do
TMyTreeNode(Self.Item[I]).SetCheckState(StateIndex, True);
end;
procedure TMyTreeNode.ToggleCheckBoxes;
var
I: Integer;
begin
if Self.StateIndex = cStateUnCheck then
SetCheckBoxes(cStateChecked, cStateUnChecked);
else if Self.StateIndex = cStateChecked then
SetCheckBoxes(cStateUnCheck, cStateUnChecked);
end;
end;
procedure TForm5.FormCreate(Sender: TObject);
var
RootNode: TTreeNode;
ParentNode: TTreeNode;
i: Integer;
begin
for i := Low(aRootList) to High(aRootList) do
begin
RootNode := TreeView1.Items.Add(nil, aRootList[i]);
ParentNode := TMyTreeNode(RootNode).AddChildWithState('Parent 1');
TMyTreeNode(ParentNode).AddChildWithState('Child 1');
TMyTreeNode(ParentNode).AddChildWithState('Child 2');
ParentNode := TMyTreeNode(RootNode).AddChildWithState('Parent 2');
TMyTreeNode(ParentNode).AddChildWithState('Child 1');
TMyTreeNode(ParentNode).AddChildWithState('Child 2');
end;
end;
procedure TForm5.TreeView1Click(Sender: TObject);
var
P: TPoint;
begin
GetCursorPos(P);
P := TreeView1.ScreenToClient(P);
if (htOnStateIcon in TreeView1.GetHitTestInfoAt(P.X, P.Y)) then
TMyTreeNode(TreeView1.GetNodeAt(P.X, P.Y)).ToggleCheckState;
end;
procedure TForm5.TreeView1CreateNodeClass(Sender: TCustomTreeView; var NodeClass: TTreeNodeClass)
begin
NodeClass := TMyTreeNode;
end;