2009-06-26 19 views

risposta

8

Questo metodo riempie un rettangolo arrotondato su un oggetto grafico (codice VB):

Public Sub FillRoundedRectangle(ByVal g As Drawing.Graphics, ByVal r As Rectangle, ByVal d As Integer, ByVal b As Brush) 
    Dim mode As Drawing2D.SmoothingMode = g.SmoothingMode 
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed 
    g.FillPie(b, r.X, r.Y, d, d, 180, 90) 
    g.FillPie(b, r.X + r.Width - d, r.Y, d, d, 270, 90) 
    g.FillPie(b, r.X, r.Y + r.Height - d, d, d, 90, 90) 
    g.FillPie(b, r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90) 
    g.FillRectangle(b, CInt(r.X + d/2), r.Y, r.Width - d, CInt(d/2)) 
    g.FillRectangle(b, r.X, CInt(r.Y + d/2), r.Width, CInt(r.Height - d)) 
    g.FillRectangle(b, CInt(r.X + d/2), CInt(r.Y + r.Height - d/2), CInt(r.Width - d), CInt(d/2)) 
    g.SmoothingMode = mode 
End Sub 

Per chiamare questa funzione, gestire l'evento vernice del picturebox e passare l'e.Graphics oggetto come primo argomento, e i limiti della casella immagine come secondo argomento se si desidera che il rettangolo riempia completamente la casella dell'immagine.

Il parametro d cambia angolo di curva, io lo chiamo con un valore di 30, si può provare diversi valori ...

Inoltre, ecco qualche codice per disegnare (invece di riempimento) un rettangolo arrotondato:

Public Sub DrawRoundedRectangle(ByVal g As Drawing.Graphics, ByVal r As Rectangle, ByVal d As Integer, ByVal p As Pen) 
    g.DrawArc(p, r.X, r.Y, d, d, 180, 90) 
    g.DrawLine(p, CInt(r.X + d/2), r.Y, CInt(r.X + r.Width - d/2), r.Y) 
    g.DrawArc(p, r.X + r.Width - d, r.Y, d, d, 270, 90) 
    g.DrawLine(p, r.X, CInt(r.Y + d/2), r.X, CInt(r.Y + r.Height - d/2)) 
    g.DrawLine(p, CInt(r.X + r.Width), CInt(r.Y + d/2), CInt(r.X + r.Width), CInt(r.Y + r.Height - d/2)) 
    g.DrawLine(p, CInt(r.X + d/2), CInt(r.Y + r.Height), CInt(r.X + r.Width - d/2), CInt(r.Y + r.Height)) 
    g.DrawArc(p, r.X, r.Y + r.Height - d, d, d, 90, 90) 
    g.DrawArc(p, r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90) 
End Sub 
+0

hey Meta -Knight grazie mille per il tuo fulmine! : D Il codice funziona molto bene. Solo un'altra domanda, come posso fare se non voglio dipingerlo immediatamente ma solo quando l'applicazione si trova in un certo stato? Dovrei usare anche in questo caso l'evento Paint? – Drake

+0

Si potrebbe avere un valore booleano nella classe (diciamo che è chiamato mustPaint) che si imposta quando si desidera dipingere il rettangolo, quindi è possibile aggiungere una condizione nell'evento Paint: se mustPaint quindi [dipingere rettangolo arrotondato qui] fine se –

+0

ok, ci proverò, grazie – Drake

0

Il modo più semplice per eseguire questa operazione consiste nel creare una bitmap al volo utilizzando l'oggetto Graphics. Il metodo DrawEllipse dovrebbe essere sufficiente.

Quindi assegnare tale bitmap come contenuto dell'oggetto PictureBox.

13

Il problema con la soluzione di riempimento contrassegnata come risposta è che non funziona bene con i pennelli non solido/uniforme. Ecco un altro uno basato sul wich classe GraphicsPath credo sia più riutilizzabile:

public static void FillRoundedRectangle(Graphics graphics, Rectangle rectangle, Brush brush, int radius) 
{ 
    if (graphics == null) 
     throw new ArgumentNullException("graphics"); 

    SmoothingMode mode = graphics.SmoothingMode; 
    graphics.SmoothingMode = SmoothingMode.AntiAlias; 

    using (GraphicsPath path = RoundedRectangle(rectangle, radius)) 
    { 
     graphics.FillPath(brush, path); 
    } 
    graphics.SmoothingMode = mode; 
} 

public static GraphicsPath RoundedRectangle(Rectangle r, int radius) 
{ 
    GraphicsPath path = new GraphicsPath(); 
    int d = radius * 2; 

    path.AddLine(r.Left + d, r.Top, r.Right - d, r.Top); 
    path.AddArc(Rectangle.FromLTRB(r.Right - d, r.Top, r.Right, r.Top + d), -90, 90); 
    path.AddLine(r.Right, r.Top + d, r.Right, r.Bottom - d); 
    path.AddArc(Rectangle.FromLTRB(r.Right - d, r.Bottom - d, r.Right, r.Bottom), 0, 90); 
    path.AddLine(r.Right - d, r.Bottom, r.Left + d, r.Bottom); 
    path.AddArc(Rectangle.FromLTRB(r.Left, r.Bottom - d, r.Left + d, r.Bottom), 90, 90); 
    path.AddLine(r.Left, r.Bottom - d, r.Left, r.Top + d); 
    path.AddArc(Rectangle.FromLTRB(r.Left, r.Top, r.Left + d, r.Top + d), 180, 90); 
    path.CloseFigure(); 
    return path; 
} 

ed ecco il codice per Draw solo (non compilare), sulla base della stessa idea:

public static void DrawRoundedRectangle(Graphics graphics, Rectangle rectangle, Pen pen, int radius) 
{ 
    if (graphics == null) 
     throw new ArgumentNullException("graphics"); 

    SmoothingMode mode = graphics.SmoothingMode; 
    graphics.SmoothingMode = SmoothingMode.AntiAlias; 

    using (GraphicsPath path = RoundedRectangle(rectangle, radius)) 
    { 
     graphics.DrawPath(pen, path); 
    } 
    graphics.SmoothingMode = mode; 
} 
Problemi correlati