2009-03-09 13 views
17

Ho un metodo che disegna un rettangolo arrotondato con un bordo. Il bordo può essere di qualsiasi larghezza, quindi il problema che sto avendo è che il bordo si estende oltre i limiti dati quando è spesso perché è disegnato dal centro di un tracciato.Come disegnare un rettangolo arrotondato con bordo di larghezza variabile all'interno di limiti specifici

Come includere la larghezza del bordo in modo che si adatti perfettamente ai limiti specificati?

Ecco il codice che sto usando per disegnare il rettangolo arrotondato.

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

risposta

29

ragazzi Va bene, ho capito! Basta ridurre i limiti per tenere conto della larghezza della penna. Sapevo che questa era la risposta che stavo chiedendo se ci fosse un modo per tracciare una linea all'interno di un percorso. Funziona bene però.

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