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
 
 
 
 
 
TitleHighlight a control when the mouse is over it
Keywordscontrol, highlight
CategoriesControls, Tips and Tricks
 
Use a Timer control to periodically see if the mouse is over the control. Uses the ScreenToClient and GetCursorPos API functions to determine the cursor's location.
 
' Highlight the control.
Private Sub Label1_MouseMove(Button As Integer, Shift As _
    Integer, x As Single, y As Single)
    If highlighted Then Exit Sub
    highlighted = True
    Label1.ForeColor = vbRed
    Timer1.Enabled = True
End Sub

' See if we should unhighlight the control.
Private Sub Timer1_Timer()
Dim pt As POINTAPI

    ' See where the cursor is.
    GetCursorPos pt
    
    ' Translate into window coordinates.
    ScreenToClient hwnd, pt

    ' See if we're still within the control.
    If pt.x < Label1.Left Or pt.y < Label1.Top Or _
       pt.x > Label1.Left + Label1.Width Or _
       pt.y > Label1.Top + Label1.Height _
    Then
        highlighted = False
        Label1.ForeColor = vbBlack
        Timer1.Enabled = False
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated