“彩蝶”伴“鼠”飞
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DrawWidth = 5
Static CX(3) As Single, cy(3) As Single′用Static声明的变量可在过程重启时仍保存上次的值
Dim xx(3) As Single, yy(3) As Single
Dim i As Integer
For i = 0 To 3
PSet (CX(i), cy(i)), Me.BackColor′用背景色画点,将上次画的彩点覆盖掉
′取以鼠标为圆心250为半径的圆内的随机点
xx(i) = (Rnd * 500 - 250) + X
yy(i) = (Rnd * 500 - 250) + Y
Next i
For i = 0 To 3
PSet (xx(i), yy(i)), RGB(Rnd * 255, Rnd * 255, Rnd * 255)'画4个颜色随机变化的彩点
′保存4个随机点的位置,以便下个MouseMove事件中能通过画背景色的点来覆盖它们
CX(i) = xx(i)
cy(i) = yy(i)
Next i
DrawWidth = 1
End Sub
如果在窗体上有Label控件,彩点会把它们擦掉,我们可以判断出随机点是否接近Label控件,从而决定是否画彩点。例如有三个Label控件,将下面代码替掉斜体部分就能使Label控件不受影响。
For i = 0 To 3
If (xx(i) <= Label1.Left - 30 Or yy(i) <= Label1.Top - 30 Or yy(i) >= Label1.Top + Label1.Height + 30 Or xx(i) >= Label1.Left + Label1.Width + 30) _
And (xx(i) <= Label2.Left - 30 Or yy(i) <= Label2.Top - 30 Or yy(i) >= Label2.Top + Label2.Height + 30 Or xx(i) >= Label2.Left + Label2.Width + 30) _
And (xx(i) <= Label3.Left - 30 Or yy(i) <= Label3.Top - 30 Or yy(i) >= Label3.Top + Label3.Height + 30 Or xx(i) >= Label3.Left + Label3.Width + 30) Then′如彩点不在Label控件范围内
PSet (xx(i), yy(i)), RGB(Rnd * 255, Rnd * 255, Rnd * 255)′画4个颜色随机变化的彩点
Else ′在Label控件范围内则不画点
xx(i) = 0
yy(i) = 0
End If