The RoundedCornerRectangle() method uses the GDI+ class to draw rectangles with rounded corners. It draws the graphic to a Graphics object that was passed to the method.
// using System.Drawing.Drawing2D
public void RoundedCornerRectangle(Graphics gfxObj, Pen penObj, float X, float Y, float RectWidth, float RectHeight, float CornerRadius)
{
GraphicsPath gfxPath = new GraphicsPath();
gfxPath.AddLine(X + CornerRadius, Y, X + RectWidth - (CornerRadius * 2), Y);
gfxPath.AddArc(X + RectWidth - (CornerRadius * 2), Y, CornerRadius * 2, CornerRadius * 2, 270, 90);
gfxPath.AddLine(X + RectWidth, Y + CornerRadius, X + RectWidth, Y + RectHeight - (CornerRadius * 2));
gfxPath.AddArc(X + RectWidth - (CornerRadius * 2), Y + RectHeight - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 0, 90);
gfxPath.AddLine(X + RectWidth - (CornerRadius * 2), Y + RectHeight, X + CornerRadius, Y + RectHeight);
gfxPath.AddArc(X, Y + RectHeight - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 90, 90);
gfxPath.AddLine(X, Y + RectHeight - (CornerRadius * 2), X, Y + CornerRadius);
gfxPath.AddArc(X, Y, CornerRadius * 2, CornerRadius * 2, 180, 90);
gfxPath.CloseFigure();
gfxObj.DrawPath(penObj, gfxPath);
gfxPath.Dispose();
}
This C# method, RoundedCornerRectangle
, is designed to draw a rectangle with rounded corners on a graphics surface. It’s intended to be used in a context where you’re doing custom drawing, like in a Windows Forms application. Let’s break down what each part of the code does:
- Method Signature:
public void RoundedCornerRectangle(Graphics gfxObj, Pen penObj, float X, float Y, float RectWidth, float RectHeight, float CornerRadius)
:public
: The method is public, meaning it can be called from outside the class it’s defined in.void
: The method doesn’t return any value.- Parameters:
Graphics gfxObj
: TheGraphics
object on which the drawing will be performed.Pen penObj
: ThePen
used to outline the rectangle.float X, float Y
: The x and y coordinates of the top-left corner of the rectangle.RectWidth, RectHeight
: The width and height of the rectangle.CornerRadius
: The radius of the rounded corners.
- Drawing the Rounded Rectangle:
GraphicsPath gfxPath = new GraphicsPath();
: Creates a newGraphicsPath
, which is a series of connected lines and curves. This path will define the outline of the rounded rectangle.- The following lines (
gfxPath.AddLine
andgfxPath.AddArc
) add segments to theGraphicsPath
to form the rounded rectangle:- Straight Lines:
gfxPath.AddLine
methods add straight line segments. These lines form the top, bottom, and sides of the rectangle, between the rounded corners. - Arcs for Corners:
gfxPath.AddArc
methods add arc segments for the corners. Each arc creates a quarter-circle at each corner, making them rounded. The parameters define the rectangle in which the arc is drawn, the start angle, and the sweep angle (all in degrees).
- Straight Lines:
gfxPath.CloseFigure();
: This closes the path by connecting the current point to the start point, making sure the shape is complete and closed.
- Drawing and Cleanup:
gfxObj.DrawPath(penObj, gfxPath);
: This line actually draws the path (our rounded rectangle) onto theGraphics
object (gfxObj
) using the specifiedPen
(penObj
).gfxPath.Dispose();
: Disposes of theGraphicsPath
object to free up resources.
This method is a good example of custom drawing in GDI+, a part of the .NET Framework used for graphics rendering. It shows how to create complex shapes (like a rectangle with rounded corners) by combining simpler shapes and lines. This method would be called within a paint event handler or similar context where you have a Graphics
object to draw on.