新建一个项目,在FORM窗体上加一个picturebox,一个timer,一个button控件
代码如下:
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Drawing.Imaging
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Timer1.Enabled = True Then
Timer1.Enabled = False
Else : Timer1.Enabled = True
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
PictureBox1.Refresh()
Dim mycolor As Color
Dim mypen As New Pen(mycolor, 5)
mypen.DashStyle = DashStyle.Custom
mypen.Color = Color.FromArgb(255, 0, 0)
Dim g1, g2, g3, g4 As Graphics
g1 = PictureBox1.CreateGraphics
g2 = PictureBox1.CreateGraphics
g3 = PictureBox1.CreateGraphics
g4 = PictureBox1.CreateGraphics
Dim a, b As Integer
a = Int(Rnd() * PictureBox1.Width)
b = Int(Rnd() * PictureBox1.Height)
g1.DrawLine(mypen, a, b, a + 60, b)
g2.DrawLine(mypen, a, b, a - 60, b)
g3.DrawLine(mypen, a, b, a, b + 60)
g4.DrawLine(mypen, a, b, a, b - 60)
g1.Dispose()
g2.Dispose()
g3.Dispose()
g4.Dispose()
End Sub
End Class