1. Computing

Display Custom TTreeView Item Hints

A Hint for A Node


TTreeNode Custom Hints

TTreeNode Custom Hints

The TTreeView Delphi control wraps the Windows tree view control. TTreeView is comonly used when a hierarhical structure needs to be displayed to the user. By clicking an item, the user can expand and collapse the associated list of subitems.

In complex tree views, you might want to display customized tooltip (hint) for each tree node.

Delphi's TTreeView exposes two properties you use to set the hint window which appears when the mouse hovers (precisely: when the mouse pointer rests momentarily) over a control: Hint and ShowHint

"Unfortunatelly", the value stored in the Hint property will be displayed, for the tree view, no matter over what item the mouse is.

TreeView Item Related Hint

To display different hints for every node in a tree view, you need to change the Hint property of the TTreeView control depending on the item the mouse is over.

The easiest way to accomplish this, is to handle the OnMouseMove event and set the Hint property (for the tree view) by locating the tree node under the mouse.

 //form's private variable: 
lastHintNode : TTreeNode; 
//treeView1 OnMouseMove event handler
 procedure TForm1.TreeView1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer) ;
   tree: TTreeView;
   hoverNode: TTreeNode;
   hitTest : THitTests;
   //ht : THitTest;
   if (Sender is TTreeView) then
     tree := TTreeView(Sender)
   hoverNode := tree.GetNodeAt(X, Y) ;
   hitTest := tree.GetHitTestInfoAt(X, Y) ;
   //list the hitTest values
   Caption := '';
   for ht in hitTest do
     Caption := Caption + GetEnumName(TypeInfo(THitTest), integer(ht)) + ', ';
   if (lastHintNode <> hoverNode) then
     if (hitTest <= [htOnItem, htOnIcon, htOnLabel, htOnStateIcon]) then
       lastHintNode := hoverNode;
       tree.Hint := NodeHint(hoverNode) ;

The GetNodeAt method returns the node that is found at the specified position. Using the X and Y values from the OnMouseMove event, you locate the node "under" the cursor.

By looking at the result of the GetHitTestInfoAt method, you ensure the mouse is actually over a node. The GetHitTestInfoAt determines what portion of the tree view, if any, sits under the point specified by the X and Y parameters.
The result of the GetHitTestInfoAt is a set type value of the THitTests type.

The NodeHint function gets the customized hint for every tree view item. For the sake of simplicity, here's how to show item's index:

 function TForm1.NodeHint(tn: TTreeNode): string;
   result := Format('Node absolute index: %d',[tn.AbsoluteIndex]) ;

Note: in real world situations, you'll want to display more complex tooltips, for example, related to the record stored in the item's Data property. A real example includes Displaying XML (RSS Feed) Data in a TreeView.

Also, you need to make sure the ShowHint property for the treeview control is set to True. Optionaly you might want to Disable Automatic Hint Feature for the TTreeView.

©2014 About.com. All rights reserved.