Take the 2-minute tour ×
Private Sub framePDF_MouseMove(ByVal... )
framePDF.BackColor = &H80000012&  

So, the frame's color is changing.
I can't find the event to return the color back - when the cursor is away from the frame ?

share|improve this question
Is it possible to add a frame slightly larger than the label, position it behind the label and add a MouseMove event to the frame? You can then detect then query the label colour to determine if the mouse is leaving or entering the label. –  ooo Aug 30 '12 at 16:16
I think, I understood. Will try, thanks –  Alegro Aug 30 '12 at 16:22
add comment

2 Answers

up vote 2 down vote accepted

In vba and VB6 there is no MouseLeave event.

The best way to achieve this is to start a timer when the mouse enters the frame.

Then in the timer code check to see if the mouse pointer is still within the bounds of the frame. If not change the colour back and stop the timer

Put this code in a module:

Public Declare Function GetCursorPos Lib "user32" (lpPoint As _
   POINTAPI) As Long

Public Type POINTAPI
        x As Long
        y As Long
End Type

Create a timer on your form, set interval =10 Enbaled = False

Then the code looks something like this:

Private Sub frameTest_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    frameTest.BackColor = vbRed
    tmrMouseLeave.Enabled = True
End Sub

Private Sub tmrMouseLeave_Timer()
    Dim pt As POINTAPI
    Call GetCursorPos(pt)
    Dim xValue As Long, yValue As Long
    xValue = pt.x * Screen.TwipsPerPixelX
    yValue = pt.y * Screen.TwipsPerPixelY

    If (xValue > (Me.Left + frameTest.Left)) And _
       (xValue < (Me.Left + frameTest.Left + frameTest.width)) And _
       (yValue > (Me.Top + frameTest.Top)) And _
       (yValue < (Me.Top + frameTest.Top + frameTest.height)) Then
        'we are still inside the frame
        'mouse is outside the frame
        frameTest.BackColor = vbBlue
        tmrMouseLeave.Enabled = False
    End If
End Sub
share|improve this answer
UnBelievable ! What is then the purpose of MouseMove ? Will the timer slow the code execution ? Could You please write a short code example for the timer ? –  Alegro Aug 30 '12 at 15:57
Thanks, Matt. I will try –  Alegro Aug 30 '12 at 16:18
add comment

On a Userform? The Userform also has a MouseMove event that doesn't fire when you're in the Frame.

Private Sub Frame1_MouseMove(ByVal ...)

    Me.Frame1.BackColor = vbRed

End Sub

Private Sub UserForm_MouseMove(ByVal ...)

    Me.Frame1.BackColor = vbWhite

End Sub

will turn the frame red when you're over it and white when you're not. These events fire constantly, so use them judiciously.

share|improve this answer
Dick, excelent, thanks a lot –  Alegro Sep 3 '12 at 7:51
add comment

Your Answer


