用timer控制picturebox在移动,图形碰到边界后返回,代码是这样的
Public Class Form2
Dim xi As Integer, yi As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim x As Single, y As Single
x = PictureBox1.Location.X
y = PictureBox1.Location.Y
If x > Me.Width - PictureBox1.Width - 16 Or x < 0 Then
xi = -xi
End If
If y > Me.Height - PictureBox1.Height - Me.Icon.Height - 6 Or y < 0 Then
yi = -yi
End If
x = x + xi
y = y + yi
PictureBox1.Location = New Point(x, y)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Interval = 30
xi = 1
yi = 1
End Sub
End Class
这样恰能完美运行,我比较奇怪的必须x和y坐标的边界位置必须分别减去16和6,不然运行起来就会超出窗体边界才回来
我想要知道这究竟是为什么,窗体长这样