如何得知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 LongPrivate Sub Command1_Click()
 Command1.Tag = ""
End SubPrivate 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 IfEnd Sub
 

解决方案 »

  1.   

    用控件的MouseMove和form1的MouseMove
    dim m as Integer Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      m = 1
    End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     m = 0
    End Sub如果M=1then鼠标还在控件上。此法对控件之间有一定的距离就有效。
      

  2.   

    把控件的MousePointer设置成99,MouseIcon选定一个图标,
    或MousePointer设置成指定的形状,MouseIcon不设置,
    当鼠标移动到控件上时鼠标变为指定样式,就知道是否在该空间上。
    这个办法比较简单。