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
 
 
 
 
TitleDisplay a message when the mouse is over a button
Keywordsbutton, message, mouse over
CategoriesControls, API, Tips and Tricks
 
Use the button's MouseMove event to display the message. Use a timer and the GetCursorPos API function to tell when the mouse has moved away.
 
Private Sub Command1_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    DisplayButtonMessage Command1, "Command 1"
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 DisplayButtonMessage(ByVal cmd As _
    CommandButton, ByVal txt As String)
    ' See if we are already displaying a message.
    If Timer1.Enabled Then Exit Sub

    ' Calculate the button's screen coordinates.
    upper_left.X = ScaleX(cmd.Left, ScaleMode, vbPixels)
    upper_left.Y = ScaleY(cmd.Top, ScaleMode, vbPixels)
    ClientToScreen hwnd, upper_left
    lower_right.X = ScaleX(cmd.Left + cmd.Width, ScaleMode, _
        vbPixels)
    lower_right.Y = ScaleY(cmd.Top + cmd.Height, ScaleMode, _
        vbPixels)
    ClientToScreen hwnd, lower_right

    ' Display the message and enable the timer.
    lblStatus.Caption = txt
    Timer1.Enabled = True
End Sub

' See if the cursor has moved off the button.
Private Sub Timer1_Timer()
Dim pt As POINTAPI

    ' Get the cursor position.
    GetCursorPos pt

    ' See if the mouse is over the button.
    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.
        Timer1.Enabled = False
        lblStatus.Caption = ""
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated