【发布时间】:2014-04-01 23:51:17
【问题描述】:
我正在尝试制作一个简单的点击系统,当用户点击屏幕上的一个点时,一个对象(在本例中为椭圆形)将移动到该点。它有点工作,唯一的问题是它只移动了一点鼠标所在的位置。我认为这与绘制椭圆的位置有关,我没有考虑到这一点:我有以下代码:
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
让它准确地移动到鼠标指针的顶部有点麻烦。有人可以帮我解决这个问题吗?谢谢。
【问题讨论】:
-
请显示定义球的位置。
-
道歉。我只是在表格上画了一个球。
-
那么就在
Form1.Designer.vb中显示ball的相关代码。
标签: vb.net mouseevent