첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170
#region 둥근 사각형 그리기 - DrawRoundedRectangle(graphics, rectangle, brush, pen, radiusX, radiusY, roundUpperLeft, roundUpperRight, roundLowerRight, roundLowerLeft)

/// <summary>
/// 둥근 사각형 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="rectangle">사각형</param>
/// <param name="brush">브러시</param>
/// <param name="pen">펜</param>
/// <param name="radiusX">반경 X</param>
/// <param name="radiusY">반경 Y</param>
/// <param name="roundUpperLeft">좌상단 라운드 여부</param>
/// <param name="roundUpperRight">우상단 라운드 여부</param>
/// <param name="roundLowerRight">우하단 라운드 여부</param>
/// <param name="roundLowerLeft">좌하단 라운드 여부</param>
public void DrawRoundedRectangle
(
    Graphics   graphics,
    RectangleF rectangle,
    Brush      brush,
    Pen        pen,
    float      radiusX,
    float      radiusY,
    bool       roundUpperLeft,
    bool       roundUpperRight,
    bool       roundLowerRight,
    bool       roundLowerLeft
)
{
    GraphicsPath path = GetRoundedRectanglePath
    (
        rectangle,
        radiusX,
        radiusY,
        roundUpperLeft,
        roundUpperRight,
        roundLowerRight,
        roundLowerLeft
    );

    graphics.FillPath(brush, path);

    graphics.DrawPath(pen, path);
}

#endregion
#region 둥근 사각형 그리기 - DrawRoundedRectangle(Graphics, rectangle, brush, pen, radius)

/// <summary>
/// 둥근 사각형 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="rectangle">사각형</param>
/// <param name="brush">브러시</param>
/// <param name="pen">펜</param>
/// <param name="radius">반경</param>
public void DrawRoundedRectangle(Graphics graphics, RectangleF rectangle, Brush brush, Pen pen, float radius)
{
    DrawRoundedRectangle(graphics, rectangle, brush, pen, radius, radius, true, true, true, true);
}

#endregion
#region 라운드 사각형 패스 구하기 - GetRoundedRectanglePath(rectangle, radiusX, radiusY, roundUpperLeft, roundUpperRight, roundLowerRight, roundLowerLeft)

/// <summary>
/// 라운드 사각형 패스 구하기
/// </summary>
/// <param name="rectangle">사각형</param>
/// <param name="radiusX">반경 X</param>
/// <param name="radiusY">반경 Y</param>
/// <param name="roundUpperLeft">좌상단 라운드 여부</param>
/// <param name="roundUpperRight">우상단 라운드 여부</param>
/// <param name="roundLowerRight">우하단 라운드 여부</param>
/// <param name="roundLowerLeft">좌하단 라운드 여부</param>
/// <returns>라운드 사각형 패스</returns>
private GraphicsPath GetRoundedRectanglePath
(
    RectangleF rectangle,
    float      radiusX,
    float      radiusY,
    bool       roundUpperLeft,
    bool       roundUpperRight,
    bool       roundLowerRight,
    bool       roundLowerLeft
)
{
    PointF point1;
    PointF point2;

    GraphicsPath path = new GraphicsPath();

    if(roundUpperLeft)
    {
        RectangleF cornerRectangle = new RectangleF
        (
            rectangle.X,
            rectangle.Y,
            2 * radiusX,
            2 * radiusY
        );

        path.AddArc(cornerRectangle, 180, 90);

        point1 = new PointF(rectangle.X + radiusX, rectangle.Y);
    }
    else
    {
        point1 = new PointF(rectangle.X, rectangle.Y);
    }

    if(roundUpperRight)
    {
        point2 = new PointF(rectangle.Right - radiusX, rectangle.Y);
    }
    else
    {
        point2 = new PointF(rectangle.Right, rectangle.Y);
    }

    path.AddLine(point1, point2);

    if(roundUpperRight)
    {
        RectangleF cornerRectangle = new RectangleF
        (
            rectangle.Right - 2 * radiusX,
            rectangle.Y,
            2 * radiusX,
            2 * radiusY
        );

        path.AddArc(cornerRectangle, 270, 90);

        point1 = new PointF(rectangle.Right, rectangle.Y + radiusY);
    }
    else
    {
        point1 = new PointF(rectangle.Right, rectangle.Y);
    }

    if(roundLowerRight)
    {
        point2 = new PointF(rectangle.Right, rectangle.Bottom - radiusY);
    }
    else
    {
        point2 = new PointF(rectangle.Right, rectangle.Bottom);
    }

    path.AddLine(point1, point2);

    if(roundLowerRight)
    {
        RectangleF cornerRectangle = new RectangleF
        (
            rectangle.Right  - 2 * radiusX,
            rectangle.Bottom - 2 * radiusY,
            2 * radiusX,
            2 * radiusY
        );

        path.AddArc(cornerRectangle, 0, 90);

        point1 = new PointF(rectangle.Right - radiusX, rectangle.Bottom);
    }
    else
    {
        point1 = new PointF(rectangle.Right, rectangle.Bottom);
    }

    if(roundLowerLeft)
    {
        point2 = new PointF(rectangle.X + radiusX, rectangle.Bottom);
    }
    else
    {
        point2 = new PointF(rectangle.X, rectangle.Bottom);
    }

    path.AddLine(point1, point2);

    if(roundLowerLeft)
    {
        RectangleF cornerRectangle = new RectangleF
        (
            rectangle.X,
            rectangle.Bottom - 2 * radiusY,
            2 * radiusX,
            2 * radiusY
        );

        path.AddArc(cornerRectangle, 90, 90);

        point1 = new PointF(rectangle.X, rectangle.Bottom - radiusY);
    }
    else
    {
        point1 = new PointF(rectangle.X, rectangle.Bottom);
    }

    if(roundUpperLeft)
    {
        point2 = new PointF(rectangle.X, rectangle.Y + radiusY);
    }
    else
    {
        point2 = new PointF(rectangle.X, rectangle.Y);
    }

    path.AddLine(point1, point2);

    path.CloseFigure();

    return path;
}

#endregion
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요