Delphi 트 리 구조 구현 구체 적 인 사례

4488 단어 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;

좋은 웹페이지 즐겨찾기