프로그래밍/델파이

트리뷰 노드 만들고 저장하기 chatgpt

do121 2023. 3. 30. 07:31

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    btnSave: TButton;
    btnLoad: TButton;
    btnAddChild: TButton;
    btnDelete: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnLoadClick(Sender: TObject);
    procedure btnAddChildClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure SaveNode(Node: TTreeNode; TextFile: TFileStream);
begin
  TextFile.WriteAnsiString(Node.Text);
  TextFile.WriteBuffer(Node.ImageIndex, SizeOf(Node.ImageIndex));
  TextFile.WriteBuffer(Node.SelectedIndex, SizeOf(Node.SelectedIndex));
end;

procedure LoadNode(Node: TTreeNode; TextFile: TFileStream);
var
  Text: AnsiString;
begin
  SetLength(Text, TextFile.ReadCardinal);
  TextFile.Read(Pointer(Text)^, Length(Text));
  Node.Text := Text;
  TextFile.ReadBuffer(Node.ImageIndex, SizeOf(Node.ImageIndex));
  TextFile.ReadBuffer(Node.SelectedIndex, SizeOf(Node.SelectedIndex));
end;

procedure SaveTreeView(TreeView: TTreeView; TextFile: TFileStream; Node: TTreeNode);
var
  Child: TTreeNode;
begin
  while Assigned(Node) do
  begin
    SaveNode(Node, TextFile);
    if Node.HasChildren then
    begin
      TextFile.WriteAnsiString('1');
      Child := Node.GetFirstChild;
      SaveTreeView(TreeView, TextFile, Child);
      TextFile.WriteAnsiString('0');
    end
    else
      TextFile.WriteAnsiString('0');
    Node := Node.GetNextSibling;
  end;
end;

procedure LoadTreeView(TreeView: TTreeView; TextFile: TFileStream; Node: TTreeNode);
var
  Child: TTreeNode;
  HasChildren: AnsiChar;
begin
  while True do
  begin
    SetLength(HasChildren, 1);
    TextFile.Read(Pointer(HasChildren)^, Length(HasChildren));
    if HasChildren = '1' then
    begin
      Child := TreeView.Items.AddChild(Node, '');
      LoadNode(Child, TextFile);
      LoadTreeView(TreeView, TextFile, Child);
    end
    else if HasChildren = '0' then
      Break;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Add some sample nodes
  TreeView1.Items.Add(nil, 'Root');
  TreeView1.Items.AddChild(TreeView1.Items.GetFirstNode, 'Child 1');
  TreeView1.Items.AddChild(TreeView1.Items.GetFirstNode, 'Child 2');
  TreeView1.FullExpand;
end;

procedure TForm1.btnSaveClick(Sender: TObject);
var
  TextFile: TFileStream;
begin
  TextFile := TFileStream.Create('treeview.txt', fmCreate);
  try
    SaveTreeView(TreeView1, TextFile, TreeView1.Items.GetFirstNode);
  finally
    TextFile.Free;
  end;
end;

procedure TForm1.btnLoadClick(Sender: TObject);
var
  TextFile: TFileStream;
begin
  TextFile := TFileStream.Create('treeview.txt', fmOpenRead);
  try
    // Remove existing nodes
    TreeView1.Items.Clear;
    // Load nodes from file
    LoadTreeView(TreeView1, TextFile, nil);
    TreeView1.FullExpand;
  finally
    TextFile.Free;
  end;
end;

procedure TForm1.btnAddChildClick(Sender: TObject);
var
  Node: TTreeNode;
begin
  Node := TreeView1.Selected;
  if Assigned(Node) then
  begin
    TreeView1.Items.AddChild(Node, 'New Child');
    Node.Expand(False);
  end;
end;

procedure TForm1.btnDeleteClick(Sender: TObject);
var
  Node: TTreeNode;
begin
  Node := TreeView1.Selected;
  if Assigned(Node) and (Node <> TreeView1.Items.GetFirstNode) then
    Node.Delete;
end;

end.

'프로그래밍 > 델파이' 카테고리의 다른 글

폼을 안보이게 하기  (1) 2024.01.26
델파이 코딩 팁  (0) 2023.05.31
가상머신에 설치된 delphi7을 실컴으로 옮기기  (0) 2023.02.17