利用Timer控件的Tick()事件在
自定义控件上绘制动画!
新建一个Windows控件项目,将Usercontrol.vb
重命名为leftToright.vb
代码如下:
Public Class leftToright
Public Enum LineStatus
公开枚举
特快 = 1
普快 = 2
中等 = 3
普慢 = 4
特慢 = 5
停止 = 6
End Enum
初始化_速度 为枚举常量2
Dim _速度 As LineStatus = LineStatus.普快
_ Public Property 速度() As LineStatus Get Return _速度 End Get Set(ByVal Value As LineStatus) _速度 = Value If 速度 = LineStatus.普快 Then Me.BackColor = Color.Green Me.Timer1.Enabled = True Me.Timer1.Interval = 16 End If If 速度 = LineStatus.特快 Then Me.BackColor = Color.Green Me.Timer1.Enabled = True Me.Timer1.Interval = 8 End If If 速度 = LineStatus.中等 Then Me.BackColor = Color.Green Me.Timer1.Enabled = True Me.Timer1.Interval = 20 End If If 速度 = LineStatus.普慢 Then Me.BackColor = Color.Green Me.Timer1.Enabled = True Me.Timer1.Interval = 25 End If If 速度 = LineStatus.特慢 Then Me.BackColor = Color.Green Me.Timer1.Enabled = True Me.Timer1.Interval = 35 End If If 速度 = LineStatus.停止 Then Me.Timer1.Enabled = False Me.BackColor = Color.Red End If End Set End Property 用来“装”矩形的位图 Dim bitmap(4) As Bitmap Dim x,r,j,k,y As Integer offsetX表示矩形每次沿x轴移动的距离 Dim offsetX As Integer 位图的宽和高 Dim bitmapWidth,bitmapHeight As Integer Private Sub leftToright_Paint(ByVal sender As Object,ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint 初始化变量 r = Me.Width / 7 x = 0 y = 2 * r + 2 * offsetX j = 4 * r + 2 * offsetX k = 6 * r + 2 * offsetX offsetX = 5 位图必须足够大,不仅能容纳矩形本身,还能遮住上一个循环中位图留下的痕迹 bitmapWidth = r + 6 * offsetX bitmapHeight = Me.Height 在位图中间画一个矩形 bitmap(1) = New Bitmap(bitmapWidth,bitmapHeight) bitmap(2) = New Bitmap(bitmapWidth,bitmapHeight) bitmap(3) = New Bitmap(bitmapWidth,bitmapHeight) bitmap(4) = New Bitmap(bitmapWidth,bitmapHeight) Dim g1 As Graphics Dim g2 As Graphics Dim g3 As Graphics Dim g4 As Graphics g1 = Graphics.FromImage(bitmap(1)) g2 = Graphics.FromImage(bitmap(2)) g3 = Graphics.FromImage(bitmap(3)) g4 = Graphics.FromImage(bitmap(4)) With g1 .Clear(BackColor) .FillRectangle(Brushes.Lime,New Rectangle(Me.Width * 0.06,Me.Height)) .Dispose() End With With g2 .Clear(BackColor) .FillRectangle(Brushes.Red,Me.Height)) .Dispose() End With With g3 .Clear(BackColor) .FillRectangle(Brushes.Yellow,Me.Height)) .Dispose() End With With g4 .Clear(BackColor) .FillRectangle(Brushes.Blue,Me.Height)) .Dispose() End With End Sub Private Sub Timer1_Tick(ByVal sender As Object,ByVal e As System.EventArgs) Handles Timer1.Tick Dim g1 As Graphics = CreateGraphics() Dim g2 As Graphics = CreateGraphics() Dim g3 As Graphics = CreateGraphics() Dim g4 As Graphics = CreateGraphics() 在恰当的位置画出位图(矩形) g1.DrawImage(bitmap(1),x,CInt(bitmapWidth * 1.25),bitmapHeight) g2.DrawImage(bitmap(2),y,bitmapHeight) g3.DrawImage(bitmap(3),bitmapHeight) g4.DrawImage(bitmap(4),bitmapHeight) g1.Dispose() g2.Dispose() g3.Dispose() g4.Dispose() If x > Me.Width Then x = -bitmapWidth + Me.Width * 0.03 End If If y > Me.Width Then y = -bitmapWidth + Me.Width * 0.03 End If If j > Me.Width Then j = -bitmapWidth + Me.Width * 0.03 End If If k > Me.Width Then k = -bitmapWidth + Me.Width * 0.03 End If 改变矩形的位置,以产生运动 x += offsetX g1 y += offsetX g2 j += offsetX g3 k += offsetX g4 End Sub End Class __________________________________________________________________________ 不得不说,这是个笨办法,其实可以某些控件代替矩形 __________________________________________________________________________ interesting __________________________________________________________________________ 要是画圆就没控件替代了 啊 __________________________________________________________________________ 我还没学到这里 谢谢你拉 __________________________________________________________________________