Vb.net VB2010使对象移动到鼠标指针

Vb.net VB2010使对象移动到鼠标指针,vb.net,mouseevent,Vb.net,Mouseevent,我试图制作一个简单的点击系统,当用户点击屏幕上的某个点时,一个对象(在本例中是一个椭圆形)将移动到该点。它有点有效,唯一的问题是它只是稍微离开鼠标的位置。我假设这与绘制椭圆的位置有关,我没有考虑到这一点:我有以下代码: Public Class Form1 Dim formWidth, formHeight As Integer Dim screenWidth As Integer = Screen.PrimaryScreen.Bounds.Width Dim scree

我试图制作一个简单的点击系统,当用户点击屏幕上的某个点时,一个对象(在本例中是一个椭圆形)将移动到该点。它有点有效,唯一的问题是它只是稍微离开鼠标的位置。我假设这与绘制椭圆的位置有关,我没有考虑到这一点:我有以下代码:

Public Class Form1
    Dim formWidth, formHeight As Integer
    Dim screenWidth As Integer = Screen.PrimaryScreen.Bounds.Width
    Dim screenHeight As Integer = Screen.PrimaryScreen.Bounds.Height
    Dim mousePos As Point
    Dim ballPos As Point

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        formHeight = screenHeight - 200
        formWidth = screenWidth - 300
        Me.Size = New System.Drawing.Size(formWidth, formHeight)
        Me.Location = New Point(5, 5)
        ballTimer.Stop()
    End Sub

    Private Sub ballTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ballTimer.Tick
        If ballPos.X < mousePos.X Then
            ballPos.X += 20
            ball.Location = ballPos
        End If
        If ballPos.X > mousePos.X Then
            ballPos.X -= 20
            ball.Location = ballPos
        End If
        If ballPos.Y < mousePos.Y Then
            ballPos.Y += 20
            ball.Location = ballPos
        End If
        If ballPos.Y > mousePos.Y Then
            ballPos.Y -= 20
            ball.Location = ballPos
        End If
        If ballPos = mousePos Then
            ballTimer.Stop()
        End If
    End Sub

    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        ballPos = New Point(ball.Location.X, ball.Location.Y)
        mousePos = New Point(MousePosition)
        ballTimer.Start()
    End Sub
End Class
公共类表单1
Dim formWidth,formHeight为整数
将屏幕宽度调整为整数=Screen.PrimaryScreen.Bounds.Width
将屏幕高度调整为整数=Screen.PrimaryScreen.Bounds.Height
暗淡的鼠标点
暗淡的圆点
私有子表单1_Load(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
formHeight=屏幕高度-200
formWidth=屏幕宽度-300
Me.Size=新系统.Drawing.Size(formWidth,formHeight)
Me.位置=新点(5,5)
停止
端接头
私有子ballTimer_Tick(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理ballTimer.Tick
如果ballPos.XmousePos.X,则
球位置X-=20
ball.Location=ballPos
如果结束
如果ballPos.YmousePos.Y,则
球位Y-=20
ball.Location=ballPos
如果结束
如果ballPos=mousePos,则
停止
如果结束
端接头
私有子表单1u MouseClick(ByVal sender作为对象,ByVal e作为System.Windows.Forms.MouseEventArgs)处理我。MouseClick
ballPos=新点(ball.Location.X,ball.Location.Y)
鼠标位置=新点(鼠标位置)
ballTimer.Start()
端接头
末级
我有点麻烦让它移动到鼠标指针的正上方。有人能帮我计算一下这个问题吗?谢谢

唯一的问题是,如果它只是稍微离开鼠标所在的位置

你以20步移动球。当球位于目标位置20像素范围内时,应将其移动到准确位置,例如

    If ballPos.X < mousePos.X Then
        If mousePos.X - ballPos.X > 20 Then
            ballPos.X += 20
        Else
            ballPos.X = mousePos.X
        End If
        ball.Location = ballPos
    End If
如果ballPos.X20,则
球位置X+=20
其他的
ballPos.X=鼠标点.X
如果结束
ball.Location=ballPos
如果结束

请显示球的定义位置。抱歉。我只是简单地把球画在表单上,然后在表单中显示
ball
的相关代码。