在窗体中建立一个图像框,然后把鼠标隐藏起来。代码如果是这样:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Left = X
Image1.Top = Y
End Sub
结果图像框充当鼠标,但是当鼠标移动到图像框上时窗体就无法获得鼠标的位置,图像框就不会动了。
如果改成:
Option ExplicitPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Left = X
Image1.Top = Y
End SubPrivate Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Left = Image1.Left + X
Image1.Top = Image1.Top + Y
End Sub
可以实现一定效果,但是还是不理想。
(鼠标隐藏代码没写)
所以想请问有没有什么方法可以获得鼠标相对于窗体的时时位置,不论鼠标是否在控件上。明白我的意思吗?

解决方案 »

  1.   

    使用API可以做到:
    Option Explicit
        Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long
        Private Type PointAPI
            X As Long
            Y As Long
        End Type
        Dim MousePos As PointAPI
        Dim NewX As Long
        Dim NewY As LongPrivate Sub Command2_Click()
        Print "有没有办法去响应控件(commandbutton)事件?"
    End SubPrivate Sub Form_Load()
        Me.WindowState = 2
        Command2.Enabled = False
    End SubPrivate Sub Timer1_Timer()
        GetCursorPos MousePos
        NewX = MousePos.X * Screen.TwipsPerPixelX
        NewY = MousePos.Y * Screen.TwipsPerPixelY - 300
        Label1 = NewX
        Label2 = NewY
        If NewX >= Command2.Left And NewX <= Command2.Left + Command2.Width And NewY >= Command2.Top And NewY <= Command2.Height + Command2.Top Then
            Command2_Click
        Else
            Command2.Enabled = False
            Me.Cls
        End If
    End Sub
      

  2.   

    楼上的意思我大致明白。
    你的代码很有意思:没有command1,command2。而且把command2.enabled=false,当然无法响应控件事件。另外我也不要在使用鼠标时多一个无效控件。(我知道你想论证一些东西才加入的,开个玩笑。)之前我也用过这个API,代码是:Option ExplicitPrivate Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long
    Private Type PointAPI
        X As Long
        Y As Long
    End Type
    Dim MousePos As PointAPIPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    GetCursorPos MousePos
    Image1.Left = MousePos.X
    Image1.Top = MousePos.Y
    End Sub结果效果与问题中的第一段代码相同,误以为这个API返回的只是鼠标在窗体时的位置,在控件上不能返回值。刚刚才明白在控件上的时候没有触发该事件,现在把代码改为:Option ExplicitPrivate Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long
    Private Type PointAPI
        X As Long
        Y As Long
    End Type
    Dim MousePos As PointAPIPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    GetCursorPos MousePos
    Image1.Left = MousePos.X
    Image1.Top = MousePos.Y
    End SubPrivate Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    GetCursorPos MousePos
    Image1.Left = MousePos.X
    Image1.Top = MousePos.Y
    End Sub就可以实现我想要的效果了。总之,谢谢楼上的回复。
      

  3.   

    LZ:应时间紧随手找了一段响应控件(commandbutton)事件的代码贴上.
    你能解决问题就好!