property Tree.ItemFromPoint (X as OLE_XPOS_PIXELS, Y as OLE_YPOS_PIXELS, ColIndex as Long, HitTestInfo as HitTestInfoEnum) as HITEM

Retrieves the item from the cursor.

 TypeDescription 
   X as OLE_XPOS_PIXELS A single that specifies the current X location of the mouse pointer. The x values is always expressed in client coordinates.  
   Y as OLE_YPOS_PIXELS A single that specifies the current Y location of the mouse pointer. The y values is always expressed in client coordinates.  
   ColIndex as Long A long expression that indicates on return, the column where the point belongs. If the return value is zero, the ColIndex may indicate the handle of the cell ( inner cell ).  
   HitTestInfo as HitTestInfoEnum A HitTestInfoEnum expression that determines on return, the position of the cursor within the cell. If the HitTestInfo value includes the exHTCellIcon or exHTCellCaptionIcon, the four most significant bytes indicates the index of the icon from the point.   
   HITEM A long expression that indicates the item's handle where the point is.  

Use the ItemFromPoint property to get the item from the point specified by the {X,Y}. The X and Y coordinates are expressed in client coordinates, so a conversion must be done in case your coordinates are relative to the screen or to other window. If the X parameter is -1 and Y parameter is -1 the ItemFromPoint property determines the handle of the item from the cursor. Use the ColumnFromPoint property to retrieve the column from cursor. Use the SelectableItem property to specify the user can select an item.

The following VB sample prints the cell's caption from the cursor ( if the control contains no inner cells. Use the SplitCell property to insert inner cells ) :

Private Sub Tree1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    On Error Resume Next
    ' Converts the container coordinates to client coordinates
    X = X / Screen.TwipsPerPixelX
    Y = Y / Screen.TwipsPerPixelY
    Dim h As HITEM
    Dim c As Long
    Dim hit As EXTREELibCtl.HitTestInfoEnum
    ' Gets the item from (X,Y)
    h = Tree1.ItemFromPoint(X, Y, c, hit)
    If Not (h = 0) Then
        Debug.Print Tree1.Items.CellCaption(h, c) & " HT = " & hit
    End If
End Sub

The following VB sample displays the cell's caption from the cursor ( if the control contains inner cells ):

Private Sub Tree1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    On Error Resume Next
    ' Converts the container coordinates to client coordinates
    X = X / Screen.TwipsPerPixelX
    Y = Y / Screen.TwipsPerPixelY
    Dim h As HITEM
    Dim c As Long
    Dim hit As EXTREELibCtl.HitTestInfoEnum
    ' Gets the item from (X,Y)
    h = Tree1.ItemFromPoint(X, Y, c, hit)
    If Not (h = 0) Or Not (c = 0) Then
        Debug.Print Tree1.Items.CellCaption(h, c) & " HT = " & hit
    End If
End Sub

The following VB sample displays the index of icon being clicked:

Private Sub Tree1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim i As HITEM, h As HitTestInfoEnum, c As Long
    With Tree1
        i = .ItemFromPoint(X / Screen.TwipsPerPixelX, Y / Screen.TwipsPerPixelY, c, h)
    End With
    If (i <> 0) or ( c <> 0 ) Then
        If exHTCellIcon = (h And exHTCellIcon) Then
            Debug.Print "The index of icon being clicked is: " & (h And &HFFFF0000) / 65536
        End If
    End If
End Sub

The following C# sample displays the caption of the cell being double clicked ( including the inner cells ): 

	EXTREELib.HitTestInfoEnum hit;
	int c = 0, h = axTree1.get_ItemFromPoint( e.x, e.y, out c, out hit );
	if ( ( h != 0 ) || ( c != 0 ) )
		MessageBox.Show( axTree1.Items.get_CellCaption( h, c ).ToString() );

The following VC sample displays the caption of the cell being clicked:

#include "Items.h"

static CString V2S( VARIANT* pv, LPCTSTR szDefault = _T("") )
{
	if ( pv )
	{
		if ( pv->vt == VT_ERROR )
			return szDefault;

		COleVariant vt;
		vt.ChangeType( VT_BSTR, pv );
		return V_BSTR( &vt );
	}
	return szDefault;
}

void OnMouseDownTree1(short Button, short Shift, long X, long Y) 
{
	long c = 0, hit = 0, hItem = m_tree.GetItemFromPoint( X, Y, &c, &hit );
	if ( ( hItem != 0 ) || ( c != 0 ) )
	{
		CItems items = m_tree.GetItems();
		COleVariant vtItem( hItem ), vtColumn( c );
		CString strCaption = V2S( &items.GetCellCaption( vtItem, vtColumn ) ), strOutput;
		strOutput.Format( "Cell: '%s', Hit = %08X\n", strCaption, hit );
		OutputDebugString( strOutput );
	}
}

The following VB.NET sample displays the caption from the cell being clicked:

Private Sub AxTree1_MouseDownEvent(ByVal sender As Object, ByVal e As AxEXTREELib._ITreeEvents_MouseDownEvent) Handles AxTree1.MouseDownEvent
    With AxTree1
        Dim i As Integer, c As Integer, hit As EXTREELib.HitTestInfoEnum
        i = .get_ItemFromPoint(e.x, e.y, c, hit)
        If (Not (i = 0) Or Not (c = 0)) Then
            Debug.WriteLine("Cell: " & .Items.CellCaption(i, c) & " Hit: " & hit.ToString())
        End If
    End With
End Sub

The following C# sample displays the caption from the cell being clicked:

private void axTree1_MouseDownEvent(object sender, AxEXTREELib._ITreeEvents_MouseDownEvent e)
{
	int c = 0;
	EXTREELib.HitTestInfoEnum hit;
	int i = axTree1.get_ItemFromPoint( e.x, e.y, out c,out hit );
	if ( ( i != 0 ) || ( c != 0 ) )
	{
		string s = axTree1.Items.get_CellCaption( i,c ).ToString();
		s = "Cell: " + s + ", Hit: " + hit.ToString();
		System.Diagnostics.Debug.WriteLine( s );
	}
}

The following VFP sample displays the caption from the cell being clicked ( the code should be in the Tree1.MouseDown event ):

*** ActiveX Control Event ***
LPARAMETERS button, shift, x, y

local c, hit
c = 0
hit = 0
with thisform.Tree1
	.Items.DefaultItem = .ItemFromPoint( x, y, @c, @hit )
	if ( .Items.DefaultItem <> 0 ) or ( c <> 0 )
		wait window nowait .Items.CellCaption( 0, c ) + " " + Str( hit )
	endif
endwith

Send comments on this topic.
1999-2015 Exontrol.COM, Software. All rights reserved.