Hi there, OK I have a small project that buggers me!
The project has been attached!
My Question is why the message is not being displayed like the 4 buttons and 4 labels show the message.
When moving the mouse pointer over the objects, a message displays and when the mouse is not over the message dissaperears, but the difference is that under the Picture Label is a Picture Box and when you move you're mouse over this label the efects are not the same than the other 8 objects and I want it to be the same.
Please help me with this because I want to add this into my big project I'm working with.
Thanks for you're help in advance.
it is because lblAbout is in a PictureBox, thus its position is not in the Form coordinates. When the timer fires, it is comparing the wrong position.
The best solution is to find the container, and add the position of the container(s) to the control. BTW declaring lbl as Control, you need only one method to compute the control position. Here is the new code, that works even with controls nested in multiple containers (e.g. a label in a pticture box, in a picture box, in a picture box etc)
Enjoy
Marco
Code:
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private upper_left As POINTAPI
Private lower_right As POINTAPI
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DisplayControlLabelMessage Command1, "Command 1"
End Sub
Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DisplayControlLabelMessage Command2, "Command 2"
End Sub
Private Sub Command3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DisplayControlLabelMessage Command3, "Command 3"
End Sub
Private Sub Command4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DisplayControlLabelMessage Command4, "Command 4"
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 250
End Sub
' See if the cursor has moved off the button or label.
Private Sub Timer1_Timer()
Dim pt As POINTAPI
' Get the cursor position.
GetCursorPos pt
' See if the mouse is over the button or label.
If pt.X < upper_left.X Or pt.X > lower_right.X Or _
pt.Y < upper_left.Y Or pt.Y > lower_right.Y _
Then
' It is no longer over the button or label.
Timer1.Enabled = False
lblStatus.Caption = ""
End If
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DisplayControlLabelMessage Label1, "Label 1"
End Sub
Private Sub Label2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DisplayControlLabelMessage Label2, "Label 2"
End Sub
Private Sub Label3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DisplayControlLabelMessage Label3, "Label 3"
End Sub
Private Sub Label4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DisplayControlLabelMessage Label4, "Label 4"
End Sub
' If we are not already displaying a message,
' display it and enable the timer to see when
' the mouse moves off of it.
Private Sub DisplayControlLabelMessage(ByVal lbl As Control, ByVal txt3 As String)
' See if we are already displaying a message.
If Timer1.Enabled Then Exit Sub
On Error Resume Next
Dim ct As Control
Dim offset(1) As Single
Set ct = lbl
Do
If TypeOf ct.Container Is Form Then Exit Do
If Err.Number <> 0 Then Exit Do
Set ct = ct.Container
offset(0) = offset(0) + ct.Left
offset(1) = offset(1) + ct.Top
Loop
' Calculate the labels's screen coordinates.
upper_left.X = ScaleX(offset(0) + lbl.Left, ScaleMode, vbPixels)
upper_left.Y = ScaleY(offset(1) + lbl.Top, ScaleMode, vbPixels)
ClientToScreen hwnd, upper_left
lower_right.X = ScaleX(offset(0) + lbl.Left + lbl.Width, ScaleMode, vbPixels)
lower_right.Y = ScaleY(offset(1) + lbl.Top + lbl.Height, ScaleMode, vbPixels)
ClientToScreen hwnd, lower_right
' Display the message and enable the timer.
lblStatus.Caption = txt3
Timer1.Enabled = True
End Sub
Private Sub lblAbout_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DisplayControlLabelMessage lblAbout, "Picture Label"
End Sub
"There are two ways to write error-free programs. Only the third one works."
Unknown