我很想在运行时创建一个填充的圆角矩形,并将其指定为Windows窗体中PictureBox(已创建和隐藏)的内容。如何在Windows Forms中使用VB.NET/C#在运行时创建一个圆角矩形?
你知道我该如何实现它吗?
我很想在运行时创建一个填充的圆角矩形,并将其指定为Windows窗体中PictureBox(已创建和隐藏)的内容。如何在Windows Forms中使用VB.NET/C#在运行时创建一个圆角矩形?
你知道我该如何实现它吗?
此方法填充上的图形对象(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
为了调用此函数,处理的PictureBox的绘制事件并通过e.Graphics对象作为第一个参数,如果你想让矩形完全填充你的图片框,那么图片框的界限就是第二个参数。
d参数改变边角的角度,我把它的值为30,你可以尝试不同的价值观......
而且,这里的一些代码来绘制(而不是填充)圆角矩形:
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
最简单的方法是使用Graphics对象即时创建一个Bitmap。 DrawEllipse方法应该足够了。
然后将该位图分配为PictureBox对象的内容。
标记为答案的填充解决方案的问题在于,它不适用于非固体/均匀刷子。这是基于GraphicsPath类至极我认为另外一个更可重复使用:
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;
}
,这里是为借鉴,只有(不填)的代码,基于同样的想法:
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;
}
哎元 - 谢谢你的闪电安慰! :D 代码工作得很好。 只是另一个问题,如果我不想立即画它,但只有当应用程序处于特定状态时,我该怎么办?我是否也应该在这种情况下使用Paint事件? – Drake 2009-06-26 14:52:34