Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a label display a shadow safely when the mouse is over it
Keywordslabel, shadow, mouse, GetCursorPos, ScreenToClient
CategoriesGraphics, Controls, Tips and Tricks, API
 

Make a Label for the shadow with BackStyle set to Transparent. Copy and paste the Label and change the color for the text. Position the text label slightly offset from the shadow label's position. Set the shadow Label's Visible property to False.

In the text Label's MouseMove event handler, see if the shadow label is already visible. If it is not, make it visible and enable the Timer tmrCheckMouse.

In the Timer's event handler, use GetCursorPos to get the mouse's current position. Use ScreenToClient to convert that position into form coordinates in pixels. Note that the form's ScaleMode is vbPixels or else you would need to convert the mouse position.

 
Private Sub lblText_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    ' See if the shadow is visible.
    If Not lblShadow.Visible Then
        ' The shadow is not visible.
        ' Display the shadow.
        lblShadow.Visible = True

        ' Enable the timer.
        tmrCheckMouse.Enabled = True
    End If
End Sub

' See if the mouse is still over the label.
Private Sub tmrCheckMouse_Timer()
Dim pt As POINTAPI

    ' Get the cursor's position in screen coordinates.
    GetCursorPos pt

    ' Convert to form coordinates.
    ' Note that this converts the position in pixels
    ' and the form's ScaleMode is vbPixels.
    ScreenToClient hwnd, pt

    ' See if the point is under the text label.
    If pt.X < lblText.Left Or pt.X > lblText.Left + _
        lblText.Width Or _
       pt.Y < lblText.Top Or pt.Y > lblText.Top + _
           lblText.Height _
    Then
        ' The mouse is not over the label.
        ' Hide the shadow.
        lblShadow.Visible = False

        ' Disable the timer.
        tmrCheckMouse.Enabled = False
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated