Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Very Urgent - Node coordinates in a TreeView

50 views
Skip to first unread message

Shanthi

unread,
Feb 11, 2002, 2:00:59 AM2/11/02
to
Is there any way to get particular Node co-ordinates (x,y) in a Treeview
control?

-Shanthi


dnagel

unread,
Feb 11, 2002, 2:20:13 AM2/11/02
to
If you have the X,Y like in the Mouse Down, Over or Up
events, use Treeview.HitTest method to return a node
reference to the item you are over.

Otherwise, NodeClick will return a node object as one of
the parameters in the event.

Can you be more specific?

D.

"Shanthi" <s...@srasys.co.in> wrote in message
news:#ep$0lssBHA.2524@tkmsftngp04...

Shanthi

unread,
Feb 13, 2002, 11:34:56 PM2/13/02
to
Dear dnagel,

Thanks for your reply. Here I have explained my problem
properly.

I want to show one popup menu during right clicking of a node in a Treeview.

In Treeview - MouseUp event using PopupMenu I am able to display the popup
menu in a correct (Current mousepointer location) location. Because MouseUp
event gives the mouse pointer current location (X,Y) so that we can display
the Menu based on the X,Y Values.
The same functionlity I want to do it through keyboard also.

In the KeyDown event I can catch the equivalent (RightClick) key & using the
Popup Menu method I can display the Popup Menu. In the above case I know the
X,Y values. Here I am unable to get it. It is not showing the Popup in the
proper location. I tried "GetCursorPos" to get X, Y values. It is returing
the same values for all the nodes. I need to get the X,Y Co-ordinates of the
currently selected node.

How can I get the X,Y co-ordinates of the currently selected node?

Hope I am clear now.

Expecting your suggestion.


Regards,
-Shanthi

"dnagel" <Grand...@hotmail.com> wrote in message
news:eYTs$xssBHA.1740@tkmsftngp04...

Randy Birch

unread,
Feb 14, 2002, 12:07:23 AM2/14/02
to
The x.y parameters are optional .. if you omit them the menu will appear at
the cursor.

--

Randy Birch
MVP Visual Basic

http://www.mvps.org/vbnet/

Please respond only to the newsgroups so all can benefit.

*** If you call the Sleep API in Bill's latest, will it have .NET dreams?
***


"Shanthi" <s...@srasys.co.in> wrote in message

news:eOxdOCRtBHA.2052@tkmsftngp07...

Shanthi

unread,
Feb 14, 2002, 12:36:37 AM2/14/02
to
Yha. As u said x,y parameters are optional. If I use mouse and select the
node then no problem. Suppose if I use keyup or keydown to traverse the node
and if I do right clik the cursor will be in different position. In this
case definitely I need to get current node X, Y coordinates.

Am i clear?


"Randy Birch" <r...@mvps.org> wrote in message
news:eodfHWRtBHA.1672@tkmsftngp04...

Rocky Clark

unread,
Feb 14, 2002, 8:29:54 PM2/14/02
to
Here's some code that will do exactly what you want.

'***** Begin Copy *****
Option Explicit

Private Const TV_FIRST = &H1100
Private Const TVM_GETITEMRECT = (TV_FIRST + 4)
Private Const TVM_GETNEXTITEM = (TV_FIRST + 10)
Private Const TVGN_CARET = &H9
Private Const TVGN_ROOT = &H0

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long

Private Function GetSelectedNodeCoords( _
tvwTree As MSComctlLib.TreeView) As RECT

Dim hNode As Long
Dim rcBox As RECT

If Not tvwTree.SelectedItem Is Nothing Then
'Node must be fully visible to get coords.
tvwTree.SelectedItem.EnsureVisible

'Get a handle to the currently selected node
hNode = SendMessage(tvwTree.hWnd, _
TVM_GETNEXTITEM, TVGN_CARET, ByVal TVGN_ROOT)

