需要了解的屬性:
Count
property Count: Integer;
Use Count to determine the number of tree nodes in the tree view that owns the tree nodes object. Count provides an upper bound when iterating through the entries in the Item property array.
Handle
property Handle: HWND;
Use Handle to obtain the handle of the tree view that owns the tree nodes object.
Item
property Item[Index: Integer]: TTreeNode; default;
Use Item to access to a node by its position in the tree view. The first node has an index of 0, the second an index of 1, and so on.
Item is the default property for TTreeNodes. This means that the name of the Item property can be omitted when indexing into the set of tree nodes. Thus, the line
FirstNode := TreeView1.Items.Item[0];
can be written
FirstNode := TreeView1.Items[0];
Owner
property Owner: TCustomTreeView;
Use the Owner property to access the tree view control that displays the nodes maintained by the TTreeNodes object.
需要了解的方法:
Add
function Add(Node: TTreeNode; const S: string): TTreeNode;
The node is added as the last sibling of the Node parameter. The S parameter specifies the Text property of the new node. Add returns the node that has been added. If the tree view is sorted, Add inserts the node in the correct sort order position rather than as the last child of the Node parameter’s parent.
AddChild
function AddChild(Node: TTreeNode; const S: string): TTreeNode;
The node is added as a child of the node specified by the Node parameter. It is added to the end of Node's list of child nodes. The S parameter specifies the Text property of the new node. AddChild returns the node that has been added. If the tree view is sorted, AddChild inserts the node in the correct sort order position, rather than as the last child of the Node parameter.
AddChildFirst
function AddChildFirst(Node: TTreeNode; const S: string): TTreeNode;
Use AddChildFirst to insert a node as the first child of the node specified by the Node parameter. The S parameter specifies the Text property of the new node. Nodes that appear after the added node are moved down one row and reindexed with valid Index values. AddChildFirst returns the node that has been added.
AddChildObject
function AddChildObject(Node: TTreeNode; const S: string; Ptr: Pointer): TTreeNode;
The node is added as the last child of the node specified by the Node parameter. The S parameter specifies the Text property of the new node. The Ptr parameter specifies the Data property value of the new node. AddChildObject returns the node that has been added.
Note: The memory referenced by Ptr is not freed when the tree nodes object is freed.
AddChildObjectFirst
function AddChildObjectFirst(Node: TTreeNode; const S: string; Ptr: Pointer): TTreeNode;
The node is added as the first child of the node specified by the Node parameter. Nodes that appear after the added node are moved down one row and reindexed with valid Index values. The S parameter specifies the Text property of the new node. The Ptr parameter specifies the Data property value of the new node. AddChildObjectFirst returns the node that has been added.
Note: The memory referenced by Ptr is not freed when the tree nodes object is freed.
AddFirst
function AddFirst(Node: TTreeNode; const S: string): TTreeNode;
The node is added as the first sibling of the node specified by the Node parameter. Nodes that appear after the added node are moved down one row and re-indexed with valid Index values. The S parameter specifies the Text property of the new node. AddFirst returns the node that has been added.
AddNode
function AddNode(Node, Relative: TTreeNode; const S: string; Ptr: Pointer; Method: TNodeAttachMode): TTreeNode;
AddNode adds a node object to the collection. AddNode returns the newly-added node, and takes the following parameters.
Node is the node object to be added. If Node is nil, then the tree view's CreateNode method is called to provide one.
The Relative and Method parameters together define the location of the new node. Relative is an existing node that will be either parent or sibling to the new node. Method specifies the precise relationship between the new node and relative, as given by a TNodeAttachMode constant.
The S parameter specifies the Text property of the new node.
The Ptr parameter specifies the Data property value of the new node.
Note: The memory referenced by Ptr is not freed when the tree nodes object is freed.
AddObject
function AddObject(Node: TTreeNode; const S: string; Ptr: Pointer): TTreeNode;
The node is added as the last sibling of the node specified by the Node parameter. The S parameter specifies the Text property of the new node. The Ptr parameter specifies the Data property value of the new node. AddObject returns the node that has been added.
Note: The memory referenced by Ptr is not freed when the tree nodes object is freed.
AddObjectFirst
function AddObjectFirst(Node: TTreeNode; const S: string; Ptr: Pointer): TTreeNode;
The node is added first to the list to which the node specified by the Node parameter belongs. nodes that appear after the added node are moved down one row and reindexed with valid Index values. The S parameter specifies the Text property of the new node. The Ptr parameter specifies the Data property value of the new node. AddObjectFirst returns the node that has been added.
AlphaSort
function AlphaSort([ARecurse: Boolean]): Boolean;
AlphaSort triggers node sorting or resorting. If an OnCompare event handler is defined for the associated TCustomTreeView, that routine determines sort order. If no OnCompare handler is defined, nodes are sorted by a simple case-sensitive compare of their captions.
The optional ARecurse parameter (default false) specifies that sorting should recursively descend the node tree and sort each subtree in turn.
Calling AlphaSort is equivalent to calling CustomSort with a nil (
Use Assign to copy information from one tree nodes object to another. If Source is any other type of object, Assign calls its inherited method, which copies properties from any object that can copy to a TTreeNodes object in its AssignTo method.
BeginUpdate
procedure BeginUpdate;
BeginUpdate prevents the screen from being repainted when new nodes are added, deleted, or inserted. Tree nodes affected by the changes will have invalid Index values until EndUpdate is called.
Use BeginUpdate to prevent screen repaints and to speed processing time while adding nodes to the tree view.
Note: Calls to BeginUpdate are cumulative; for every call to BeginUpdate there must be a corresponding call to EndUpdate.
Clear
procedure Clear;
Use the Clear method to remove all the nodes of a tree view.
Create
constructor Create(AOwner: TCustomTreeView);
Call Create to instantiate a TTreeNodes object at runtime. The TTreeNodes object used by TTreeView is created automatically from the constructor of the tree view object.
After calling the inherited constructor, Create sets the Owner property of the tree nodes object to the value of the AOwner parameter.
CustomSort
type TTVCompare = function(lParam1, lParam2, lParamSort: Longint; ARecurse: Boolean = False): Integer stdcall;
function CustomSort(SortProc: TTVCompare; Data: Longint): Boolean;
CustomSort triggers node sorting or resorting, using a comparison routine indicated by the SortProc parameter. The Data parameter is passed to the comparison routine. The optional ARecurse parameter (default false) specifies that sorting should recursively descend the node tree and sort each subtree in turn.
If SortProc is nil (
In the comparison routine, the lParam1 and lParam2 parameters refer to two nodes when cast to TTreeNode. The lParamSort parameter is the value previously passed to the Data parameter of CustomSort. The return value of the comparison routine indicates the relative sort order of IParam1 and IParam2:
!al(,3,TopicNotFound,main)
Return Value Meaning
!al(,3,TopicNotFound,main)
< 0 IParam1 comes before IParam2
!al(,3,TopicNotFound,main)
0 IParam1 and IParam2 are equivalent
!al(,3,TopicNotFound,main)
> 0 IParam2 comes before IParam1
Delete
procedure Delete(Node: TTreeNode);
Delete removes the tree node specified by the Node parameter and all its children from the tree view.
Destroy
destructor Destroy; override;
Do not call Destroy directly in an application. The tree view that owns the TTreeNodes object destroys the tree nodes object by calling its Free method.
EndUpdate
procedure EndUpdate;
Use the EndUpdate method to enable screen updating after BeginUpdate has been called. Calls to BeginUpdate are cumulative, so calling EndUpdate will only update the tree view if every other call to BeginUpdate has already been matched by a call to EndUpdate.
GetFirstNode
function GetFirstNode: TTreeNode;
Use the GetFirstNode method to retrieve the first node in the tree view. GetFirstNode returns the value of Item[0].
GetNode
function GetNode(ItemId: HTreeItem): TTreeNode;
ItemId is a handle to the node in the tree view.
Insert
function Insert(Node: TTreeNode; const S: string): TTreeNode;
Call Insert to add a new sibling to the Node parameter, immediately preceding Node. The S parameter specifies the Text property of the new node. Insert returns the new node.
InsertNode
function InsertNode(Node, Sibling: TTreeNode; const S: string; Ptr: Pointer): TTreeNode;
Call InsertNode to add a new sibling to the Node parameter, immediately preceding Node. The S parameter specifies the Text property of the new node. The Ptr parameter specifies data associated with the node. InsertNode returns the new node.
Note: The memory referenced by Ptr is not freed when the tree nodes object is freed.
InsertObject
function InsertObject(Node: TTreeNode; const S: string; Ptr: Pointer): TTreeNode;
Call InsertObject to add a new sibling to the Node parameter, immediately preceding Node. The S parameter specifies the Text property of the new node and the Ptr parameter specifies the Data property of the new node. InsertObject returns the new node.
Note: The memory referenced by Ptr is not freed when the tree nodes object is freed.