VB.Net Tutorial/2D Graphics/Animation
Содержание
Animator
<source lang="vbnet">Imports System Imports System.Drawing Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data public class Animator
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Private img As Bitmap Dim currentlyAnimating As Boolean = False
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(536, 349) Me.Name = "Form1" Me.Text = "Form1" End Sub
- End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load img = New Bitmap("yourfile.jpg") End Sub Public Sub AnimateImage() If Not currentlyAnimating Then ImageAnimator.Animate(img, New EventHandler(AddressOf OnFrameChanged)) currentlyAnimating = True End If End Sub "AnimateImage
Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs) Me.Invalidate() End Sub "OnFrameChanged Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) AnimateImage() ImageAnimator.UpdateFrames() e.Graphics.DrawImage(Me.img, New Point(0, 0)) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) End Sub
End Class</source>
Program that animates a series of images
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Collections public class AnimationBasedOnTimer
public Shared Sub Main Application.Run(New FrmLogoAnimator) End Sub
End class Public Class FrmLogoAnimator
Inherits System.Windows.Forms.Form Private mImages As ArrayList = New ArrayList() Private mCount As Integer = 1 Public Sub New() MyBase.New() InitializeComponent() Dim i As Integer For i = 0 To 29 mImages.Add(Image.FromFile( i & ".gif")) Next logoPictureBox.Image = CType(mImages(0), Image) logoPictureBox.Size = logoPictureBox.Image.Size End Sub " New Friend WithEvents timer As System.Windows.Forms.Timer Friend WithEvents logoPictureBox As _ System.Windows.Forms.PictureBox
- Region " Windows Form Designer generated code "
"Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private components As System.ruponentModel.IContainer "Required by the Windows Form Designer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container() Me.timer = New System.Windows.Forms.Timer(Me.ruponents) Me.logoPictureBox = New System.Windows.Forms.PictureBox() Me.SuspendLayout() " "Timer " Me.timer.Enabled = True " "logoPictureBox " Me.logoPictureBox.Location = New System.Drawing.Point(32, 24) Me.logoPictureBox.Name = "logoPictureBox" Me.logoPictureBox.Size = New System.Drawing.Size(168, 88) Me.logoPictureBox.TabIndex = 0 Me.logoPictureBox.TabStop = False " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(232, 141) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.logoPictureBox}) Me.Name = "Form1" Me.Text = "LogoAnimator" Me.ResumeLayout(False) End Sub
- End Region
Private Sub timer_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles timer.Tick mCount = (mCount + 1) Mod 30 logoPictureBox.Image = CType(mImages(mCount), Image) End Sub
End Class</source>
Scrolling Text by Timer
<source lang="vbnet">Imports System.Windows.Forms public class ScrollingText
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private components As System.ruponentModel.IContainer Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Timer1 As System.Windows.Forms.Timer <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container Me.Label1 = New System.Windows.Forms.Label Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Timer1 = New System.Windows.Forms.Timer(Me.ruponents) Me.SuspendLayout() " "Label1 " Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(32, 32) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(216, 24) Me.Label1.TabIndex = 0 Me.Label1.Text = "Scrolling Text" " "Button1 " Me.Button1.Location = New System.Drawing.Point(24, 80) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 24) Me.Button1.TabIndex = 1 Me.Button1.Text = "Start" " "Button2 " Me.Button2.Location = New System.Drawing.Point(112, 80) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(64, 24) Me.Button2.TabIndex = 2 Me.Button2.Text = "Stop" " "Button3 " Me.Button3.Location = New System.Drawing.Point(192, 80) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(72, 24) Me.Button3.TabIndex = 3 Me.Button3.Text = "Exit" " "Timer1 " Me.Timer1.Interval = 300 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 150) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.Label1) Me.Name = "Form1" Me.Text = "Scroll Text" Me.ResumeLayout(False) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Do While Label1.Text.Length < 13 Label1.Text = Label1.Text & " " Loop Label1.AutoSize = True Timer1.Start() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Timer1.Stop() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click End End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Label1.Text = Microsoft.VisualBasic.Right(Label1.Text, 1) & Microsoft.VisualBasic.Left(Label1.Text, Label1.Text.Length - 1) End Sub
End Class</source>
Snow Animation
<source lang="vbnet">"Visual Basic.Net JingCai Programming 100 Examples "Author: Yong Zhang "Publisher: Water Publisher China "ISBN: 750841156
Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class Snow
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private components As System.ruponentModel.IContainer Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents Timer1 As System.Windows.Forms.Timer <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container Me.PictureBox1 = New System.Windows.Forms.PictureBox Me.Timer1 = New System.Windows.Forms.Timer(Me.ruponents) Me.SuspendLayout() " "PictureBox1 " Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Fill Me.PictureBox1.Location = New System.Drawing.Point(0, 0) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(292, 266) Me.PictureBox1.TabIndex = 0 Me.PictureBox1.TabStop = False " "Timer1 " Me.Timer1.Enabled = True Me.Timer1.Interval = 10 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.PictureBox1) Me.WindowState = System.Windows.Forms.FormWindowState.Maximized Me.ResumeLayout(False) End Sub Dim number, x(), y(), v(), s() As Integer Dim t As Random = New Random Dim g As System.Drawing.Graphics Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load PictureBox1.Location = Me.Location PictureBox1.Size = Me.Size PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage snow() End Sub Private Sub snow() number = 2000 ReDim x(number - 1) ReDim y(number - 1) ReDim v(number - 1) ReDim s(number - 1) Dim i As Integer For i = 0 To number - 1 Insnow(i) Next End Sub Private Sub Insnow(ByVal i As Integer) x(i) = t.Next(0, Me.Width - 1) y(i) = t.Next(0, Me.Height * 5 / 7) v(i) = t.Next(5, 20) s(i) = (t.Next(1, 3) * 100 + t.Next(50, 200)) / 100 End Sub Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint g = PictureBox1.CreateGraphics Dim i As Integer For i = 0 To number - 1 g.FillEllipse(Brushes.White, x(i), y(i), s(i), s(i)) Next End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim i As Integer For i = 0 To number - 1 y(i) = y(i) + v(i) If y(i) >= Me.Height Then Insnow(i) End If Next PictureBox1.Invalidate() End Sub
End Class</source>