绘制圆角矩形 ,拷贝就能使用
///
/// 绘制Panel容器
///
///
///
private void Set_Paint(object sender, PaintEventArgs e)
{
//圆角矩形实心颜色,交叉对角线的模式:HatchStyle.DiagonalCross,Color.White前景色、Color.Red背景色
Brush brush = new HatchBrush(HatchStyle.DiagonalCross, Color.White, Color.Red);
//圆角矩形边框颜色
Pen pen = new Pen(Color.Black, 2);
pen.DashStyle = DashStyle.Dash;
//初始化确定圆角实心矩形初始X、Y位置和Width、height宽高
Rectangle rectangle = new Rectangle(4,4,82,44);
//绘制圆角矩形、实现方法
FillRoundRectangle(e.Graphics, rectangle, brush, pen, 15);
}
///
/// C# GDI+ 绘制圆角实心矩形
///
/// Graphics 对象
/// 要填充的矩形
/// 填充背景色和样式
/// 外部边框颜色和样式
/// 圆角半径
public void FillRoundRectangle(Graphics g, Rectangle rectangle, Brush brush,Pen pen, int r)
{
rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width - 1, rectangle.Height - 1); //确定圆角实心矩形初始X、Y位置和Width、height宽高
g.FillPath(brush, GetRoundRectangle(rectangle, r)); //绘制圆角实心矩形
g.DrawPath(pen, GetRoundRectangle(rectangle, r)); //绘制圆角实心矩形边框
Brush brush_word = new SolidBrush(Color.Black); //绘制的字体颜色(new记得放在方法外)
g = this.panel.CreateGraphics();//为panel容器创建Graphics对象
Font ft = new Font("黑体", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));//定义字体
g.DrawString("Hello", ft, brush_word, (rectangle.Width / 2) - 10, (rectangle.Height / 2) - 5);
}
///
/// 根据普通矩形得到圆角矩形的路径
///
/// 原始矩形
/// 半径
/// 图形路径
private static GraphicsPath GetRoundRectangle(Rectangle rectangle, int r)
{
int l = 2 * r;
// 把圆角矩形分成八段直线、弧的组合,依次加到路径中
GraphicsPath gp = new GraphicsPath();
gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y));
gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F);
gp.AddLine(new Point(rectangle.Right, rectangle.Y + r), new Point(rectangle.Right, rectangle.Bottom - r));
gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F);
gp.AddLine(new Point(rectangle.Right - r, rectangle.Bottom), new Point(rectangle.X + r, rectangle.Bottom));
gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F);
gp.AddLine(new Point(rectangle.X, rectangle.Bottom - r), new Point(rectangle.X, rectangle.Y + r));
gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F);
return gp;
}
效果图:边框、颜色、内部字符串按实际情况定义即可,一般会满足各种情况