'添加一个计时器 Timer1 ,一个图像框 Image1 Option Explicit Dim dx '定义两个窗体层变量 Dim dy '分别存放X,Y 方向变化增量 Private Sub Form_Load() '初始化 Timer1.Enabled = True Timer1.Interval = 100 dx = Form1.Width / (Rnd * 150 + 200) dy = Form1.Height / (Rnd * 150 + 200) End Sub Private Sub Timer1_Timer() Image1.Top = Image1.Top + dy '通过改变 top 属性改变高度 Image1.Left = Image1.Left + dx If Image1.Top <= 0 Then dy = -dy '如“碰壁”则变向 If Image1.Left <= 0 Then dx = -dx If Image1.Top >= Form1.Height - Image1.Height Then dy = -dy If Image1.Left >= Form1.Width - Image1.Width Then dx = -dx End Sub
Option Explicit Dim dx '定义两个窗体层变量 Dim dy '分别存放X,Y 方向变化增量
Private Sub Form_Load() '初始化 Timer1.Enabled = True Timer1.Interval = 10 dx = Screen.Width / (Rnd * 150 + 200) dy = Screen.Height / (Rnd * 150 + 200) End Sub
Private Sub Image1_Click() Unload Me End Sub
Private Sub Timer1_Timer() Me.Top = Me.Top + dy '通过改变 top 属性改变高度 Me.Left = Me.Left + dx If Me.Top <= 0 Then dy = -dy '如“碰壁”则变向 If Me.Left <= 0 Then dx = -dx If Me.Top >= Screen.Height - Me.Height Then dy = -dy If Me.Left >= Screen.Width - Me.Width Then dx = -dx End Sub