使用API也可以:[DllImport("gdi32.Dll")] public static extern int CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
调用Gdi的API(RoundRect)可以直接画出: public partial class Form1 : Form { [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern bool RoundRect(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight); public Form1() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); RoundRect(e.Graphics.GetHdc(), 20, 20, 100, 100, 20, 20); } }
API的可以直接绘制出来么?
如下代码入到你的窗体里就可以直接运行出来了: [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern bool RoundRect(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight); protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); RoundRect(e.Graphics.GetHdc(), 20, 20, 100, 100, 20, 20); }
{
Bitmap bm = new Bitmap(800, 600);
Graphics g = Graphics.FromImage(bm);
g.FillRectangle(Brushes.White,new Rectangle(0,0,800,600));
FillRoundRectangle(g,Brushes.Plum,new Rectangle(100, 100, 100, 100), 8);
DrawRoundRectangle(g, Pens.Yellow,new Rectangle(100, 100, 100, 100), 8);
bm.Save(Response.OutputStream, ImageFormat.Jpeg);
g.Dispose();
bm.Dispose();
}
public static void DrawRoundRectangle(Graphics g,Pen pen,Rectangle rect, int cornerRadius)
{
using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
{
g.DrawPath(pen, path);
}
}
public static void FillRoundRectangle(Graphics g, Brush brush,Rectangle rect, int cornerRadius)
{
using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
{
g.FillPath(brush, path);
}
}
internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
{
GraphicsPath roundedRect = new GraphicsPath();
roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
roundedRect.CloseFigure();
return roundedRect;
}
public static extern int CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
public partial class Form1 : Form
{
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern bool RoundRect(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight);
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
RoundRect(e.Graphics.GetHdc(), 20, 20, 100, 100, 20, 20);
}
}
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern bool RoundRect(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight);
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
RoundRect(e.Graphics.GetHdc(), 20, 20, 100, 100, 20, 20);
}
{
base.OnPaint(e);
//Pen的作成
Pen blackPen = new Pen(Color.Black, 10);
//设定LineJoin属性为Bevel描绘四角形
blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;
e.Graphics.DrawRectangle(blackPen, new Rectangle(70, 10, 40, 40));
//设定LineJoin属性为Miter描绘四角形
blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Miter;
e.Graphics.DrawRectangle(blackPen, new Rectangle(130, 10, 40, 40));
//设定LineJoin属性为MiterClipped描绘四角形
blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.MiterClipped;
e.Graphics.DrawRectangle(blackPen, new Rectangle(190, 10, 40, 40));
//设定LineJoin属性为Round描绘四角形(角为圆弧)
blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
e.Graphics.DrawRectangle(blackPen, new Rectangle(250, 10, 40, 40));
}
结果如下图所示。