none
Possibly useful "I'm running" signal for apps

    General discussion

  • This originally was asked as a question about the Moving Ball Illusion, thanks to TommyTwoTrain I got it working properly. I just finished up an app that needed to show that a timer was indeed running so I adapted the code to a much smaller size. Hope someone finds it useful and/or entertaining. Just paste everything into a windows form's code and go.

    Imports System.Drawing.Drawing2D
    
    Public Class Form1
        Dim Brs As New List(Of Brush)
        Dim Ballstep As Integer = 30
        Private TimeAngle As Single = 0
        Private BallAngle As Single = 0
        Dim PBSize As Integer = 30
        WithEvents timer1 As New Windows.Forms.Timer With {.Interval = 20, .Enabled = False}
        WithEvents PBSpin As New PictureBox With {.Size = New Size(50, 50), .Location = New Point(10, 10)}
        WithEvents StartBtn As New Button With {.Location = New Point(175, 10), .Size = New Size(50, 20)}
        WithEvents StopBtn As New Button With {.Location = New Point(230, 10), .Size = New Size(50, 20)}
        WithEvents Sizer As New NumericUpDown With {.Location = New Point(10, 150), .Size = New Size(70, 20), .Minimum = 25, .Maximum = 100, .Increment = 2}
    
        Private Sub DrawBall(g As Graphics, angle As Integer, w2 As Integer, h2 As Integer)
            With g
                .ResetTransform()
                .TranslateTransform(w2, h2)
                .RotateTransform(angle)
                Dim r As Integer = PBSpin.Width \ 15 '30 ' ball size
                Dim x As Integer = (w2 - r) * Math.Cos((angle + TimeAngle) / 57.3)
                .FillEllipse(Brs(CInt(angle / Ballstep)), New Rectangle(x - r, -r, r * 2, r * 2))
            End With
        End Sub
        Private Sub timer1_Tick(sender As Object, e As EventArgs) Handles timer1.Tick
            TimeAngle += 5 'rotate speed here
            If TimeAngle > 360 Then TimeAngle -= 360
            PBSpin.Invalidate()
        End Sub
        Private Sub PBSpin_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PBSpin.Paint
            With e.Graphics
                .Clear(Me.BackColor)
                .SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                Using br As New SolidBrush(Me.BackColor)
                    Dim w2 As Integer = PBSpin.Width / 2
                    Dim h2 As Integer = PBSpin.Height / 2
                    .FillEllipse(br, 0, 0, CInt(2 * w2), CInt(2 * h2))
                    For a = 0 To 179 Step 22.5
                        DrawBall(e.Graphics, a, w2, h2)
                    Next
                    .ResetTransform()
                End Using
            End With
        End Sub
    
        Private Sub Stop_Click(sender As System.Object, e As System.EventArgs) Handles StopBtn.Click
            timer1.Stop()
        End Sub
    
        Private Sub Start_Click(sender As System.Object, e As System.EventArgs) Handles StartBtn.Click
            timer1.Start()
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.Size = New Size(300, 300)
            Brs.Add(Brushes.Red)
            Brs.Add(Brushes.Orange)
            Brs.Add(Brushes.Green)
            Brs.Add(Brushes.Blue)
            Brs.Add(Brushes.Indigo)
            Brs.Add(Brushes.Violet)
    
            Me.Controls.Add(PBSpin)
            Me.Controls.Add(StartBtn)
            Me.Controls.Add(StopBtn)
            Me.Controls.Add(Sizer)
            Sizer.Value = 30
            StartBtn.Text = "Start"
            StopBtn.Text = "Stop"
            timer1.Start()
        End Sub
    
        Private Sub Sizer_ValueChanged(sender As System.Object, e As System.EventArgs) Handles Sizer.ValueChanged
            PBSpin.Size = New Size(Sizer.Value, Sizer.Value)
        End Sub
    End Class
    

    Wednesday, April 19, 2017 4:29 PM