📄 treeview1.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Welcome to Delphi Tips !</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<link rel="stylesheet" href="../tips.css"></head>
<body bgcolor="#D8D0C8" text="#675350">
<div align="left">
<table border="1" cellpadding="0" cellspacing="0" width="579" height="63" bordercolorlight="#784400" bordercolordark="#D8D0C8">
<tr>
<td width="194" height="86"><img src="../images/title.gif" width="340" height="85"></td>
<td width="377" height="86" valign="top"><img src="../images/dong.jpg" width="251" height="85">
</td>
</tr>
</table>
<br>
</div>
<div align="left">
<table border="1" cellpadding="0" cellspacing="0" width="578" height="18" bordercolorlight="#784400" bordercolordark="#CCCCCC" class="p2">
<tr bgcolor="#B0A498">
<td width="574" height="18"> ><a href="../index.htm">DelphiTips首页</a>
> <a href="main.htm">控件应用</a></td>
</tr>
</table>
<br>
<table width="96%" border="1" bordercolorlight="#784400" cellspacing="0" bordercolordark="#D8D0C8">
<tr bgcolor="#B0A498" class="p1">
<td colspan="3">
<div align="center">树形图(Treeview)的使用概述</div>
</td>
<td width="12%" class="p2"><<<a href="010.htm">上一篇</a></td>
<td width="13%" class="p2"><a href="013.htm">下一篇</a>>></td>
</tr>
<tr class="p2">
<td colspan="5">
<table width="100%" border="0" class="p2">
<tr>
<td width="3%"> </td>
<td width="94%">
<p class="p2"> 树形图<font face="Times New Roman">(Treeview)</font>是<font
face="Times New Roman">Win95</font>下新增加的通用显示部件<font
face="Times New Roman">(Common Control</font>,在<font face="Times New Roman">COMCTL32.DLL</font>中<font
face="Times New Roman">)</font>之一,从<font face="Times New Roman">Delphi2.0</font>开始也增加了相应的控件<font
face="Times New Roman">Treeview</font>,用于取代原<font face="Times New Roman">Outline</font>控件。由于树形图结构较复杂,使用起来常不知如何下手。这里就使用中的一些问题作些介绍。<br>
<font face="Times New Roman">Treeview</font>用于显示按照树形结构进行组织的数据,这在实际当中用途还是比较广泛的,如计算机中的文件系统<font
face="Times New Roman">(Windows95</font>中的资源管理器<font face="Times New Roman">)</font>、企业或公司的组成结构等等。<font
face="Times New Roman">Treeview</font>控件中一个树形图由节点<font
face="Times New Roman">(TreeNode)</font>和连接线组成。<font face="Times New Roman">TtreeNode</font>是<font
face="Times New Roman">TTreeview</font>的基本组成单元。一个树的节点又包含文本<font
face="Times New Roman">(Text)</font>和数据<font face="Times New Roman">(Data)</font>。<font
face="Times New Roman">Text</font>为<font face="Times New Roman">String</font>类,<font
face="Times New Roman">Data</font>则为无定形指针<font face="Times New Roman">(Untyped
Pointer)</font>,可以指向一个与节点相联系的数据结构。<br>
每一个节点下子节点形成这一节点的<font face="Times New Roman">Items</font>属性,当前节点有一个唯一的<font
face="Times New Roman">Index(TreeNode</font>的<font face="Times New Roman">Index</font>属性<font
face="Times New Roman">)</font>,用于说明子节点在<font face="Times New Roman">Items</font>中的位置,每一个节点下的子节点是顺序编号的,第一个是<font
face="Times New Roman">0</font>,第二个是<font face="Times New Roman">1</font>,依次类推。用<font
face="Times New Roman">IndexOf</font>方法获得子节点的顺序,绝对顺序<font
face="Times New Roman">(AbsoluteIndex)</font>则是指从<font face="Times New Roman">Treeview</font>第一个项开始的顺序值,第一个是<font
face="Times New Roman">0</font>,如此推下去。<font face="Times New Roman">Item</font>属性则根据<font
face="Times New Roman">Index</font>的值返回当前节点的第<font
face="Times New Roman">Index</font>个子节点。<font face="Times New Roman">Count</font>则表明属于此项的所有子节点的数量。用<font
face="Times New Roman">MoveTo</font>方法将<font face="Times New Roman">Item</font>由一个位置移到另一个位置。<br>
<font face="Times New Roman">Expanded</font>属性表明是否所有的子项都全部展开<font
face="Times New Roman">(</font>包括子项的子项<font face="Times New Roman">)</font>,为<font
face="Times New Roman">True</font>表示全部展开。<font face="Times New Roman">IsVisible</font>属性表明一个项是否在树中能被看到,如果树全部展开那么这个<font
face="Times New Roman">Item</font>是肯定可以被看到。<font face="Times New Roman">HasChildren</font>属性表明一个项是否有子项。
<font face="Times New Roman">GetFirstChild, GetLastChild, GetPrevChild,
and GetNextChild</font>分别返回当前项子项的第一个、最后一个和前一个、后一个项。<font
face="Times New Roman">GetNextSibling and GetPrevSibling</font>则返回在同一<font
face="Times New Roman">Level</font>下的下一个和上一个项。<font
face="Times New Roman">GetNextVisible and GetPrevVisible</font>则返回能看得到的下一个和上一个项。如果一个节点有<font
face="Times New Roman">Parent</font>,则<font face="Times New Roman">HasAsParent</font>方法返回<font
face="Times New Roman">True. Parent</font>为当前项的父项。<font
face="Times New Roman">Focused</font>属性确定焦点是否落在此节点上,被<font
face="Times New Roman">Focus</font>时会一个标准的方框围住。很显然,只有一个节点会被聚焦。 <font face="Times New Roman">Selected</font>属性表明一个节点是否被选中,同样只有一个节点会被选中。<font
face="Times New Roman">DropTarget</font>属性表明节点在拖动操作中是源还是目标。<br>
<br>
<font face="Times New Roman">.1.</font>添加、删除、修改节点:<br>
静态的方法可以在设计时通过<font face="Times New Roman">Items</font>的编辑器设置各节点的内容。<br>
在添加和删除前必须保证有节点被选中<font face="Times New Roman">(Treeview.Selected
= nil)</font><br>
用<font face="Times New Roman">AddFirst, AddFirstChild, AddChild</font>等先添加根节点,如<font
face="Times New Roman">Treeview.Items.AddFirst( nil, 'Root')</font>;<br>
然后以此为基础,添加此项的子节点。<br>
<br>
删除节点<br>
<font face="Times New Roman">Treeview.Selected.Delete</font><br>
<br>
编辑节点内容<br>
<font face="Times New Roman">Treeview.Selected.EditText</font><br>
<br>
注意:由于根节点没有父节点 <font face="Times New Roman">(TTreeNode.Parent=
nil)</font><br>
此外,在大批量添加数据到<font face="Times New Roman">Treeview</font>中时最好使用<br>
<font face="Times New Roman">TreeView.Items.BeginUpdate;</font><br>
添加节点<br>
<font face="Times New Roman">TreeView.Items.EndUpdate</font><br>
这样能加快显示速度。<br>
<br>
<font face="Times New Roman">2.</font>在节点上添加图象<br>
<font face="Times New Roman">Treeview</font>中几个与图象相关的属性:<br>
<font face="Times New Roman">SelectedIndex</font>:当节点被选中时在<font
face="Times New Roman">TimageList </font>中选什么样的图象<br>
<font face="Times New Roman">OverlayIndex</font>:选那副图象作为掩图(一幅图象透明地显示在另一幅图象的前面),比如一个节点不可用时加一副<font
face="Times New Roman">X</font>图象在其前面。<br>
<font face="Times New Roman">ImageIndex</font>:在常态时选用的图的序号<br>
<font face="Times New Roman">StateIndex</font>: 在<font face="Times New Roman">StateImages</font>这个<font
face="Times New Roman">ImageList</font>中对应的序号,<font face="Times New Roman">-1</font>时不显示图象<br>
比较典型的,象在文件管理器中的所显示的一样,<font
face="Times New Roman">Treeview</font>控件在节点之前也可以显示图象。在<font
face="Times New Roman">Form</font>中放置一<font face="Times New Roman">ImageList</font>控件,加入几个图片,分别被<font
face="Times New Roman">Index</font>为<font face="Times New Roman">0,1,</font>…在<font
face="Times New Roman">Treeview</font>的<font face="Times New Roman">Image</font>属性项填入你所加入的<font
face="Times New Roman">ImageList</font>的控件名称。<font face="Times New Roman">TreeNode</font>的<font
face="Times New Roman">ImageIndex</font>表示节点未被选中时<font
face="Times New Roman">(Selected=nil)</font>的图片序号,<font face="Times New Roman">SelectedIndex</font>表示节点被选中时图片序号。<br>
<br>
<font face="Times New Roman">3.</font>关于<font face="Times New Roman">Level<br>
Level</font>的概念可以用下图表示:<br>
<font face="Times New Roman">Level0</font> <font face="Times New Roman">Level1
Level2<br>
<br>
4.</font>排序<br>
<font face="Times New Roman">SortType</font>决定什么时候进行排序;<br>
<font face="Times New Roman">TreeView.AlphaSort</font>对节点进行排序,如果不能满足要求,你可以定义自己的<font
face="Times New Roman">CustomSort</font>方法。<br>
<br>
<font face="Times New Roman">5.Drag&Drop</font>操作,与标准的拖放操作使用方法一样。<br>
<br>
以上具体函数的使用细节可以参考联机帮助。具体的代码编写可以参考<font
face="Times New Roman">Delphi2.0</font>中提供的有关<font face="Times New Roman">Treeview</font>和<font
face="Times New Roman">ListView</font>控件的使用例子。</p>
</td>
<td width="3%"> </td>
</tr>
</table>
</td>
</tr>
<tr bgcolor="#B0A498" class="p2">
<td height="21" colspan="2"> <marquee behavior="alternate">如果你有什么好的资料,可以寄给我哟:)</marquee></td>
<td height="21" width="14%"><a href="../index.htm"><<回到首页</a></td>
<td height="21" width="12%"><<<a href="010.htm">上一篇</a></td>
<td height="21" width="13%"><a href="013.htm">下一篇</a>>></td>
</tr>
</table>
<br>
<br>
<hr size=1 noshade width=500>
<table width="75%" border="0" align="center" class="p2">
<tr>
<td colspan="2"> </td>
<td width="24%">
<table width="95%" border="1" bordercolorlight="#663300" bordercolordark="#CCCCCC" cellspacing="0" class="p2">
<tr>
<td>
<div align="center">Delphi 技巧集</div>
</td>
</tr>
</table>
</td>
<td width="38%"> </td>
</tr>
<tr>
<td width="16%"> </td>
<td colspan="2">Copyright 1999.11 by 东子</td>
<td width="38%"><a href="../jintongbao@188.net">Mail to me!</a></td>
</tr>
<tr>
<td colspan="4">
<div align="center">感谢广州视窗提供主页空间</div>
</td>
</tr>
</table>
<br>
</div><div align="center"><center>
</center></div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -