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 button that shows its borders only when the mouse is over it by using capture
DescriptionThis example shows how to make a button that shows its borders only when the mouse is over it using capture in Visual Basic 6. It uses the SetCapture API function to detect when the user moves the mouse off of the button.
Keywordsbutton, mouse, border, capture
CategoriesControls
 
When the mouse moves over the button, the program uses the SetCapture API function to make future mouse events go to the button control. When the mouse leaves the button, it uses ReleaseCapture to release the capture.

Note that Windows sets and releases capture when you click on the button. The program sets its m_MouseCaptured flag to False when this happens so it knows that the mouse is no longer captured.

 
Private Declare Function SetCapture Lib "user32" (ByVal _
    hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As _
    Long

Private m_MouseCaptured As Boolean

Private Sub Picture1_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    ' See if the mouse is over the PictureBox.
    If X < 0 Or X > Picture1.Width Or _
       Y < 0 Or Y > Picture1.Height _
    Then
        ' The mouse is not over the PictureBox.
        ' Release the capture.
        ReleaseCapture
        Picture1.BorderStyle = vbBSNone
        m_MouseCaptured = False
    Else
        ' The mouse is over the PictureBox.
        ' See if we already have the mouse captured.
        If Not m_MouseCaptured Then
            ' The mouse is not captured. Capture it.
            m_MouseCaptured = True
            SetCapture Picture1.hwnd
            Picture1.BorderStyle = vbFixedSingle
        End If
    End If
End Sub

Private Sub Picture1_Click()
    ' Release the capture.
    Picture1_MouseMove 0, 0, -1, -1

    ' Click.
    MsgBox "Clicked"
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated