需要了解的屬性:
AutoExpand
property AutoExpand: Boolean;
Set AutoExpand to true to cause the selected item to expand and the unselected items to collapse.
BorderStyle
property BorderStyle: TBorderStyleTBorderStyle;
Set BorderStyle to specify whether the tree vIEw control should be outlined with a single-line border. These are the possible values:
Value Meaning
bsNone No visible border
bsSingle Single-line border
Canvas
property Canvas: TCanvas;
Use the Canvas property to paint to the canvas from the OnCustomDraw and OnCustomDrawItem event handlers.
ChangeDelay
property ChangeDelay: Integer;
Use ChangeDelay to get or set the delay, in milliseconds, between when a node is selected and when the OnChange event occurs.
Set the ChangeDelay to 50 milliseconds to emulate the behavior of the tree-vIEw control used in Windows Explorer.
DropTarget
property DropTarget: TTreeNode;
Read DropTarget to determine whether a node in the tree view is drawn as the target of a drag and drop Operation. Set DropTarget when specifying a particular node in the tree vIEw as the drop target of a dragged item.
Note: When DropTarget is set, the application must still handle the actual logic of accepting the dragged object by the indicated node.
HideSelection
property HideSelection: Boolean;
Use HideSelection to specify whether the user is given visual feedback about the current selection in the tree vIEw when it does not have focus. If true, the selected node is not visually distinct from other nodes until focus returns to the control. If false, the node always appears selected.
HotTrack
property HotTrack: Boolean;
Set HotTrack to true to provide visual feedback about which item is under the mouse. Set HotTrack to false to suppress the visual feedback about which item is under the mouse.
Images
property Images: TCustomImageList;
Use Images to provide a customized list of bitmaps that can be displayed to the left of a node’s label. Individual nodes specify the image from this list that should appear by setting their ImageIndex property.
Indent
property Indent: Integer;
Use Indent to determine how far child nodes are indented from their parent nodes when the parent is expanded.
Items
property Items: TTreeNodes;
Individual nodes in a tree view are TTreeNode objects. These individual nodes can be accessed by using the Items property along with the item's index into the tree vIEw. For example, to Access the second item in the tree vIEw, you could use the following code.
MyTreeNode := TreeVIEw1.Items[1];
When setting this property at design-time in the Object Inspector the Tree View Items Editor appears. Use the New Item and New SubItem buttons to add items to the tree vIEw. Use the Text property to modify what text is displayed in the label of the item.
At run-time nodes can be added and inserted by using the TTreeNodes methods AddChildFirst, AddChild, AddChildObjectFirst, AddChildObject, AddFirst, Add, AddObjectFirst, AddObject, and Insert.
Note: Accessing tree view items by index can be time-intensive, particularly when the tree view contains many items. For optimal performance, try to design your application so that it has as few dependencies on the tree vIEw’s item index as possible.
MultiSelect
property MultiSelect: Boolean;
Set MultiSelect to specify whether users can select multiple nodes using the Control and Shift keys. A selection style must also be chosen in MultiSelectStyle.
MultiSelectStyle
type
TMultiSelectStyles = (msControlSelect, msShiftSelect, msVisibleOnly, msSiblingOnly);
TMultiSelectStyle = set of TMultiSelectStyles;
property MultiSelectStyle: TMultiSelectStyle;
MultiSelectStyle determines how multiple selections are made when MultiSelect is true. MultiSelectStyle must include at least one of the following values.
msControlSelect: Clicking on any node with the Control key pressed toggles the selection of that node.
msShiftSelect: Clicking on any node with the Shift key press selects that node, the last single node selected, and the nodes in between. All other nodes are deselected.
msVisibleOnly Multiple: selections with the Shift key do not include child nodes of collapsed nodes.
msSiblingOnly: Selected nodes are restricted to a single set of siblings.
If msControlSelect or msShiftSelect are in effect, the last singly-selected node becomes the primary selection, referenced by Selections[0]. The primary selection is the anchor for extended selections using the Shift key.
ReadOnly
property ReadOnly: Boolean;
Use ReadOnly to specify whether the user can edit the nodes of the tree vIEw. If ReadOnly is true, the user can expand and collapse nodes, but can’t edit their labels. If ReadOnly is false, the user can edit the labels as well. The default value is false.
RightClickSelect
property RightClickSelect: Boolean;
Use RightClickSelect to allow the Selected property to indicate nodes the user clicks with the right mouse button. If RightClickSelect is true, the value of Selected is the value of the node last clicked with either the right or left mouse button. If RightClickSelect is false, the value of Selected is the node last clicked using the left mouse button.
RightClickSelect affects only the value of the Selected property. It does not cause the tree vIEw to highlight a new node if the node is selected using the right mouse button.
Note: RightClickSelect must be set to true before the user right-clicks the tree vIEw for it to affect the value of the Selected property.
RowSelect
property RowSelect: Boolean;
Set RowSelect to true to cause the entire row of the selected item to be highlighted.
RowSelect is ignored if ShowLines is true.
Selected
property Selected: TTreeNode;
Read Selected to Access the selected node of the tree vIEw. If there is no selected node, the value of Selected is nil (
Set Selected to set a node in the tree view. When a node becomes selected, the tree view's OnChanging and OnChange events occur. Also, if the specified node is the child of a collapsed parent item, the parent's list of child items is expanded to reveal the specified node. In this case, the tree vIEw's OnExpanded and OnExpanding events occur as well.
If the RightClickSelect property is true, the value of Selected is the last node that was clicked in the tree vIEw, even if it was clicked with the right mouse button. This may differ from the node that is highlighted to indicate selection.
If the MultiSelect property is true and the MultiSelectStyle property includes msControlSelect, then Selected returns the last node clicked on, even if that click deselected the node. For a current selection status when MultiSelect is true, refer to the Selections property.
SelectionCount
property SelectionCount: Cardinal;
SelectionCount returns the number of nodes currently selected.
Selections
property Selections[Index: Integer]: TTreeNode;
Selections returns one of selected nodes. The maximum value of Index is SelectionCount-1. If more than one node is selected, Selections[0] is the primary selected node. This node is the starting point for extended selections if MultiSelectStyle includes msShiftSelect.
ShowButtons
property ShowButtons: Boolean;
If ShowButtons is true, a button will appear to the left of each parent item. The user can click the button to expand or collapse the child items as an alternative to double-clicking the parent item.
ShowLines
property ShowLines: Boolean;
If ShowLines is true, lines linking child nodes to their parent nodes are displayed. Nodes at the root of the hIErarchy are not automatically linked. To link nodes at the root, the ShowRoot property must also be set to true.
ShowRoot
property ShowRoot: Boolean;
To show lines connecting top-level nodes to a single root, set the tree view's ShowRoot and ShowLines propertIEs to true.
SortType
property SortType: TSortType;
Once a tree view is sorted, the original hIErarchy is lost. That is, setting the SortType back to stNone will not restore the original order of items. These are the possible values:
stNone: No sorting is done.
stData: The items are sorted when the Data object or SortType is changed.
stText: The items are sorted when the Caption or SortType is changed.
stBoth: The items are sorted when either the Data object, the Caption or SortType is changed
Optionally, the OnCompare event can be hooked to handle comparisons.
StateImages
property StateImages: TCustomImageList;
Use StateImages to provide a set of bitmaps that reflect the state of tree vIEw nodes. The state image appears as an additional image to the left of the item's icon.
ToolTips
property ToolTips: Boolean;
Set ToolTips to true to specify that items in the tree vIEw control have tool tips (Help Hints).
Specify the ToolTip text in an OnHint event handler using the Hint property.
TopItem
property TopItem: TTreeNode;
When TopItem is changed, the tree view scrolls vertically so that the specified node is topmost in the list vIEw.
需要了解的方法:
AlphaSort
function AlphaSort([ARecurse: Boolean]): Boolean;
AlphaSort triggers node sorting or resorting. If an OnCompare event handler is defined, 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 true) 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 (
Calling AlphaSort has the same effect as calling the same method for the Items property, except that in TTreeNodes, AlphaSort is not recursive by default.
To sort a subtree, use the AlphaSort method in TTreeNodes.
ClearSelection
procedure ClearSelection(KeepPrimary: Boolean = False); virtual;
ClearSelection deselects all selected nodes. If the KeepPrimary parameter is true, the primary selection (in the first element of the Selections property) is not toggled.
Create
constructor Create(AOwner: TComponent);
Call Create to instantiate a tree view at runtime. Tree view controls placed on forms at design time are created automatically. After calling the inherited constructor, Create initializes many propertIEs the control, including setting BorderStyle to bsSingle and setting ShowButtons, ShowRoot, and ShowLines, and HideSelection to true.
CustomSort
type TTVCompare = function(lParam1, lParam2, lParamSort: Longint): Integer stdcall;
function CustomSort(SortProc: TTVCompare; Data: Longint; ARecurse: Boolean = True): 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 true) 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:
Return Value Meaning
< 0 IParam1 comes before IParam2
0 IParam1 and IParam2 are equivalent
> 0 IParam2 comes before IParam1
Calling CustomSort has the same effect as calling the same method for the Items property, except that in TTreeNodes, CustomSort is not recursive by default.
To sort a subtree, call the CustomSort method of the Items property.
Deselect
procedure Deselect(Node: TTreeNode); virtual;
Deselect removes the selection from Node. This method has no effect unless MultiSelect is true and MultiSelectStyle includes msControlSelect.
Destroy
destructor Destroy; override;
Do not call the destructor directly in an application. Instead, use the Free method. Free checks that the tree vIEw is not nil, and only then calls Destroy.
FindNextToSelect
function FindNextToSelect: TTreeNode; virtual;
Starting with the node indicated by the Selected property, FindNextToSelect searches for a deselected node, with preference given to subsequent siblings. If the Selected node and all its siblings are selected, the search proceeds to the parent node and its siblings, and so on up the tree. If the top level is reached without finding a deselected node, nil is returned.
Note that the Selected property can refer to a deselected node when MultiSelect is true. In this case, FindNextToSelect returns that node.
FullCollapse
procedure FullCollapse;
Call FullCollapse to hide all the nodes in the tree vIEw except those at the first level.
FullExpand
procedure FullExpand;
Call FullExpand to display all the nodes in the tree vIEw. If ShowButtons is true, all buttons will change from '+' to '-'.
GetHitTestInfoAt
function GetHitTestInfoAt(X, Y: Integer): THitTests;
Call GetHitTestInfoAt to determine what portion of the tree view, if any, sits under the point specified by the X and Y parameters. For example, use GetHitTestInfoAt to provide feedback about how to expand or collapse nodes when the mouse is over the relevant portions of the tree vIEw.
GetHitTestInfo returns a THitTests type. This set describes the possible tree vIEw elements under the mouse.
GetNodeAt
function GetNodeAt(X, Y: Integer): TTreeNode;
Call GetNodeAt to Access the node at the position specified by the X and Y parameters. X and Y specify the position in pixels relative to the top left corner of the tree vIEw. If there is no node at the location, GetNodeAt returns nil (
GetSelections
function GetSelections(AList: TList): TTreeNode;
GetSelections clears AList and copIEs all selected node objects to it. The return value is the Selected property.
IsEditing
function IsEditing: Boolean;
IsEditing returns true if any node label in the tree vIEw is being edited.
LoadFromFile
procedure LoadFromFile(const FileName: string);
Use the LoadFromFile method to retrieve tree view nodes from a file and add them to the tree view. FileName should reference a file that was saved using the SaveToFile method. Tree view files are text files that list the Text property of each node in a tree vIEw, one node per line. Each node text value is preceded by spaces to indicate the depth of nesting.
Note: Tree view files contain only the text of the tree vIEw nodes. They do not contain any data associated with the nodes.
LoadFromStream
procedure LoadFromStream(Stream: TStream);
Use LoadFromStream to read the nodes of the tree view from the specified stream. LoadFromStream adds the nodes in the stream data to the tree vIEw.
The format for tree vIEw data used by the SaveToStream and LoadFromStream methods lists the Text property for each node. Data associated with the nodes is not saved or loaded.
For example, an application can save the information displayed in a tree view as the data in a Binary Large Object (BLOB) field of a dataset using the SaveToStream method. LoadFromStream can later retrIEve the data using a BLOB stream.
SaveToFile
procedure SaveToFile(const FileName: string);
Use the SaveToFile method to store tree view data to a text file. The nodes can later be reloaded from the file into a new tree vIEw object using the LoadFromFile method.
Tree view files are text files that list the Text property of each node in a tree vIEw, one node per line. Each node text value is preceded by spaces to indicate the depth of nesting.
Note: Tree view files contain only the text of the tree vIEw nodes. They do not contain any data associated with the nodes.
SaveToStream
procedure SaveToStream(Stream: TStream);
Use the SaveToStream method to stream out the nodes in a tree view. It can be streamed back in to another tree vIEw object using the LoadFromStream method.
The format for tree vIEw data used by the SaveToStream and LoadFromStream methods lists the Text property for each node. Data associated with the nodes is not saved or loaded.
Select
procedure Select(const Nodes: array of TTreeNode); overload; virtual;
procedure Select(Nodes: TList); overload; virtual;
procedure Select(Node: TTreeNode; ShiftState: TShiftState = []); overload; virtual;
The select method selects one or more tree nodes.
In the first two forms, all nodes in the Nodes parameter are selected. Any other selected nodes are deselected.
In the third form, Node is selected as if clicked with the mouse. To achIEve the effect of using the Control key, the Shift key, or the right mouse button, include ssCtrl, ssShift, or ssRight in the ShiftState parameter.
Subselect
procedure Subselect(Node: TTreeNode; Validate: Boolean = False); virtual;
Subselect toggles the selection state of Node. If Validate is true, Subselect ensures that the selection state information in vIEw, list, and node objects are consistent, and that only those nodes allowed by MultiSelectStyle are actually selected.
If Subselect is called when MultiSelect is false, ETreeVIEwError is raised.