我很想在運行時創建一個填充的圓角矩形,並將其指定爲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