VB Code:
  1. Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
  2. Private Declare Function ReleaseCapture Lib "user32" () As Long
  3. Private Declare Function GetCapture Lib "user32" () As Long
  4.  
  5. Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  6.     Select Case True
  7.         Case X < 0, Y < 0, X > Command1.Width, Y > Command1.Height
  8.             ' Leaving
  9.             ReleaseCapture
  10.             ' Do Stuff
  11.             Debug.Print "Leaving"
  12.         Case Not GetCapture() = Command1.hwnd
  13.             ' Entering
  14.             SetCapture Command1.hwnd
  15.             ' Do Stuff
  16.             Debug.Print "Entering"
  17.     End Select
  18. End Sub