2009-06-26 11 views

Antwort

8

Dieses Verfahren füllt ein Rechteck mit abgerundeten Ecken auf einem Grafikobjekt (VB-Code):

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 

diese Funktion aufzurufen, um die Farbe des Ereignisses picturebox Griff und der e.Graphics als erstes Argument Objekt übergeben, und die Begrenzungen der Bildbox als zweites Argument, wenn das Rechteck Ihre Bildbox vollständig füllen soll.

Der d-Parameter ändert sich der Winkel Ecken, ich habe es mit einem Wert von 30 aufrufen, können Sie verschiedene Werte versuchen ...

Auch hier ist ein Code (statt fill) ein abgerundetes Rechteck zu zeichnen:

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 vielen Dank für Ihren Blitz anwser! : D Der Code funktioniert sehr gut. Nur eine weitere Frage, wie kann ich tun, wenn ich es nicht sofort malen möchte, sondern nur, wenn die Anwendung in einem bestimmten Zustand ist? Sollte ich auch in diesem Fall das Paint-Event verwenden? – Drake

+0

Sie könnten einen booleschen Wert in Ihrer Klasse haben (sagen wir mustPaint), den Sie setzen, wenn das Rechteck gezeichnet werden soll. Dann können Sie eine Bedingung im Paint-Ereignis hinzufügen: Wenn mustPaint dann [gerundetes Rechteck hier malen] endet wenn –

+0

ok, ich werde es versuchen, danke – Drake

0

Der einfachste Weg, dies zu tun, ist ein Bitmap im laufenden Betrieb mit dem Graphics-Objekt zu erstellen. Die DrawEllipse-Methode sollte ausreichen.

Dann weisen Sie diese Bitmap als Inhalt des PictureBox-Objekts zu.

13

Das Problem mit der Fülllösung, die als Antwort markiert ist, ist, dass es nicht gut mit nicht festen/einheitlichen Pinseln funktioniert. Hier ist eine andere auf der Grundlage der Graphic Klasse Wich ich denke, ist mehr wiederverwendbar:

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

und hier ist der Code für Draw nur (nicht ausfüllen), basierend auf der gleichen Idee:

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