Delphi完成樹型構造詳細實例。本站提示廣大學習愛好者:(Delphi完成樹型構造詳細實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Delphi完成樹型構造詳細實例正文
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, DB, ADODB;
type
PNodeInfoEx = ^TNodeInfoEx;
TNodeInfoEx = Packed Record
NodeID : Integer;
ParentID : Integer;
NodeType : Integer;
ChnNodeTitle : String;
ImageIndex: SmallInt;
SelectedIndex: SmallInt;
end;
TForm1 = class(TForm)
tv1: TTreeView;
btn1: TButton;
qry1: TADOQuery;
procedure btn1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
function StaticBuildTree(TreeView:TTreeView ):Boolean;
function AddTreeItem(TreeView:TTreeView; AddNodeInfo:PNodeInfoEx):TTreeNode;
function FindTreeItem(TreeView:TTreeView; CurNodeID:integer): TTreeNode;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.StaticBuildTree(TreeView:TTreeView ):Boolean;
var
AddNodeInfo : PNodeInfoEx;
begin
Result := False;
qry1.LoadFromFile('c:/AdminixTree.xml');//這裡以XML文件做為數據源
Treeview.Items.BeginUpdate;//記住:在停止批量添加數據時要應用BeginUpdate,來臨時封閉因為添加數據而觸發的某些事宜(如OnChange事宜等)
Treeview.Items.Clear;//清空Treeview
try
try
if qry1.RecordCount >0 then
begin
qry1.First;
while Not qry1.Eof do
begin
New(AddNodeInfo) ;//生成構造體
AddNodeInfo^.NodeID := qry1.FieldByName('NODE_ID').AsInteger;
AddNodeInfo^.ParentID := qry1.FieldByName('PARENT_ID').AsInteger;
AddNodeInfo^.NodeType := qry1.FieldByName('NodeType').AsInteger;
AddNodeInfo^.ChnNodeTitle := qry1.FieldByName('ChnNodeTitle').AsString;
AddNodeInfo^.ImageIndex := qry1.FieldByName('ImageIndex').AsInteger;
AddNodeInfo^.SelectedIndex := qry1.FieldByName('SelectedIndex').AsInteger;
AddTreeItem(Treeview,AddNodeInfo);//把構造體的指針存到Treeview中
qry1.Next;
end;
end;
except
Application.MessageBox('生成樹結點掉敗',MB_ICONSTOP+MB_OK);
raise;//向下級拋異常
end;
qry1.Close;
Result := True;
finally
Treeview.Items.EndUpdate;
end;
end;
//在參加結點時,應先斷定參加的是父結點照樣子結點,斷定的根據是在已存在的樹結點中能否存在該結點的ParentID
function TForm1.AddTreeItem(TreeView:TTreeView; AddNodeInfo:PNodeInfoEx):TTreeNode;
var
ParentNode: TTreeNode;
begin
ParentNode := FindTreeItem(Treeview,AddNodeInfo^.ParentID);
If ParentNode <> nil then
Result := Treeview.Items.AddChildObject(ParentNode, Trim(AddNodeInfo.ChnNodeTitle), Pointer(AddNodeInfo))
else
Result := Treeview.Items.AddObject(ParentNode, Trim(AddNodeInfo.ChnNodeTitle), Pointer(AddNodeInfo));
if Result<>nil then
begin
Result.ImageIndex := AddNodeInfo.ImageIndex;
Result.SelectedIndex := AddNodeInfo.SelectedIndex;
end;
end;
//這裡是斷定能否存在其父結點
function TForm1.FindTreeItem(TreeView:TTreeView; CurNodeID:integer): TTreeNode;
var
i : Integer;
begin
Result := nil;
for i := 0 to Treeview.Items.Count-1 do
begin
if CurNodeID=PNodeInfoEx(Treeview.Items[i].Data)^.NodeID then
begin
Result := Treeview.Items[i];
Exit;
end;
end;
end;
//生成樹構造
procedure TForm1.btn1Click(Sender: TObject);
begin
StaticBuildTree (tv1)
end;
//在窗體釋放時必定要把樹結點中的構造體指針給釋放失落,關於在Dispose時為何要停止強迫轉型後釋放,之前有專門的講授,在此不在累述
procedure TForm1.FormDestroy(Sender: TObject);
var
i : Integer;
begin
for i := 0 to tv1.Items.Count-1 do
begin
Dispose( PNodeInfoEx(tv1.Items[i].Data) )
end;
end;
end.
//若何拜訪樹結點?
procedure TForm1.tv1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
pNode:TTreeNode;
begin
pNode:=tv1.GetNodeAt(x,y);
if (pNode<>nil) and (Button=mbleft) then
ShowMessage(PNodeInfoEx(pNode.Data)^.ChnNodeTitle);
end;