C#•Round Out the Edges

Listing 3. A GraphicsPath object creates shapes represented by a series of lines and curves. The roundness parameter in the GetRoundBox method dictates how round the edges should be.

private GraphicsPath GetRoundBox(Rectangle rect, 
   int roundness) 
{

   // Create a GraphicsPath object, which
   // represents a series of lines and curves,
   // to create a shape
   GraphicsPath result = new GraphicsPath();

   if (roundness > 1) 
   {
      // Get a floating point number
      // from the roundness parameter, used
      // to create the SizeF struct's values
      roundness = Math.Min(Math.Min(rect.Width, 
         rect.Height)/3, roundness);
      float diameter = roundness * 2.0F; 
      SizeF sizeF = new SizeF( diameter, diameter );

      // Create a rectangleF struct
      // with the size of the ListBox
      RectangleF arc = new 
         RectangleF( rect.Location, sizeF ); 

      // Create and add a series of
      // curves for the right, left, top,
      // and bottom of the ListBox, at 
      // the coordinates determined by the
      // Rectangle
      result.AddArc( arc, 180, 90 ); 
      arc.X = rect.Right-diameter; 
      result.AddArc( arc, 270, 90 ); 
      arc.Y = rect.Bottom-diameter; 
      result.AddArc( arc, 0, 90 ); 
      arc.X = rect.Left;     
      result.AddArc( arc, 90, 90 );     

      // Make sure the final line is drawn
      // from the last drawn line to the
      // first drawn line
      result.CloseFigure(); 
   }
   else 
   {
      result.AddRectangle(rect);
   }

   return result;
}