在判断一个鼠标已离开一个控件时,对于有hwnd属性的控件通常可以用SetCapture来进行处理。但是,如果没有hwnd属性,如Label,怎么办?
我的目的是用于一个自定义控件,考虑过用Hook,但是总感觉性能可能不会太好。

解决方案 »

  1.   

    没有hwnd的,一定有parent
    如果parent1是label1的parent
    那么parent1的mousemove事件,就是label1的mouseout了
      

  2.   

    在UserControl中,我如何对UserContro.ParentContol的MouseMove事件进行处理呀?
    我将我的最终目的说出来算了!简单的讲:
    我打算做一个透明控件,跟Label差不多。当MoveMove时ForColor=rgb(255,0,0),当MoveOut时ForColor=rgb(0,255,0)。代码:
    UserContol中:
    UserContol.BackStyle=0
    UserContol.WindowLess=TruePrivate Sub UserControl_Paint()
        UserControl.Cls
        Print "Caption"
    End Sub问题来了:
    一、无窗口控件的MouseMove事件本身就不会触发(即Usercontrol_Mousemove),只有
    UserControl_HitTest事件。
    二、在UserControl_HitTest中SetCapture无效。
    Private Sub UserControl_HitTest(X As Single, Y As Single, HitResult As Integer)
        Dim MouseOver As Boolean
        
        MouseOver = (0 <= X) And (X <= UserControl.Width) And (0 <= Y) And (Y <= UserControl.Height)
        If MouseOver = False Then Stop
        If MouseOver Then
            SetCapture UserControl.hwnd
            UserControl.ForeColor = RGB(255, 0, 255)
        Else
            UserControl.ForeColor = RGB(0, 255, 0)
            ReleaseCapture
        End If
    End Sub
    在上面的代码中,MouseOver 始终=True。有什么办法可以解决如上问题,或达到目的?
      

  3.   

    用PictureBox嘛,形状你可以自己用API:SetWindowRect()设置
      

  4.   

    无窗口控件的hwnd=0,所以setcapture无效我有个办法,
    用UserControl.ContainerHwnd获得装载UserControl1的容器hwnd
    用UserControl.Extender.left,UserControl.Extender.top获得,UserControl1所在位置,
    用GetCursorPos获得鼠标当前坐标(屏幕),
    用ScreenToClient获得鼠标在容器的坐标
    在timer控件的timer事件中判断鼠标是否在UserControl1里
      

  5.   

    Intelement(智能元素):用Text代替Label试试
    --------------------------------------------
    我要做的控件是透明的呀,Text能行吗?smalle(锋) :用PictureBox嘛,形状你可以自己用API:SetWindowRect()设置
    --------------------------------------------
     将Picture本身也是不透明的,到是可以用API将PictureBox搞成透明,那么和一个透明的"空"UserControl有什么区别?我个人感觉是不是绕了一个圈又回到了问题本身.lingll(凌零羚):
    --------------------------------------------
    想想看,可能应该没有问题,我先试试看.
      

  6.   

    lingll(凌零羚):
    问题基本上解决了,谢谢!
      

  7.   

    为了感谢大家的支持,我将代码帖出来:
    Private Sub timDetectPos_Timer()
        Dim hwndContainer As Long
        Dim posCurrent As POINTAPI
        Dim posContainer As POINTAPI
        Dim R As RECT
        Dim lRet As Long
        
        '获得鼠标当前坐标(屏幕)
        GetCursorPos posCurrent
        '获得鼠标在容器的坐标
        hwndContainer = UserControl.ContainerHwnd
        lRet = ClientToScreen(hwndContainer, posContainer)
        With R
            .Left = UserControl.Extender.Left / Screen.TwipsPerPixelX + posContainer.x
            .Top = UserControl.Extender.Top / Screen.TwipsPerPixelY + posContainer.y
            .Right = .Left + UserControl.Width / Screen.TwipsPerPixelX
            .Bottom = .Top + UserControl.Height / Screen.TwipsPerPixelY
        End With    If PtInRect(R, posCurrent.x, posCurrent.y) = 0 Then
            debug.print "NotIn"
        Else
            debug.print "IsIn"
        End If
        
    End Sub
      

  8.   

    http://zyl910vb.51.net/vb/gui/MouseLeave.htm右击连接,目标另存为
    注意把下载后的*.zip.jpg改名成*.zip
      

  9.   

    还有关于透明Usercontrol的问题,欢迎光临!
    http://expert.csdn.net/Expert/topic/1626/1626484.xml?temp=.151745
      

  10.   

    JennyVenus() :
    打不开该地址,发个到我邮箱中吧!
    [email protected]
      

  11.   

    怪,不是ScreenToClient 吗
    怎么是ClientToScreen?
      

  12.   

    我做了个控件,是这样实现的'*************************************************************************
    '**函 数 名:UserControl_MouseMove
    '**输    入:
    '**输    出:无
    '**功能描述:设置鼠标移动事件,鼠标离开事件
    '**全局变量:
    '**调用模块:
    '**作    者:
    '**日    期:
    '**修 改 人:
    '**日    期:
    '**版    本:版本1.0
    '*************************************************************************
    Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
       If X >= 0 And X <= UserControl.Width And Y >= 0 And Y <= UserControl.Height Then
          RaiseEvent MouseMove(Button, Shift, X, Y)
          Call SetCapture(UserControl.hwnd)    '捕获鼠标
       Else
          RaiseEvent MouseLeave
          Call ReleaseCapture              '释放鼠标
       End If
    End Sub
      

  13.   

    VB在这方面的表现不尽如人意
    UserControl默认的方法和事件比较的少
    如果想把内置子控件大多数或全部的方法和事件都向外界显露
    则要声明的语句太多太多再比如
    做某个类似控件ComboBox的控件
    要把它某个属性以类似ComboBox对象的List方法显露在IDE属性窗口中
    则方法是不可行的总之限制太多.NET则在这方面先进很多