画带圆弧的矩形就不能使用自带的方法了,自带的Graphics画出的矩形是方方正正,不带圆弧的,废话不多说,上代码:
private void Form1_Paint(object sender, PaintEventArgs e) { Rectangle ren = new Rectangle(100, 100, 300, 250); Draw(ren, e.Graphics, 16); } private void Draw(Rectangle rectangle, Graphics g, int _radius) { Pen shadowPen = new Pen(Color.Blue,5); var gpath = DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - 2, rectangle.Height - 1, _radius); g.DrawPath(shadowPen, gpath); g.FillPath(Brushes.Blue, gpath); } public static GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius) { GraphicsPath gp = new GraphicsPath(); gp.AddArc(x, y, radius, radius, 180, 90); gp.AddArc(width - radius, y, radius, radius, 270, 90); gp.AddArc(width - radius, height - radius, radius, radius, 0, 90); gp.AddArc(x, height - radius, radius, radius, 90, 90); gp.CloseAllFigures(); return gp; }
运行效果: