2009-03-09 7 views
17

테두리가있는 둥근 사각형을 그리는 방법이 있습니다. 테두리는 임의의 너비가 될 수 있으므로, 경로의 중심에서부터 그려지기 때문에 경계가 두꺼울 때 경계가 지나치게 확장되는 문제가 있습니다.경계 너비가 가변 인 둥근 사각형 그리기 방법

테두리의 폭을 포함시켜 주어진 경계에 완벽하게 맞출 수있는 방법은 무엇입니까?

다음은 둥근 사각형을 그릴 때 사용하는 코드입니다.

private void DrawRoundedRectangle(Graphics gfx, Rectangle Bounds, int CornerRadius, Pen DrawPen, Color FillColor) 
{ 
    GraphicsPath gfxPath = new GraphicsPath(); 

    DrawPen.EndCap = DrawPen.StartCap = LineCap.Round; 

    gfxPath.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90); 
    gfxPath.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90); 
    gfxPath.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90); 
    gfxPath.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90); 
    gfxPath.CloseAllFigures(); 

    gfx.FillPath(new SolidBrush(FillColor), gfxPath); 
    gfx.DrawPath(DrawPen, gfxPath); 
} 

답변

29

괜찮 았어! 펜의 너비를 고려하여 범위를 줄여야합니다. 저는 이것이 경로 안쪽에 선을 그리는 방법이 있다면 궁금한 대답이라고 생각했습니다. 그래도 잘 작동합니다.

private void DrawRoundedRectangle(Graphics gfx, Rectangle Bounds, int CornerRadius, Pen DrawPen, Color FillColor) 
{ 
    int strokeOffset = Convert.ToInt32(Math.Ceiling(DrawPen.Width)); 
    Bounds = Rectangle.Inflate(Bounds, -strokeOffset, -strokeOffset); 

    DrawPen.EndCap = DrawPen.StartCap = LineCap.Round; 

    GraphicsPath gfxPath = new GraphicsPath(); 
    gfxPath.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90); 
    gfxPath.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90); 
    gfxPath.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90); 
    gfxPath.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90); 
    gfxPath.CloseAllFigures(); 

    gfx.FillPath(new SolidBrush(FillColor), gfxPath); 
    gfx.DrawPath(DrawPen, gfxPath); 
} 
관련 문제