UPanel圆角面板
容器,可设置边框圆角度、边框(粗细、颜色)、背景色(纯色或渐变)、渐变模式
创建过程:
1.设置控件样式: AllPaintingInWmPaint、OptimizedDoubleBuffer、UserPaint、ResizeRedraw、SupportsTransparentBackColor
2.属性扩展: BgColor 背景色1、 BorderColor 边框颜色、BorderWidth边框粗细、BgColor2(第二 种背景色)、Radius圆角半径、GradientMode 渐变模式
3.重写OnSizeChanged设置控件区域控件绘制区
4.重写OnPaint绘制面板–边框、背景填充
public class PaintClass
{
public 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;
}
}
public partial class UPanel : Panel
{
public UPanel()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint,true);//忽略窗口消息,减少闪烁
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);//绘制到缓存区,减少闪烁
SetStyle(ControlStyles.UserPaint,true);//控件由其自身,而不是操作系统绘制
SetStyle(ControlStyles.ResizeRedraw, true);//控件调整大小时重绘
SetStyle(ControlStyles.SupportsTransparentBackColor, true);//支持透明背景
}
private Color bgColor = Color.LightGray;
[DefaultValue(typeof(Color),"LightGray"),Description("控件的背景色1")]
public Color BgColor
{
get { return bgColor; }
set
{
bgColor = value;
//Refresh();//立即重绘
Invalidate();//引发重绘,不会立即执行
}
}
private Color bgColor2 = Color.Transparent;
[DefaultValue(typeof(Color), "Transparent"), Description("控件的背景色2")]
public Color BgColor2
{
get { return bgColor2; }
set
{
bgColor2 = value;
Invalidate();//引发重绘,不会立即执行
}
}
private Color borderColor = Color.Gray;
[DefaultValue(typeof(Color), "Gray"), Description("控件的边框颜色")]
public Color BorderColor
{
get { return borderColor; }
set
{
borderColor = value;
Invalidate();//引发重绘,不会立即执行
}
}
private int borderWidth = 0;
[DefaultValue(typeof(int), "0"), Description("控件的边框粗细")]
public int BorderWidth
{
get { return borderWidth; }
set
{
borderWidth = value;
Invalidate();//引发重绘,不会立即执行
}
}
private int radius = 5;
[DefaultValue(typeof(int), "5"), Description("控件的边框圆角半径")]
public int Radius
{
get { return radius; }
set
{
radius = value;
Invalidate();//引发重绘,不会立即执行
}
}
private LinearGradientMode gradientMode = LinearGradientMode.Vertical;
[DefaultValue(typeof(LinearGradientMode), "Vertical"), Description("控件的背景渐变模式")]
public LinearGradientMode GradientMode
{
get { return gradientMode; }
set
{
gradientMode = value;
Invalidate();//引发重绘,不会立即执行
}
}
Rectangle r;//绘制区域
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
r = this.ClientRectangle; //获取当前绘制区域
this.Region = new Region(r);
r.Width -= 1;
r.Height -= 1;
}
//重写重绘
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//绘制对象
Graphics g = e.Graphics;
//呈现质量 HighQuality AntiAlias
g.SmoothingMode = SmoothingMode.HighQuality;
GraphicsPath path = new GraphicsPath();
GraphicsPath path2 = new GraphicsPath();
//内部填充矩形
Rectangle rect;
//生成外部矩形的路径
path = PaintClass.GetRoundRectangle(r, radius);
if (this.BorderWidth > 0)
{
//填充外部矩形---边框
g.FillPath(new SolidBrush(BorderColor), path);
//定义内部矩形
rect = new Rectangle(r.X + BorderWidth, r.Y + BorderWidth, r.Width - 2 * borderWidth, r.Height - 2 * borderWidth);
//生成内部矩形的圆角路径
path2 = PaintClass.GetRoundRectangle(rect, radius - 1);
}
else //无边框时的内部矩形
{
path2 = path;
rect = r;
}
if (this.BgColor2 != Color.Transparent)
{
//线型渐变画刷
LinearGradientBrush bgBrush = new LinearGradientBrush(rect, BgColor, BgColor2, GradientMode);
g.FillPath(bgBrush, path2);//填充圆角矩形内部
}
else
{
Brush b = new SolidBrush(BgColor);
g.FillPath(b, path2);//填充圆角矩形内部
}
}
}
``