⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 014.htm

📁 Delphi书籍--Delphi网上教程
💻 HTM
字号:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>-->DELPHI专题--控件应用-->树形图(Treeview)的使用概述</TITLE>
<META NAME="keywords" CONTENT=" DELPHI专题--控件应用 树形图(Treeview)的使用概述">
<META NAME="description" CONTENT=" - DELPHI专题--控件应用 - 树形图(Treeview)的使用概述">

<style>
<!--
#page {position:absolute; z-index:0; left:0px; top:0px}
.tt3 {font: 9pt/12pt "宋体"}
.tt2 {font: 12pt/15pt "宋体"}
a {text-decoration:none}
a:hover {color: blue;text-decoration:underline}
-->
</style>
</HEAD>
<a href="index.html">返回</a>



<body text="#000000" aLink=#9900ff link=#006699 vLink=#006699 bgcolor="#FFFFFF" leftmargin="3" topmargin="3" marginheight="3" marginwidth="3">
<TABLE WIDTH="100%" CELLPADDING=10 CELLSPACING=0 BORDER=0>
<TR>

<TD class="tt2" bgcolor="#F5F8F8" width="84%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="楷体_GB2312">树形图(Treeview)的使用概述</FONT></B></center>
<hr color="#EE9B73" size="1" width="94%">
<p class="tt2"> 树形图<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>
&nbsp;&nbsp;&nbsp; <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&amp;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> 
<hr color="#EE9B73" size="1" width="94%"> 
 
</TD> 
 
</TR> 
</table> 
</BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -