Capture mouse enter and mouse leave events


'Code By Kiran.
'Capture mouse enter and mouse leave events
'As you know there is no mouse enter and mouse leave events in VB classic but you can get these by using following code and API but you have some limitation to use the code, control must have Hdc property; e.g.you can use it for PictureBox and CommandButton but not for Labels

'Windows API Declaration
Public Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetCapture Lib "user32" () As Long
Public Declare Function ReleaseCapture Lib "user32" () As Long

Private Sub Picturebox1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)

With Picturebox1 'you control here, it must have hdc property
If ((x < 0) Or (x > .Width) Or (y < 0) Or (y > .Height)) Then 'MouseOut
Call ReleaseCapture
.BackColor = &H80FFFF 'you code her what ever you want to on mouse leave
ElseIf GetCapture <> .hwnd Then 'MouseEnter
Call SetCapture(.hwnd)
.BackColor = &HFF00& 'you code her what ever you want to on mouse over
Else
'Mouse Move
End If
End With
End Sub


You can view this article online at:
http://www.kiranreddys.com/knowledgebase/index.php/article/capture-mouse-enter-and-mouse-leave-events