Winform绘制圆角矩形控件
1、选择自定义控件
2、
public partial class UCirleButton : Button { public UCirleButton() { InitializeComponent(); } private Color bgColor = Color.Gray; [DefaultValue(typeof(Color), "Gray"), Description("控件颜色")] public Color BgColor { get { return bgColor; } set { bgColor = value; 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.Red; [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), "0"), 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(); } } private const int WM_PAINT = 0xf; protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_PAINT) { //重绘边框 if (this.radius > 0) { using (Graphics gp = Graphics.FromHwnd(this.Handle)) { //消除锯齿 gp.SmoothingMode = SmoothingMode.AntiAlias; Rectangle r = new Rectangle(); r.Width = this.Width; r.Height = this.Height; //绘制边框和对象 DrawBorder(gp, r, this.radius); } } } } private void DrawBorder(Graphics gp, Rectangle rect, int radius) { rect.Width -= 1; rect.Height -= 1; GraphicsPath path = new GraphicsPath(); //绘制圆角矩形路径 path = PaintCommon.GetRoundRectangle(rect, radius); if (BorderWidth > 0) { using (Pen pen = new Pen(this.borderColor, BorderWidth)) { //绘制边框 gp.DrawPath(pen, path); } } //背景区域的矩形 Rectangle rect1 = new Rectangle(rect.X + BorderWidth, rect.Y + BorderWidth, rect.Width - 2 * BorderWidth, rect.Height - 2 * BorderWidth); //绘制背景 DrawBackCorol(gp, rect1, this.radius); //绘制按钮文本 StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; gp.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), rect1, format); } /// <summary> /// 绘制背景 /// </summary> /// <param name="gp"></param> /// <param name="rect"></param> /// <param name="radius"></param> private void DrawBackCorol(Graphics gp, Rectangle rect, int radius) { //获取背景区域圆角矩形 GraphicsPath path = PaintCommon.GetRoundRectangle(rect, radius); if (this.BgColor2 != Color.Transparent) { //线性渐变画刷 LinearGradientBrush bush = new LinearGradientBrush(rect, BgColor, BgColor2, gradientMode); //填充圆角矩形内部 gp.FillPath(bush, path); } else { Brush b = new SolidBrush(BgColor); gp.FillPath(b, path); } } }
public class PaintCommon { public static GraphicsPath GetRoundRectangle(Rectangle rectangle,int r) { int l = 2 * r; //把圆角矩形分成8段直线、弧的组合、一次加到路径中 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; } }