圆角面板
1 效果如


2 代码编写
public partial class UPanel : Panel { public UPanel() { InitializeComponent(); } private Color bgColor = Color.Gray; /// <summary> /// 背景色(渐变色中,颜色1) /// </summary> [DefaultValue(typeof(Color), "Gray"), Description("控件背景色")] public Color BgColor { get { return bgColor; } set { bgColor = value; Refresh(); } } private Color borderColor = Color.Red; [DefaultValue(typeof(Color), "Gray"), Description("控件边框颜色")] public Color BorderColor { get { return borderColor; } set { borderColor = value; Refresh(); } } private int borderWidth = 0; [DefaultValue(typeof(int), "0"), Description("控件边框粗细")] public int BorderWidth { get { return borderWidth; } set { borderWidth = value; Refresh(); } } [DefaultValue(typeof(Color), "Gray"), Description("控件背景颜色2")] private Color bgColor2 = Color.White; public Color BgColor2 { get { return bgColor2; } set { bgColor2 = value; Refresh(); } } [DefaultValue(typeof(int), "5"), Description("圆角弧度大小")] private int radius = 5; public int Radius { get { return radius; } set { radius = value; Refresh(); } } [DefaultValue(typeof(LinearGradientMode), "Vertical"), Description("渐变方式")] private LinearGradientMode gradientMode = LinearGradientMode.Vertical; public LinearGradientMode GradientMode { get { return gradientMode; } set { gradientMode = value; Invalidate(); } } Rectangle r; protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); r = new Rectangle(0, 0, this.Width, this.Height); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.HighQuality; r.Width = this.Width; r.Height = this.Height; r.Width -= 1; r.Height -= 1; GraphicsPath path = new GraphicsPath(); GraphicsPath path2 = new GraphicsPath(); Rectangle rect; if (this.BorderWidth > 0) { path = PaintClass.GetRoundRectangle(r, radius); using (Pen pen = new Pen(this.BorderColor, BorderWidth)) { g.DrawPath(pen, 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 = PaintClass.GetRoundRectangle(r, radius); 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);//填充圆角矩形内部 } } }

浙公网安备 33010602011771号