我做一个ACTIVE控件,如何得知当前鼠标在不在UserControl上面?

解决方案 »

  1.   

    GetMouse.bas----------------------------------------------------------------------
    Option ExplicitType POINTAPI
         X As Long
         Y As Long
    End Type'api声明
    private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) _
    As Longprivate Declare Function WindowFromPoint Lib "user32" _
    (ByVal xPoint As Long, ByVal yPoint As Long) As LongPrivate Declare Function ScreenToClient Lib "user32" _
    (ByVal hwnd As Long, lpPoint As POINTAPI) As LongPublic function GetMouseHwnd() as longDim lonCStat As Long
    Dim lonCurhWnd As Long
    Dim mpoiCursorPos As POINTAPI
        
     'two API function
     '取得鼠标坐标  到mpoiCursorPos中。
    lonCStat = GetCursorPos&(mpoiCursorPos) 'get mouse position'根据坐标找到对应该点的窗口句柄
    getmousehwnd = WindowFromPoint(mpoiCursorPos.X, _
        mpoiCursorPos.Y)
        
        
    End function
    -------------------------------------------------------------------------------------if GetMouseHwnd=usercontrol.hwnd then msgbox "鼠标在控件上" else msgbox "不在"
      

  2.   

    Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      If GetMouseHwnd = UserControl.hwnd Then
            UserControl.BackColor = &H80000010
         Else
             UserControl.BackColor = &H80000000
      End If
    End Sub
    我在MouseMove里面这样写的
    为什么不行呢?鼠标移上去有变化,可当移出后不变回来啊。。
      

  3.   

    因为只有鼠标在控件上时才会产生MouseMove事件,所以在这个事件中,GetMouseHwnd = UserControl.hwnd 会永远成立。
      

  4.   

    在那上面就一般来说都会产生mouse_move事件了
    这样不行的话,用screentoclient 将鼠标坐标转化成当前窗体坐标,判断一下就知道了
      

  5.   

    Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      If GetMouseHwnd = UserControl.hwnd Then
            UserControl.BackColor = &H80000010
         Else
             UserControl.BackColor = &H80000000
      End If
    End Sub因在退出时没有事件发生!  not(GetMouseHwnd = UserControl.hwnd)不会出现!你用拖拉方式处理即可!
      

  6.   

    你在控件里放一个timer控件.
    在Timer事件中写这段代码private sub Timer1_timer()
    If GetMouseHwnd = UserControl.hwnd Then
            UserControl.BackColor = &H80000010
         Else
             UserControl.BackColor = &H80000000
      End Ifend sub