Posłuż się tym kodem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
procedure WriteNodeData(const f:TFileStream; node:TTreeNode); var temp,t: integer; st: array[0..255] of char; begin //zapis nazwy temp := Length(node.Text); f.Write(temp, sizeof(integer)); for t := 0 to temp-1 do st[t] := node.text[t+1]; f.Write(st, temp); //zapis dzieci //ile dzieci temp := node.Count; f.Write(temp, sizeof(integer)); for t := 0 to temp-1 do WriteNodeData(f, node.Item[t]); end; procedure ReadNodeData(const f:TFileStream; node:TTreeNode); var temp,t: integer; st: array[0..255] of char; s: string; nod: TTreeNode; begin //odczyt nazwy f.Read( temp, sizeof(integer)); f.read(st, temp); s := ''; for t := 0 to temp-1 do s := s + st[t]; nod := form1.TreeView1.Items.AddChild(node, s); //ile dzieci f.Read( temp, sizeof(integer)); //zapis dzieci for t := 0 to temp-1 do ReadNodeData(f, nod); end; procedure TForm1.SaveData1Click(Sender: TObject); var f: TFileStream; t,y: integer; begin f := TFileStream.Create('tree.dat',fmCreate); t :=TreeView1.Items.Count; y := 0; for t := 0 to TreeView1.Items.Count -1 do if TreeView1.items[t].Level =0 then inc(y); f.Write(y,sizeof(integer)); for t := 0 to TreeView1.Items.Count -1 do if TreeView1.items[t].Level =0 then WriteNodeData(f, TreeView1.items[t]); f.free; end; procedure TForm1.LoadData2Click(Sender: TObject); var f: TFileStream; t,y: integer; begin TreeView1.Items.Clear; f := TFileStream.Create('tree.dat',fmOpenRead); f.read(y, sizeof(integer)); for t := 0 to y-1 do ReadNodeData(f, nil); f.free; end; |
Autor: Toster