码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi中动态加载TreeView信息

时间:2019-08-25 17:47:40      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:and   begin   click   message   not   dia   bsp   end   span   

技术图片

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, DB, ADODB, StdCtrls;

type

   PNodeInfo=^TNodeInfo;
   TNodeInfo=record
     ID:string;
     FullName:string;
     Url:string;
   end;

  TForm3 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    TreeView1: TTreeView;
    Button1: TButton;
    procedure CreateChildTree(ParentNode: TTreeNode);
    procedure CreateViewTree(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TreeView1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.CreateChildTree(ParentNode: TTreeNode);
var
  Query:TADOQuery;
  ChildNode:TTreeNode; //孩子结点
  ChildNodeInfo:PNodeInfo; //孩子结点信息
begin
  Query:=TADOQuery.Create(nil);
  with Query do
  begin
    Connection:=ADOConnection1;
    SQL.Add(SELECT ID,FullName,Url FROM PInfo WHERE ParentID = ‘‘+PNodeInfo(ParentNode.Data)^.ID+‘‘); //获取孩子结点信息
    Open;
    while not Eof do
    begin
      New(ChildNodeInfo);
      ChildNodeInfo^.ID:=FieldByName(ID).AsString;
      ChildNodeInfo^.FullName:=FieldByName(FullName).AsString;
      ChildNodeInfo^.Url:=FieldByName(Url).AsString;
      ChildNode:=TreeView1.Items.AddChildObject(ParentNode,(ChildNodeInfo^.FullName),ChildNodeInfo); //添加孩子结点,并关联孩子结点信息
      CreateChildTree(ChildNode); //进行递归
      Next;
    end;
    Close;
  end;
end;

procedure TForm3.CreateViewTree(Sender: TObject);
var
  BootNode:TTreeNode; //根结点
  BootNodeInfo:PNodeInfo; //根结点信息
begin
  with ADOQuery1 do
  begin
    SQL.Clear;
    SQL.Add(SELECT ID,FullName,Url FROM PInfo WHERE ParentID IS NULL); //获取根结点信息
    Open;
 
    New(BootNodeInfo);
    BootNodeInfo^.ID:=FieldByName(ID).AsString;
    BootNodeInfo^.FullName:=FieldByName(FullName).AsString;
    BootNodeInfo^.Url:=FieldByName(Url).AsString;
    TreeView1.Items.Clear;
    BootNode:=TreeView1.Items.AddChildObject(nil,(BootNodeInfo^.FullName),BootNodeInfo); //添加根结点,并关联根结点信息
    Close;
  end;
  CreateChildTree(BootNode); //创建子树
  TreeView1.FullExpand; //展开所有树结点
end;
procedure TForm3.Button1Click(Sender: TObject);
begin
   CreateViewTree(Sender);
end;

procedure TForm3.TreeView1DblClick(Sender: TObject);
var
  TNode:TTreeNode;
  X,Y:Integer;
begin
  //获取双击的结点
  with TreeView1 do
  begin
    X:=ScreenToClient(Mouse.CursorPos).X;
    Y:=ScreenToClient(Mouse.CursorPos).Y;
    TNode:=GetNodeAt(X,Y);
  end;
  if (TNode<>nil) and (TNode=TreeView1.Selected) then
    ShowMessage(PNodeInfo(TNode.Data)^.Url); //显示双击结点的信息
end;

end.
 

 

Delphi中动态加载TreeView信息

标签:and   begin   click   message   not   dia   bsp   end   span   

原文地址:https://www.cnblogs.com/jijm123/p/11408413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!