RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomTreeView.GetHitTestInfoAt Method

Returns information about the location of a point relative to the client area of the tree view control.

Pascal
function GetHitTestInfoAt(X: Integer; Y: Integer): THitTests;
C++
__fastcall THitTests GetHitTestInfoAt(int X, int Y);

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.  

C++ Examples: 

 

/*
The following code checks a list of htHitTest types and adds
the ones that occur when the mouse was pressed while on the
tree view.
*/
void __fastcall TForm1::TVMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y)
{
  THitTests MyHitTest = TV->GetHitTestInfoAt(X,Y);
  if (MyHitTest.Contains(htNowhere))
    ListBox1->Items->Add("Nowhere");
  if (MyHitTest.Contains(htOnItem))
    ListBox1->Items->Add("OnItem");
  if (MyHitTest.Contains(htOnButton))
    ListBox1->Items->Add("OnButton");
  if (MyHitTest.Contains(htOnIndent))
    ListBox1->Items->Add("OnIndent");
  if (MyHitTest.Contains(htOnLabel))
    ListBox1->Items->Add("OnLabel");
  if (MyHitTest.Contains(htOnRight))
    ListBox1->Items->Add("OnRight");
  if (MyHitTest.Contains(htAbove))
    ListBox1->Items->Add("Above");
  if (MyHitTest.Contains(htBelow))
    ListBox1->Items->Add("Below");
  if (MyHitTest.Contains(htOnIcon))
    ListBox1->Items->Add("OnIcon");
  if (MyHitTest.Contains(htOnStateIcon))
    ListBox1->Items->Add("OnStateIcon");
  if (MyHitTest.Contains(htToLeft))
    ListBox1->Items->Add("ToLeft");
  if (MyHitTest.Contains(htToRight))
    ListBox1->Items->Add("ToRight");
}
/*
The following code uses GetNodeAt to add a dragged node as a
child of the node under the mouse when it is dropped.  This
example requires a populated TreeView.  Also, the TTreeView
DragMode property set to dmAutomatic, and the TTreeView
OnDragOver event handler must be implemented to accept the
drop.
*/
void __fastcall TForm1::TreeView1DragDrop(TObject *Sender, TObject *Source,
      int X, int Y)
{
  if (Source != dynamic_cast<TObject *>(TreeView1) || TreeView1->Selected == NULL)
    return;
  THitTests HT = TreeView1->GetHitTestInfoAt(X, Y);
  TNodeAttachMode AttachMode;
  TTreeNode *Item = TreeView1->GetNodeAt(X, Y);
  if (HT.Contains(htOnItem) || HT.Contains(htOnIcon))
    AttachMode = naAddChild;
  else if (HT.Contains(htNowhere))
    AttachMode = naAdd;
  else if (HT.Contains(htOnIndent))
    AttachMode = naInsert;
  else
    return;
  TreeView1->Selected->MoveTo(Item, AttachMode);
}

void __fastcall TForm1::TreeView1DragOver(TObject *Sender, TObject *Source,
      int X, int Y, TDragState State, bool &Accept)
{
  Accept = (Source == dynamic_cast<TObject *>(TreeView1));
}

 

Delphi Examples: 

{
The following code checks a list of htHitTest types and adds
the ones that occur when the mouse was pressed while on the
tree view.
}
procedure TForm1.TVMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  MyHitTest : THitTests;
begin
  MyHitTest := TV.GetHitTestInfoAt(X,Y);
  if htNowhere in MyHitTest then
    ListBox1.Items.Add('NoWhere');
  if htOnItem in MyHitTest then
    ListBox1.Items.Add('OnItem');
  if htOnButton in MyHitTest then
    ListBox1.Items.Add('OnButton');
  if htOnIndent in MyHitTest then
    ListBox1.Items.Add('OnIndent');
  if htOnLabel in MyHitTest then
    ListBox1.Items.Add('OnLabel');
  if htOnRight in MyHitTest then
    ListBox1.Items.Add('OnRight');
end;
{
The following code uses GetNodeAt to add a dragged node as a
child of the node under the mouse when it is dropped.  This
example requires a populated TreeView.  Also, the TTreeView
DragMode property must be set to dmAutomatic, and the TTreeView
OnDragOver event handler must be implemented to accept the
drop.
}
procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var 
  AnItem: TTreeNode;
  AttachMode: TNodeAttachMode;
  HT: THitTests;
begin
  if TreeView1.Selected = nil then Exit;
  HT := TreeView1.GetHitTestInfoAt(X, Y);
  AnItem := TreeView1.GetNodeAt(X, Y);
  if (HT - [htOnItem, htOnIcon, htNowhere, htOnIndent] <> HT) then 
  begin
    if (htOnItem in HT) or (htOnIcon in HT) then
      AttachMode := naAddChild
    else if htNowhere in HT then AttachMode := naAdd
    else if htOnIndent in HT then AttachMode := naInsert;
    TreeView1.Selected.MoveTo(AnItem, AttachMode); 
  end;
end;

procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := Source is TTreeView;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!