Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default TreeView mousemove event

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,489
Default TreeView mousemove event

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

Karim Benabd wrote:
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


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default TreeView mousemove event

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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default TreeView mousemove event

Hi Andy,

I used a conversion function published by Microsoft with which it is
possible to convert twips (Treeview output) into pixels used by the
MouseMove event.

However, the values given by the conversion function are not accurate.
Here is my code:

Here is the function:

================================================== =
Option Explicit:
'Declarations
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
'Constants
Const WU_LOGPIXELSX = 88
Const WU_LOGPIXELSY = 90
'Function
Function ConvertTwipsToPixels(lngTwips As Long, lngDirection As Long)
As Long
'Handle to device
Dim lngDC As Long
Dim lngPixelsPerInch As Long
Const nTwipsPerInch = 1440
lngDC = GetDC(0)

If (lngDirection = 0) Then 'Horizontal
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSX)
Else 'Vertical
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSY)
End If
lngDC = ReleaseDC(0, lngDC)
ConvertTwipsToPixels = (lngTwips / nTwipsPerInch) * lngPixelsPerInch
End Function

Here is the code inside the userform with a TreeView and 3 Labels:

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
X_Pixels = ConvertTwipsToPixels(X, 0)
Y_Pixels = ConvertTwipsToPixels(Y, 1)
Set nodHoover = TreeView1.HitTest(X_Pixels, Y_Pixels)
Label2.Caption = X & " pix," & X_Pixels & " pix,"
Label3.Caption = Y & " pix," & Y_Pixels & " pix,"
If Not nodHoover Is Nothing Then
Label1.Caption = nodHoover.Text
End If
End If
End Sub

Private Sub UserForm_Initialize()
Dim NodX As Node
Dim L As Long
With TreeView1
..Nodes.Clear
Set NodX = .Nodes.Add(, , "Root", "I am Root")
NodX.Expanded = True
Set NodX = Nothing
Set NodX = .Nodes.Add("Root", tvwChild, "XX", "Item XX")
NodX.Expanded = True
Set NodX = Nothing
For L = 1 To 3
Set NodX = .Nodes.Add("XX", tvwChild, "X" & L, "Item X" & L)
NodX.Expanded = True
Set NodX = Nothing
Next
End With
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
Label1.Caption = "UserForm"
End Sub
================================================== =

Could you please help me?

Thanks,
Karim

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,489
Default TreeView mousemove event

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

Karim Benabd wrote:
Hi Andy,

I used a conversion function published by Microsoft with which it is
possible to convert twips (Treeview output) into pixels used by the
MouseMove event.

However, the values given by the conversion function are not accurate.
Here is my code:

Here is the function:

================================================== =
Option Explicit:
'Declarations
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
'Constants
Const WU_LOGPIXELSX = 88
Const WU_LOGPIXELSY = 90
'Function
Function ConvertTwipsToPixels(lngTwips As Long, lngDirection As Long)
As Long
'Handle to device
Dim lngDC As Long
Dim lngPixelsPerInch As Long
Const nTwipsPerInch = 1440
lngDC = GetDC(0)

If (lngDirection = 0) Then 'Horizontal
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSX)
Else 'Vertical
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSY)
End If
lngDC = ReleaseDC(0, lngDC)
ConvertTwipsToPixels = (lngTwips / nTwipsPerInch) * lngPixelsPerInch
End Function

Here is the code inside the userform with a TreeView and 3 Labels:

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
X_Pixels = ConvertTwipsToPixels(X, 0)
Y_Pixels = ConvertTwipsToPixels(Y, 1)
Set nodHoover = TreeView1.HitTest(X_Pixels, Y_Pixels)
Label2.Caption = X & " pix," & X_Pixels & " pix,"
Label3.Caption = Y & " pix," & Y_Pixels & " pix,"
If Not nodHoover Is Nothing Then
Label1.Caption = nodHoover.Text
End If
End If
End Sub

Private Sub UserForm_Initialize()
Dim NodX As Node
Dim L As Long
With TreeView1
.Nodes.Clear
Set NodX = .Nodes.Add(, , "Root", "I am Root")
NodX.Expanded = True
Set NodX = Nothing
Set NodX = .Nodes.Add("Root", tvwChild, "XX", "Item XX")
NodX.Expanded = True
Set NodX = Nothing
For L = 1 To 3
Set NodX = .Nodes.Add("XX", tvwChild, "X" & L, "Item X" & L)
NodX.Expanded = True
Set NodX = Nothing
Next
End With
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
Label1.Caption = "UserForm"
End Sub
================================================== =

Could you please help me?

Thanks,
Karim


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default TreeView mousemove event

Thanks Andy. It works fine and deserves 5 stars.

Cheers,
Karim

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MouseMove & Click event over an image control furbiuzzu Excel Programming 1 June 30th 06 09:46 AM
EMBEDing an object to access procedures such as MouseMove MarkBradshaw Excel Programming 0 October 7th 05 11:52 AM
Chart - display point details on MouseMove event mangesh_yadav[_30_] Excel Programming 5 July 27th 04 03:09 PM
TreeView marijan glavac[_2_] Excel Programming 2 April 30th 04 07:28 AM
MouseMove causes Excel Crash Pal Excel Programming 2 January 31st 04 01:36 PM


All times are GMT +1. The time now is 11:00 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"