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
 
 
 
 
 
TitleDraw pie slices in VB .NET
Keywordspie slice, wedge, circle
CategoriesAlgorithms, Graphics
 
In the form's Paint event handler, use the PaintEventArgs parameter to get the Graphics object on which to draw. Use its DrawEllipse method to draw a circle. Use its FillPie method to fill in a pie slice and use DrawPie to outline the slice.

Note that the starting angle in FillPie and DrawPie use degrees clockwise from horizontal. The final argument to these methods is the sweep angle (angle the slice covers) not the ending angle for the slice. Make the sweep angle positive for a clockwise slice, negative for a counter clockwise slice.

 
' Draw pie slices. Note that the DrawPie
' and FillPie methods draw using degrees 
' clockwise from horizontal.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    Const GAP As Single = 5
    Const W As Single = 100
    Dim gr As Graphics = e.Graphics
    Dim X As Single
    Dim Y As Single
    Dim circle_brush As Brush = Brushes.White
    Dim slice_brush As Brush = Brushes.Black

    ' Northeast wedge.
    X = 0
    Y = 0
    gr.DrawEllipse(Pens.White, X, Y, W, W)
    gr.FillPie(slice_brush, _
        X, Y, W, W, 300, 30)
    gr.DrawPie(Pens.Red, _
        X, Y, W, W, 300, 30)

    ' Everything else.
    X += W + GAP
    gr.DrawEllipse(Pens.White, X, Y, W, W)
    gr.FillPie(slice_brush, _
        X, Y, W, W, 300, -330)
    gr.DrawPie(Pens.Red, _
        X, Y, W, W, 300, -330)

    ' East wedge.
    Y += W + GAP
    X = 0
    gr.DrawEllipse(Pens.White, X, Y, W, W)
    gr.FillPie(slice_brush, _
        X, Y, W, W, 315, 90)
    gr.DrawPie(Pens.Red, _
        X, Y, W, W, 315, 90)

    ' Everything else.
    X += W + GAP
    gr.DrawEllipse(Pens.White, X, Y, W, W)
    gr.FillPie(slice_brush, _
        X, Y, W, W, 315, -270)
    gr.DrawPie(Pens.Red, _
        X, Y, W, W, 315, -270)

    ' Northwest quadrant.
    Y += W + GAP
    X = 0
    gr.DrawEllipse(Pens.White, X, Y, W, W)
    gr.FillPie(slice_brush, _
        X, Y, W, W, 315, 45)
    gr.DrawPie(Pens.Red, _
        X, Y, W, W, 315, 45)

    ' Everything else.
    X += W + GAP
    gr.DrawEllipse(Pens.White, X, Y, W, W)
    gr.FillPie(slice_brush, _
        X, Y, W, W, 315, -315)
    gr.DrawPie(Pens.Red, _
        X, Y, W, W, 315, -315)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated