650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "width: 287px; height: 384px" border = "0" alt = "" src = "http://www.bkjia.com/uploads/allimg/131228/1K0496060-0.jpg" width = "160" height = "120"/>
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; // child node
ChildNodeInfo: PNodeInfo; // child node Information
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 + '''); // obtain the child node Information
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); // Add a child node and associate it with the child node Information
CreateChildTree (ChildNode); // recursive
Next;
End;
Close;
End;
End;
Procedure TForm3.CreateViewTree (Sender: TObject );
Var
BootNode: TTreeNode; // Root Node
BootNodeInfo: PNodeInfo; // root node Information
Begin
With ADOQuery1 do
Begin
SQL. Clear;
SQL. Add ('select ID, FullName, Url FROM PInfo WHERE ParentID IS null'); // obtain the root node Information
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); // Add the root node and associate it with the root node Information
Close;
End;
CreateChildTree (BootNode); // create a subtree
TreeView1.FullExpand; // expand all Tree nodes
End;
Procedure TForm3.Button1Click (Sender: TObject );
Begin
CreateViewTree (Sender );
End;
Procedure TForm3.TreeView1DblClick (Sender: TObject );
Var
TNode: TTreeNode;
X, Y: Integer;
Begin
// Obtain the double-click Node
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); // displays the information of the double-click node.
End;
End.
This article from the "lost after a short stay" blog, please be sure to keep this source http://suguiyang.blog.51cto.com/1035725/375681