无需你的理解 發表於 2013-10-19 15:54:17

Delphi实现树型结构具体实例

<P><div class="codetitle"><span><U>复制代码</U></span> 代码如下:</div><div class="codebody" id="code39713"><BR>unit Unit1;<BR>interface<BR>uses<BR>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<BR>&nbsp; Dialogs, StdCtrls, ComCtrls, DB, ADODB;<BR>type<BR>&nbsp; PNodeInfoEx = ^TNodeInfoEx;<BR>&nbsp; TNodeInfoEx = Packed Record<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NodeID&nbsp;&nbsp;&nbsp; : Integer;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ParentID&nbsp; : Integer;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NodeType&nbsp; : Integer;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ChnNodeTitle : String;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ImageIndex: SmallInt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SelectedIndex: SmallInt;<BR>&nbsp;&nbsp; end;<BR>&nbsp; TForm1 = class(TForm)<BR>&nbsp;&nbsp;&nbsp; tv1: TTreeView;<BR>&nbsp;&nbsp;&nbsp; btn1: TButton;<BR>&nbsp;&nbsp;&nbsp; qry1: TADOQuery;<BR>&nbsp;&nbsp;&nbsp; procedure btn1Click(Sender: TObject);<BR>&nbsp;&nbsp;&nbsp; procedure FormDestroy(Sender: TObject);<BR>&nbsp; private<BR>&nbsp;&nbsp;&nbsp; { Private declarations }<BR>&nbsp;&nbsp;&nbsp; function StaticBuildTree(TreeView:TTreeView ):Boolean;<BR>&nbsp;&nbsp;&nbsp; function AddTreeItem(TreeView:TTreeView; AddNodeInfo:PNodeInfoEx):TTreeNode;<BR>&nbsp;&nbsp;&nbsp; function FindTreeItem(TreeView:TTreeView; CurNodeID:integer): TTreeNode;<BR>&nbsp; public<BR>&nbsp;&nbsp;&nbsp; { Public declarations }<BR>&nbsp; end;<BR>var<BR>&nbsp; Form1: TForm1;<BR>implementation<BR>{$R *.dfm}<br><br>function TForm1.StaticBuildTree(TreeView:TTreeView ):Boolean;<BR>var<BR>&nbsp; AddNodeInfo : PNodeInfoEx;<BR>begin<BR>&nbsp; Result := False;<BR>&nbsp; qry1.LoadFromFile('c:/AdminixTree.xml');//这里以XML文件做为数据源<BR>&nbsp; Treeview.Items.BeginUpdate;//记住:在进行批量添加数据时要使用BeginUpdate,来暂时关闭由于添加数据而触发的某些事件(如OnChange事件等)<BR>&nbsp; Treeview.Items.Clear;//清空Treeview<BR>&nbsp; try<BR>&nbsp;&nbsp;&nbsp; try<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if qry1.RecordCount &gt;0 then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qry1.First;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while Not qry1.Eof do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; New(AddNodeInfo) ;//生成结构体<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddNodeInfo^.NodeID := qry1.FieldByName('NODE_ID').AsInteger;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddNodeInfo^.ParentID := qry1.FieldByName('PARENT_ID').AsInteger;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddNodeInfo^.NodeType := qry1.FieldByName('NodeType').AsInteger;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddNodeInfo^.ChnNodeTitle := qry1.FieldByName('ChnNodeTitle').AsString;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddNodeInfo^.ImageIndex := qry1.FieldByName('ImageIndex').AsInteger;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddNodeInfo^.SelectedIndex := qry1.FieldByName('SelectedIndex').AsInteger;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddTreeItem(Treeview,AddNodeInfo);//把结构体的指针存到Treeview中<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qry1.Next;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp; except<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Application.MessageBox('生成树结点失败',MB_ICONSTOP+MB_OK);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise;//向上级抛异常<BR>&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp; qry1.Close;<BR>&nbsp;&nbsp;&nbsp; Result := True;<BR>&nbsp; finally<BR>&nbsp;&nbsp;&nbsp; Treeview.Items.EndUpdate;<BR>&nbsp; end;<BR>end;<BR>//在加入结点时,应先判断加入的是父结点还是子结点,判断的依据是在已存在的树结点中是否存在该结点的ParentID<BR>function TForm1.AddTreeItem(TreeView:TTreeView; AddNodeInfo:PNodeInfoEx):TTreeNode;<BR>var<BR>&nbsp;&nbsp;&nbsp; ParentNode: TTreeNode;<BR>begin<BR>&nbsp;&nbsp;&nbsp; ParentNode := FindTreeItem(Treeview,AddNodeInfo^.ParentID);<BR>&nbsp;&nbsp;&nbsp; If ParentNode &lt;&gt; nil then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result := Treeview.Items.AddChildObject(ParentNode, Trim(AddNodeInfo.ChnNodeTitle), Pointer(AddNodeInfo))<BR>&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result := Treeview.Items.AddObject(ParentNode, Trim(AddNodeInfo.ChnNodeTitle), Pointer(AddNodeInfo));<BR>&nbsp;&nbsp;&nbsp; if Result&lt;&gt;nil then<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result.ImageIndex&nbsp;&nbsp;&nbsp; := AddNodeInfo.ImageIndex;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result.SelectedIndex := AddNodeInfo.SelectedIndex;<BR>&nbsp;&nbsp;&nbsp; end;<BR>end;<BR>//这里是判断是否存在其父结点<BR>function TForm1.FindTreeItem(TreeView:TTreeView; CurNodeID:integer): TTreeNode;<BR>var<BR>&nbsp;&nbsp;&nbsp; i : Integer;<BR>begin<BR>&nbsp; Result := nil;<BR>&nbsp; for i := 0 to Treeview.Items.Count-1 do<BR>&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if CurNodeID=PNodeInfoEx(Treeview.Items.Data)^.NodeID then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result := Treeview.Items;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp; end;<BR>end;<BR>//生成树结构<BR>procedure TForm1.btn1Click(Sender: TObject);<BR>begin<BR>&nbsp;&nbsp; StaticBuildTree (tv1)<BR>end;<BR>//在窗体释放时一定要把树结点中的结构体指针给释放掉,对于在Dispose时为什么要进行强制转型后释放,以前有专门的讲解,在此不在累述<BR>procedure TForm1.FormDestroy(Sender: TObject);<BR>var<BR>&nbsp;&nbsp;&nbsp; i : Integer;<BR>begin<BR>&nbsp; for i := 0 to tv1.Items.Count-1 do<BR>&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dispose( PNodeInfoEx(tv1.Items.Data)&nbsp; )<BR>&nbsp; end;<BR>end;<BR>end.<BR></div></P>
<P><div class="codetitle"><span><U>复制代码</U></span> 代码如下:</div><div class="codebody" id="code31427"><BR>//如何访问树结点?<BR>procedure TForm1.tv1MouseDown(Sender: TObject; Button: TMouseButton;<BR>&nbsp; Shift: TShiftState; X, Y: Integer);<BR>var<BR>&nbsp; pNode:TTreeNode;<BR>begin<BR>&nbsp; pNode:=tv1.GetNodeAt(x,y);<BR>&nbsp; if (pNode&lt;&gt;nil) and&nbsp; (Button=mbleft) then<BR>&nbsp;&nbsp; ShowMessage(PNodeInfoEx(pNode.Data)^.ChnNodeTitle);<BR>end;<BR></div></P>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>delphi7连接mysql5的实现方法</li><li>Delphi下OpenGL2d绘图之画四边形的方法</li><li>Delphi下OpenGL2d绘图之画线的方法</li><li>Delphi下OpenGL2d绘图之画点的方法</li><li>Delphi下OpenGL2d绘图之初始化流程详解</li><li>Delphi使用OpenGL2d绘图之画图片Bmp的方法</li><li>C# 调用Delphi dll 实例代码</li><li>Delphi 生成excel中饼图的实例代码</li><li>Delphi7中Listview的常用功能汇总</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: Delphi实现树型结构具体实例