自制一个按钮控件,想实现以下效果:鼠标移过时按钮变色,移开时再变其它色(鼠标并没有按下)移过时按钮变色没有什么问题,问题是如何判断鼠标已经移开控件?我的思路是用一个timer加GetCursouPos来实现,不知到各位大侠有没有更好的招?

解决方案 »

  1.   

    Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim MouseOver As Boolean
        
        '判断当前鼠标位置是否在Command2上
        MouseOver = (0 <= X) And (X <= Command2.Width) And (0 <= Y) And (Y <= Command2.Height)
        If MouseOver Then
            SetCapture Command2.hWnd
            debug.print "鼠标来了"
        Else
            debug.print "鼠标又走了"
            ReleaseCapture
        End If
    End Sub