Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSlowly fill a circle with color from bottom to top in Visual Basic .NET
DescriptionThis example shows how to slowly fill a circle with color from bottom to top in Visual Basic .NET
Keywordscircle, ellipse, fill circle, fill ellipse, VB.NET, animation
CategoriesVB.NET, Graphics
 
The program draws an ellipse on its form. When the user clicks the ellipse, the program enables a timer. When the timer fires, it invalidates the form to geneate a Paint event.
 
' If the user clicked on the circle, start filling.
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.MouseEventArgs) Handles _
    Me.MouseDown
    Dim cx As Integer = m_CircleBounds.X + _
        m_CircleBounds.Width \ 2
    Dim cy As Integer = m_CircleBounds.Y + _
        m_CircleBounds.Height \ 2
    If (cx - e.X) * (cx - e.X) + (cy - e.Y) * (cy - e.Y) <= _
        100 * 100 Then
        tmrFiller.Enabled = True
    End If
End Sub

' Redraw.
Private Sub tmrFiller_Tick(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles tmrFiller.Tick
    Me.Invalidate()
End Sub
 
The Paint event handler draws the circle in its background color. If the timer is enabled, it then colors the filled part. It creates a GraphicsPath object and adds an arc to it representing the filled area. It uses the Grpahics object's FillPath method to fill the area.

The code then increases the filled arc's sweep angle. If the angle is greater than 360 degrees, the whole circle is filled so the code disables the timer and resets for the next mouse click.

 
' Draw the circle.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    ' Color the circle's background.
    Using br As New SolidBrush(m_BackColor)
        e.Graphics.FillEllipse(br, m_CircleBounds)
    End Using

    ' If the timer is enabled, fill part of the circle.
    If tmrFiller.Enabled Then
        ' Color the circle's filled part.
        Using br As New SolidBrush(m_FillColor), gp As New _
            GraphicsPath()
            Dim start_angle As Single = 90 - m_SweepAngle / _
                2
            gp.AddArc(m_CircleBounds, start_angle, _
                m_SweepAngle)
            e.Graphics.FillPath(br, gp)
            m_SweepAngle += 20
        End Using

        ' Increase the sweep angle.
        m_SweepAngle = CSng(m_SweepAngle + PI / 10)

        ' See if we're done with this color.
        If m_SweepAngle > 360 Then
            tmrFiller.Enabled = False
            m_BackColor = m_FillColor
            m_FillColorNumber = (m_FillColorNumber + 1) Mod _
                m_Colors.Length
            m_FillColor = m_Colors(m_FillColorNumber)
            m_SweepAngle = 0
        End If
    End If

    ' Outline the circle.
    e.Graphics.DrawEllipse(Pens.Black, m_CircleBounds)
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated