1. Home
  2. Computing & Technology
  3. Delphi Programming

Display Custom TTreeView Item Hints
A Hint for A Node

By Zarko Gajic, About.com Guide

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) ;
var
  tree: TTreeView;
  hoverNode: TTreeNode;
  hitTest : THitTests;
  //ht : THitTest;
begin
  if (Sender is TTreeView) then
    tree := TTreeView(Sender)
  else
    Exit;

  hoverNode := tree.GetNodeAt(X, Y) ;
  hitTest := tree.GetHitTestInfoAt(X, Y) ;

(*
  //list the hitTest values
  Caption := '';
  for ht in hitTest do
  begin
    Caption := Caption + GetEnumName(TypeInfo(THitTest), integer(ht)) + ', ';
  end;
  *)


  if (lastHintNode <> hoverNode) then
  begin
    Application.CancelHint;

    if (hitTest <= [htOnItem, htOnIcon, htOnLabel, htOnStateIcon]) then
    begin
      lastHintNode := hoverNode;
      tree.Hint := NodeHint(hoverNode) ;
    end;
  end;
end;

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;
begin
  result := Format('Node absolute index: %d',[tn.AbsoluteIndex]) ;
end;

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.

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TTreeView
  6. Display Custom TTreeView Item Hints

©2009 About.com, a part of The New York Times Company.

All rights reserved.