[c:\root\][c:\root\aaa\][c:\root\aaa\a1\][c:\root\aaa\a2\][c:\root\aaa\a1\a11][c:\root\aaa\a1\a12][c:\root\bbb\][c:\root\bbb\b1\][c:\root\ccc\]root----aaa
| |----a1
| | |---a11
| | |---a12
| |
| |----a2
|
|--bbb
| |---b1
|
|--ccc
如何将上面格式的INI文件转成下面的TREEVIEW,
实际的INI文件中的目录数是不固定的,但都是一个根目录下的。
| |----a1
| | |---a11
| | |---a12
| |
| |----a2
|
|--bbb
| |---b1
|
|--ccc
如何将上面格式的INI文件转成下面的TREEVIEW,
实际的INI文件中的目录数是不固定的,但都是一个根目录下的。
先找出第一个二级目录的所有直接下级,逐个创建,然后下一个。。依次类推
直到最后一级
for i:=0 to treeview.items.count-1 do
begin
node:=treeview.item[i];
插入node下一级过程;
end;由于插入时真实的COUNT是增加的,所以不能完全遍历上一级节点,请大侠帮忙!
//open an file, load it into treeview.
//2003.02.04
var
Ini:TIniFile;
SectionCount,ItemCount,i,j:integer;
TreeNode:TTreeNode;
begin
Ini:=TIniFile.Create(ExtractFilePath(Application.ExeName)+DescriptPath+FileName);
try
TreeViewMenu.Items.BeginUpdate;
TreeViewMenu.Items.Clear;
TreeNode:=nil;
with TreeViewMenu.Items.Add(TreeNode,Ini.ReadString('General','Name',AFileName)) do begin
ImageIndex:=0;
SelectedIndex:=0;
end;
SectionCount:=Ini.ReadInteger('General','SectionCount',0);
for i:=1 to SectionCount do begin
TreeNode:=TreeViewMenu.Items.GetFirstNode;
with TreeViewMenu.Items.AddChild(TreeNode,Ini.ReadString('Section'+IntToStr(i),'Name','')) do begin
ImageIndex:=1;
SelectedIndex:=1;
end;
ItemCount:=Ini.ReadInteger('Section'+IntToStr(i),'ItemCount',0);
TreeNode:=TreeNode.GetLastChild;
for j:=1 to ItemCount do begin
with TreeViewMenu.Items.AddChild(TreeNode,Ini.ReadString('Section'+IntToStr(i),'Item'+IntToStr(j),'')) do begin
ImageIndex:=2;
SelectedIndex:=2;
end;
end;
end;
if TreeViewMenu.Items.GetFirstNode<>nil then
TreeViewMenu.Items.GetFirstNode.Expand(false);
finally
TreeViewMenu.Items.EndUpdate;
Ini.Free;
end;
end;我这个程序里使用的ini文件中用
[General]
SectionCount=xx
来定义有多少个section,
每个Section下用
[SectionX]
ItemCount=xx
来定义该section下有多少个item,你的程序中可以使用ini.ReadSections来得到section列表,ini.ReadSection来得到某个section下有多少个item,照这个把程序稍许改一下就可以了。
现在公布出来,希望对大家有个帮助!
其中lshi
function addtotree(rootnode: TTreeNode): boolean;
var num, i: integer;
n: TStringlist;
nodes: array of TTreeNode;
begin
n := TStringlist.Create;
n.Clear; num := strsubcount(rootnode.Text, '\'); for i := 0 to l.Count - 1 do
begin
if (strsubcount(l.Strings[i], '\') = num + 1) and (pos(rootnode.Text, l.strings[i]) > 0) then
n.Add(l.Strings[i]);
end; setlength(nodes, n.Count); for i := 0 to n.Count - 1 do
begin
form1.treeview1.Items.BeginUpdate;
nodes[i] := form1.treeview1.Items.AddChildObject(rootnode, n.Strings[i], nil);
form1.treeview1.Items.EndUpdate;
end; for i := 0 to n.count - 1 do
begin
addtotree(nodes[i]);
end; n.Free; result := true;
end;
strsubcounts是统计子串的个数的函数。
var
PathList, Path:TStringList;
Node, NodeChild:TTreeNode;
I,J:Integer;
begin
PathList:=TStringList.Create();
Path:=TStringList.Create();
try
PathList.LoadFromFile(AFileName);
Path.Delimiter:='\';
Node:=nil;
for I:=0 to PathList.Count-1 do begin
Path.DelimitedText:=PathList.Strings[I];
Node:=nil;
for J:=0 to Path.Count-1 do begin
if Node=nil then begin
NodeChild:=TreeView1.Items.GetFirstNode();
end
else begin
NodeChild:=Node.getFirstChild();
end;
while NodeChild<>nil do begin
if NodeChild.Text=Path.Strings[J] then begin
Node:=NodeChild;
break;
end;
NodeChild:=NodeChild.GetNext();
end;
if NodeChild=nil then begin
Node:=TreeView1.Items.AddChild(Node,Path.Strings[J]);
//{ enable these codes to see the effect
if Assigned(Node.Parent) then begin
Node.Parent.Expand(false);
end;
Application.ProcessMessages;
Sleep(500);
//}
end;
end;//for J
end;//for I
finally
PathList.Free;
Path.Free;
end;
end;
Path.DelimitedText:=PathList.Strings[I];
//**
if Length(Path.Strings[Path.Count-1])=0 then begin
Path.Delete(Path.Count-1);
end;
//**
Node:=nil;
for J:=0 to Path.Count-1 do begin