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 link label that opens a Web page and that changes appearance when the mouse is over it in Visual Basic 6
DescriptionThis example shows how to make a link label that opens a Web page and that changes appearance when the mouse is over it in Visual Basic 6.
Keywordslink label, LinkLabel, Visual Basic, Web, changing font, hover
CategoriesControls, Internet, API
 
Make a label with the desired ForeColor, MousePointer, and font. When the user clicks on the link, use ShellExecute to open the Web page.
 
' Open the Web page.
Private Sub lblLink_Click()
    ShellExecute ByVal 0&, "open", _
        "http://www.vb-helper.com", _
        vbNullString, vbNullString, _
        SW_SHOWMAXIMIZED
End Sub
 
Also add two hidden labels with ForeColor (and possibly other properties) set to those you want the link to display when the mouse is over it or not over it. In the label's MouseMove event handler, change the link label to use the desired ForeColor and other properties. Enable a timer to see when the mouse leaves the label.
 
Private Sub lblLink_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    ' See if the link has the mouse over color.
    If Not lblLink.ForeColor = lblMouseOverColor.ForeColor _
        Then
        ' Display the color.
        lblLink.ForeColor = lblMouseOverColor.ForeColor
        lblLink.Font.Underline = True

        ' Enable the timer.
        tmrResetLink.Enabled = True
    End If
End Sub
 
When the timer fires, use the GetCursorPos API function to see if the mouse is still over the label and, if it is not, reset the link label's appearance.
 
' See if the mouse is still over the label.
Private Sub tmrResetLink_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 < lblLink.Left Or pt.X > lblLink.Left + _
        lblLink.Width Or _
       pt.Y < lblLink.Top Or pt.Y > lblLink.Top + _
           lblLink.Height _
    Then
        ' The mouse is not over the label.
        ' Restore the original font.
        lblLink.ForeColor = lblMouseOffColor.ForeColor
        lblLink.Font.Underline = False

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