shows its borders only when the mouse is over it by using capture

Started by nandagopal, May 18, 2009, 04:24 PM

Previous topic - Next topic

nandagopal

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