' Sunset Example ' Draw an animated sunset, including sun, sky and grass. Public Class Form1 Inherits System.Windows.Forms.Form #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.ComponentModel.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. Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents mnuStart As System.Windows.Forms.MenuItem Friend WithEvents mnuStop As System.Windows.Forms.MenuItem Friend WithEvents mnuSpeed As System.Windows.Forms.MenuItem Friend WithEvents mnuSlow As System.Windows.Forms.MenuItem Friend WithEvents mnuMedium As System.Windows.Forms.MenuItem Friend WithEvents mnuFast As System.Windows.Forms.MenuItem Friend WithEvents Timer1 As System.Windows.Forms.Timer Friend WithEvents picDisplay As System.Windows.Forms.PictureBox Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container Me.picDisplay = New System.Windows.Forms.PictureBox Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.mnuStart = New System.Windows.Forms.MenuItem Me.mnuStop = New System.Windows.Forms.MenuItem Me.mnuSpeed = New System.Windows.Forms.MenuItem Me.mnuSlow = New System.Windows.Forms.MenuItem Me.mnuMedium = New System.Windows.Forms.MenuItem Me.mnuFast = New System.Windows.Forms.MenuItem Me.Timer1 = New System.Windows.Forms.Timer(Me.components) Me.SuspendLayout() ' 'picDisplay ' Me.picDisplay.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.picDisplay.Location = New System.Drawing.Point(40, 32) Me.picDisplay.Name = "picDisplay" Me.picDisplay.Size = New System.Drawing.Size(300, 300) Me.picDisplay.TabIndex = 0 Me.picDisplay.TabStop = False ' 'MainMenu1 ' Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuStart, Me.mnuStop, Me.mnuSpeed}) ' 'mnuStart ' Me.mnuStart.Index = 0 Me.mnuStart.Text = "Start" ' 'mnuStop ' Me.mnuStop.Index = 1 Me.mnuStop.Text = "Stop" ' 'mnuSpeed ' Me.mnuSpeed.Index = 2 Me.mnuSpeed.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuSlow, Me.mnuMedium, Me.mnuFast}) Me.mnuSpeed.Text = "Speed" ' 'mnuSlow ' Me.mnuSlow.Index = 0 Me.mnuSlow.Text = "Slow" ' 'mnuMedium ' Me.mnuMedium.Index = 1 Me.mnuMedium.Text = "Medium" ' 'mnuFast ' Me.mnuFast.Index = 2 Me.mnuFast.Text = "Fast" ' 'Timer1 ' ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(376, 366) Me.Controls.Add(Me.picDisplay) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "Sunset Example" Me.ResumeLayout(False) End Sub #End Region Private sunRadius As Integer = 20 Private sunPosition As Integer = 50 Private skyBrightness As Integer = 255 Private sunBrightness As Integer = 255 Private Sub picDisplay_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) Handles picDisplay.Paint Dim g As Graphics = e.Graphics Dim skyColor As Color = Color.FromArgb( _ skyBrightness * 0.75, skyBrightness * 0.75, skyBrightness) Dim blackPen As New Pen(Color.Black, 2) Dim sunBrush As New SolidBrush(Color.FromArgb( _ 127 + sunBrightness * 0.5, sunBrightness, 0)) Dim grassBrush As New SolidBrush(Color.FromArgb( _ 0, sunBrightness, 0)) ' Set sky color. picDisplay.BackColor = skyColor ' Draw sun. g.FillEllipse(sunBrush, 230, sunPosition, 2 * sunRadius, 2 * sunRadius) g.DrawEllipse(blackPen, 230, sunPosition, 2 * sunRadius, 2 * sunRadius) ' Draw grass. g.FillRectangle(grassBrush, -3, 200, 310, 110) g.DrawRectangle(blackPen, -3, 200, 310, 110) End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Timer1.Tick If sunPosition < 252 Then sunPosition += 2 End If If sunBrightness > 4 Then sunBrightness -= 4 End If If skyBrightness > 2 Then skyBrightness -= 2 End If picDisplay.Invalidate() End Sub Private Sub mnuStart_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuStart.Click sunPosition = 50 sunBrightness = 255 skyBrightness = 255 Timer1.Enabled = True End Sub Private Sub mnuStop_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuStop.Click Timer1.Enabled = False End Sub Private Sub mnuSlow_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuSlow.Click Timer1.Interval = 100 End Sub Private Sub mnuMedium_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuMedium.Click Timer1.Interval = 50 End Sub Private Sub mnuFast_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuFast.Click Timer1.Interval = 25 End Sub End Class