2009-06-26 26 views

Respuesta

8

Este método llena un rectángulo redondeado en un objeto gráfico (código 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 

llamar a esta función, controlar el evento de pintura de la PictureBox y pasar el e.Graphics objeto como el primer argumento, y los límites del cuadro de imagen como el segundo argumento si desea que el rectángulo complete su cuadro de imagen por completo.

El parámetro d cambia el ángulo de las esquinas, lo llamo con un valor de 30, se puede probar diferentes valores ...

Además, aquí hay un código para dibujar (en lugar de relleno) un rectángulo redondeado:

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

bueno Meta -Knight ¡muchas gracias por tu rayo de luz! : D El código está funcionando muy bien. Solo otra pregunta, ¿cómo puedo hacer si no quiero pintarlo inmediatamente pero solo cuando la aplicación se encuentra en cierto estado? ¿Debo usar también en este caso el evento Paint? – Drake

+0

Puede tener un valor booleano en su clase (digamos que se llama mustPaint) que establece cuando desea que se pinte el rectángulo, entonces puede agregar una condición en el evento Paint: if mustPaint then [pintar el rectángulo redondeado aquí] end si –

+0

está bien, lo intentaré, gracias – Drake

0

La manera más fácil de hacerlo es crear un mapa de bits sobre la marcha utilizando el objeto Graphics. El método DrawEllipse debería ser suficiente.

A continuación, asigne ese mapa de bits como el contenido del objeto PictureBox.

13

El problema con la solución de relleno que está marcada como la respuesta es que no funciona bien con cepillos no sólidos/uniformes. Aquí es otra basada en el wich clase GraphicsPath creo que es más reutilizable:

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

y aquí está el código para el sorteo (no relleno), basado en la misma 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; 
} 
Cuestiones relacionadas