2009-03-09 24 views
17

Tôi có phương pháp vẽ hình chữ nhật tròn với đường viền. Đường viền có thể là bất kỳ chiều rộng nào, do đó, vấn đề tôi gặp phải là đường viền đang mở rộng qua các giới hạn nhất định khi đường viền dày vì nó được vẽ từ giữa đường dẫn.Cách vẽ hình chữ nhật tròn với đường viền có chiều rộng thay đổi bên trong các giới hạn cụ thể

Tôi sẽ bao gồm chiều rộng của đường viền sao cho nó vừa khít với các giới hạn nhất định?

Đây là mã tôi đang sử dụng để vẽ hình chữ nhật tròn.

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); 
} 

Trả lời

29

Được rồi, tôi đã tìm ra! Chỉ cần thu nhỏ các giới hạn để tính đến chiều rộng của cây bút. Tôi biết đây là câu trả lời tôi chỉ tự hỏi liệu có cách nào vẽ đường thẳng bên trong con đường không. Điều này hoạt động tốt mặc dù.

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); 
} 
Các vấn đề liên quan