'Set the first four bytes (Long) of the
'rect to the handle of the node, before
'calling TVM_GETITEMRECT.
rcBox.Left = hNode

'Get the coordinates of the node
If SendMessage(tvwTree.hWnd, _
TVM_GETITEMRECT, True, rcBox) = 0 Then
'Zero the rect to show failure
With rcBox
.Left = 0
.Top = 0
.Right = 0
.Bottom = 0
End With
End If

End If

'Return the rect
GetSelectedNodeCoords = rcBox

End Function

'***** End Copy *****

***** Sample to call this Code: *****
Private Sub Command1_Click()

Dim rcBox As RECT

rcBox = GetSelectedNodeCoords(tvwMain)

With rcBox
If .Right > 0 Then
Debug.Print .Left, .Top, .Right, .Bottom
Else
Debug.Print "Failed to retrieve coordinates"
End With

End Sub

***** End Sample Code *****

Rocky Clark (Kath-Rock Software)

"Shanthi" <s...@srasys.co.in> wrote in message
news:#ep$0lssBHA.2524@tkmsftngp04...

Rocky Clark

unread,
Feb 14, 2002, 8:54:39 PM2/14/02
to
Oops! That's what I get for coding in my email...

Add the End If in this section of code.


With rcBox
If .Right > 0 Then
Debug.Print .Left, .Top, .Right, .Bottom
Else
Debug.Print "Failed to retrieve coordinates"

End If
End With

Sorry about that,
Rocky

"Rocky Clark" <so...@nosuchluck.com> wrote in message
news:mwZa8.159256$jO5.18...@typhoon.tampabay.rr.com...

Brad Martinez

unread,
Feb 14, 2002, 10:08:26 PM2/14/02
to
Rocky,

> 'Get a handle to the currently selected node
> hNode = SendMessage(tvwTree.hWnd, _
> TVM_GETNEXTITEM, TVGN_CARET, ByVal TVGN_ROOT)

You only need to specify the message's lParam when using
a flag that returns an item handle relative to the lParam, as in:

TVGN_CHILD
TVGN_NEXT
TVGN_PREVIOUS
TVGN_PARENT
TVGN_NEXTVISIBLE
TVGN_PREVIOUSVISIBLE.

The message's the lParam is ignores for the flags that return an
"absolute" item handle:

TVGN_FIRSTVISIBLE
TVGN_CARET
TVGN_DROPHILITE
TVGN_ROOT

The treeview's pre-defined macros prevents this potential confusion
while making things much more readable...

Public Function TreeView_GetNextItem(hwnd As Long, hItem As Long, flag As Long) As Long
TreeView_GetNextItem = SendMessage(hwnd, TVM_GETNEXTITEM, ByVal flag, ByVal hItem)
End Function

Public Function TreeView_GetSelection(hwnd As Long) As Long
TreeView_GetSelection = TreeView_GetNextItem(hwnd, 0, TVGN_CARET)
End Function

--
Brad Martinez, http://www.mvps.org
Please direct questions/replies to the newsgroup

Shanthi

unread,
Feb 15, 2002, 2:03:56 AM2/15/02
to
Dear Rocky

Thanks a lot for your timely help. It is working fine.

-Regards
Shanthi

"Rocky Clark" <so...@nosuchluck.com> wrote in message
news:mwZa8.159256$jO5.18...@typhoon.tampabay.rr.com...

Rocky Clark

unread,
Feb 15, 2002, 7:00:20 AM2/15/02
to
Nice, thanks Brad! I found the call in the MSDN somewhere and I wondered
myself, why TVGN_ROOT? Then I realized that TVGN_ROOT = 0, so it's doing the
same thing as your code, except not nearly as readable. I'll switch my
routines over to your suggestions. Much better!

Rocky


"Brad Martinez" <bt...@msn.com.nospam> wrote in message
news:u6veD4ctBHA.1652@tkmsftngp03...

0 new messages