各位好!
    当鼠标移入某个控件的表面时,可以在MouseMove事件中编写代码来响应,比如改变颜色之类。那么又怎么判断鼠标从这个控件上移出去了呢?
     请高人赐教!

解决方案 »

  1.   

    在该mousemove事件中设置一个变量,如下
    dim bInCtrl as boolean
    bInCtrl=(x>=left) and ( x<=width ) and ( y>=top) and (y<=height)
    if bInCtrl=true then
        SetCapture hwnd '控件的句柄
    else
        ReleaseCapture
    end if
      

  2.   

    问:怎样捕捉控件的Mouse in和Mouse leave事件答:
    没有这样的事件。只要鼠标进入控件就产生MouseMove事件,你可以用第一次触发MouseMove事件来代替MouseIn事件。 
        至于MouseLeave事件就麻烦,可以利用Form的MouseMove事件来代替控件的MouseLeave事件。 
        如果控件具有hWnd属性(Label没有这个属性,但CommandButtun有),也可以调用SetCapture和ReleaseCapture这两个Windows API函 
        数的方法来实现它。具体步骤如下: 
        1) 在VB中新建一个标准EXE工程; 
        2) 画出一个按钮Command1; 
        3) 在窗体Form1中定义Windows API的声明; 
        Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long 
        Private Declare Function ReleaseCapture Lib "user32" () As Long 
        4) 在Command1的MouseMove事件中编写以下代码: 
        Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) 
         Dim MouseEnter As Boolean '鼠标进入的标志位 
         
         MouseEnter = (0 <= X) And (X <= Command1.Width) And (0 <= Y) And (Y <= 
        Command1.Height) '计算鼠标的移动是否在Command1里面 
         If MouseEnter Then '鼠标已经进入 
         Me.Caption = "Mouse In Button!" 
         SetCapture Command1.hWnd 
         Else '鼠标已经离开 
         Me.Caption = "Mouse Out!" 
         ReleaseCapture 
         End If 
        End Sub 
        另外一个简单的办法是使用免费的MouseTrap控件(http://www.halfx.com/downloads/MouseTrap.EXE)。
      

  3.   

    捕捉 MouseExit 事件
    MouseDown、MouseUp、MouseMove。VB 似乎提供了很好的 Mouse 事件。但好象还缺少什么!对!还差 MouseExit(鼠标移出)事件。在 VB 中,我们要捕捉 MouseExit 事件,必须用 API 函数:
    Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    然后,我们可以在控件(以 Picture1 为例)的 MouseMove 事件上加上以下代码:With Picture1 'Change this to the name of the control
    If Button = 0 Then
    If (X < 0) Or (Y < 0) Or (X > .Width) Or (Y > .Height) Then
    'Mouse pointer is outside button, so let other controls receive
    'mouseevents too:
    ReleaseCapture
    ' 放入鼠标离开的代码
    Else
    ' Mouse pointer is over button, so we'll capture it, thus
    ' we'll receive mouse messages even if the mouse pointer is
    ' not over the button
    SetCapture .hWnd' 放入鼠标进入的代码
    End If
      

  4.   

    Option ExplicitPrivate Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If X > 0 And X < Text1.Width And Y > 0 And Y < Text1.Height Then
            SetCapture Text1.hwnd
            Text1.BackColor = vbRed
        Else
            Text1.BackColor = vbWhite
            ReleaseCapture
        End If
    End Sub
      

  5.   

    除了通过对象的hwnd属性来获得句柄以外,可不可以通过API函数来获得句柄(比如一个框架的),从而判断鼠标从这个对象上移出去了呢?
    可不可以举个例子?(窗体的除外)
    先谢谢大家了