TreeView mousemove event

K

Karim Benabd

Hi,

I have a TreeView on a userform and I would like to show some
information on a label based on the current node (not selected) over
which the mouse is moving.

I made many searches in googles and could not find a code snippet on
this.

Thanks for your help.

Regards,
Karim
 
A

Andy Pope

Something like this?

Private Sub TreeView1_MouseMove(Button As Integer, _
Shift As Integer, x As Single, y As Single)

Dim nodHoover As Node

If Button = 0 Then
Set nodHoover = TreeView1.HitTest(x, y)
If Not nodHoover Is Nothing Then
Label1.Caption = nodHoover.Text
End If
End If

End Sub

Cheers
Andy
 
K

Karim Benabd

Hi Andy,

I got the following error when trying to run this code in Excel 2000
(Translation from French):

Compiler error:
The procedure declaration does not correspond to the event description
or to the event procedure with the same name.

When I made the event parameters as follows:
Private Sub TreeView1_MouseMove(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As stdole.OLE_XPOS_PIXELS, ByVal Y As
stdole.OLE_YPOS_PIXELS)
Dim nodHoover As Node
If Button = 0 Then
Set nodHoover = TreeView1.HitTest(X, Y)
If Not nodHoover Is Nothing Then
Label1.Caption = nodHoover.Text
End If
End If
End Sub

With this, the error disappeared but the code did not work as expected.
The Label1 shows only the root text.

Thanks for your help.
Karim
 
A

Andy Pope

It's the other way round. You need to take the pixel values in the vba
event and change them to twips which the HitTest function expects.

Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, _
ByVal hdc As Long) As Long
Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, _
ByVal nIndex As Long) As Long

Const HWND_DESKTOP As Long = 0
Const LOGPIXELSX As Long = 88
Const LOGPIXELSY As Long = 90

'--------------------------------------------------
Function TwipsPerPixelX() As Single
'--------------------------------------------------
'Returns the width of a pixel, in twips.
'--------------------------------------------------
Dim lngDC As Long
lngDC = GetDC(HWND_DESKTOP)
TwipsPerPixelX = 1440& / GetDeviceCaps(lngDC, LOGPIXELSX)
ReleaseDC HWND_DESKTOP, lngDC
End Function

'--------------------------------------------------
Function TwipsPerPixelY() As Single
'--------------------------------------------------
'Returns the height of a pixel, in twips.
'--------------------------------------------------
Dim lngDC As Long
lngDC = GetDC(HWND_DESKTOP)
TwipsPerPixelY = 1440& / GetDeviceCaps(lngDC, LOGPIXELSY)
ReleaseDC HWND_DESKTOP, lngDC
End Function

Private Sub TreeView1_MouseMove(ByVal Button As Integer, ByVal Shift As
Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As
stdole.OLE_YPOS_PIXELS)

Dim nodTemp As Node
Dim strText As String

If Button = 0 Then
Set nodTemp = TreeView1.HitTest( _
TwipsPerPixelX * x, TwipsPerPixelY * y)
If Not nodTemp Is Nothing Then
strText = nodTemp.Text
Else
strText = "Nothing"
End If
Me.Caption = "X=" & x & " Y=" & y & " " & strText
End If
End Sub

Cheers
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top