如何得知Mouse已離開某物件(二)
來源:cww   參考 王國榮先生的作法
上一回使用mouse Hook的方式來Check Mouse是否已離開某物件,詳見
如何得知Mouse已離開某物件(Mouse Hook)但使用這個方法太麻
煩了,改用SetCapture 來使Mouse的Message轉到某個Window之上,如此,不管Mouse移動
於何處,都會將Mouse Input Message傳給某個Window,最後使用ReleaseCapture來取消這
個作用。

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

Private Sub Command1_Click()
 Command1.Tag = ""
End Sub

Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Command1.Tag = "In" Then
	If X < 0 Or Y < 0 Or X > Command1.Width Or Y > Command1.Height Then
	    Command1.Tag = ""
	    ReleaseCapture
	    Command1.Caption = "離開"
	End If
    Else
	Command1.Tag = "In"
	SetCapture Command1.hwnd
	Command1.Caption = "進入"
    End If

End Sub