如何建立动态树? 谢谢

解决方案 »

  1.   

    var
      Action: TAction;
      Category: String;
      CatList: TStrings;
      CatIndex: Integer;
      I: Integer;
      Node: TTreeNode;
      ParentNode: TTreeNode;
      GroupIndex: Integer;
      GroupExpanded: Integer;
      SeparatorIndex: Integer;
    begin
      tvActions.Images.Assign(Actions.Images);
      GroupIndex := tvActions.Images.Count;
      GroupExpanded := GroupIndex + 1;
      SeparatorIndex := GroupExpanded + 1;
      FExpandedIndex := GroupExpanded;
      FCollapsedIndex := GroupIndex;
      tvActions.Images.AddImages(imgsAdded);
      CatList := TStringList.Create;
      try
        for I := 1 to Actions.ActionCount - 1 do
        begin
          try
            Action := TAction(Actions.Actions[I]);
            Category := Action.Category;
            CatIndex := CatList.IndexOf(Category);
            if CatIndex < 0 then
            begin
              ParentNode := tvActions.Items.AddChild(nil, Category);
              ParentNode.ImageIndex := GroupIndex;
              ParentNode.SelectedIndex := GroupIndex;
              Node := tvActions.Items.AddChild(ParentNode, '分隔符');
              Node.ImageIndex := SeparatorIndex;
              Node.SelectedIndex := SeparatorIndex;
              CatIndex := CatList.AddObject(Category, ParentNode);
            end;
            ParentNode := TTreeNode(CatList.Objects[CatIndex]);
            Node := tvActions.Items.AddChild(ParentNode, Action.Caption);
            Node.ImageIndex := Action.ImageIndex;
            Node.SelectedIndex := Action.ImageIndex;
            Node.Data := Action;
          except
          end;
        end;
      finally
        CatList.Free;
      end;end;