C# Winform实现圆角无锯齿按钮

 更新时间:2022年08月29日 09:24:10   作者:liaogaobo2008  
这篇文章主要介绍了C# Winform实现圆角无锯齿按钮,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下

前言:

发现用Winform做一个圆角按钮遇到麻烦,主要是锯齿问题,后面想了想办法解决问题了。

主要方法是按钮的区域通过Region指定,但按钮需要自己画,否则怎么搞都出现锯齿,网上有朋友提供一个漂亮的方案,可是代码不完整无法使用,我的解决方案现在分享如下:

  public enum ControlState { Hover , Normal, Pressed }
    public class RoundButton : Button
    {
             private int radius;//半径 
        private Color _baseColor = Color.FromArgb(51, 161, 224);//基颜色
        private Color _hoverColor= Color.FromArgb(51, 0, 224);//基颜色
        private Color _normalColor = Color.FromArgb(0, 161, 224);//基颜色
        private Color _pressedColor = Color.FromArgb(51, 161, 0);//基颜色
        //圆形按钮的半径属性
        [CategoryAttribute("布局"), BrowsableAttribute(true), ReadOnlyAttribute(false)]
        public int Radius
        {
            set
            {
                radius = value;
                this.Invalidate();
            }
            get
            {
                return radius;
            }
        }
        [DefaultValue(typeof(Color), "51, 161, 224")]
        public Color NormalColor
        {
            get
            {
                return this._normalColor;
            }
            set
            {
                this._normalColor = value;
                this.Invalidate();
            }
        }
      //  [DefaultValue(typeof(Color), "220, 80, 80")]
        public Color HoverColor {
            get
            {
                return this._hoverColor;
            }
            set
            {
                this._hoverColor = value;
                this.Invalidate();
            }
        }
 
      //  [DefaultValue(typeof(Color), "251, 161, 0")]
        public Color PressedColor {
            get
            {
                return this._pressedColor;
            }
            set
            {
                this._pressedColor = value;
                this.Invalidate();
            }
        }
        public ControlState ControlState { get; set; }
        protected override void OnMouseEnter(EventArgs e)//鼠标进入时
        {
            base.OnMouseEnter(e);
            ControlState = ControlState.Hover;//正常
        }
        protected override void OnMouseLeave(EventArgs e)//鼠标离开
        {
            base.OnMouseLeave(e);
            ControlState = ControlState.Normal;//正常
        }
        protected override void OnMouseDown(MouseEventArgs e)//鼠标按下
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left && e.Clicks == 1)//鼠标左键且点击次数为1
            {
                ControlState = ControlState.Pressed;//按下的状态
            }
        }
        protected override void OnMouseUp(MouseEventArgs e)//鼠标弹起
        {
            base.OnMouseUp(e);
            if (e.Button == MouseButtons.Left && e.Clicks == 1)
            {
                if (ClientRectangle.Contains(e.Location))//控件区域包含鼠标的位置
                {
                    ControlState = ControlState.Hover;
                }
                else
                {
                    ControlState = ControlState.Normal;
                }
            }
        }
        public RoundButton()
        {
            Radius = 15;
            this.FlatStyle = FlatStyle.Flat;
            this.FlatAppearance.BorderSize = 0;
            this.ControlState = ControlState.Normal;
            this.SetStyle(
             ControlStyles.UserPaint |  //控件自行绘制,而不使用操作系统的绘制
             ControlStyles.AllPaintingInWmPaint | //忽略擦出的消息,减少闪烁。
             ControlStyles.OptimizedDoubleBuffer |//在缓冲区上绘制,不直接绘制到屏幕上,减少闪烁。
             ControlStyles.ResizeRedraw | //控件大小发生变化时,重绘。                  
             ControlStyles.SupportsTransparentBackColor, true);//支持透明背景颜色
        }
 
        private Color GetColor(Color colorBase, int a, int r, int g, int b)
        {
            int a0 = colorBase.A;
            int r0 = colorBase.R;
            int g0 = colorBase.G;
            int b0 = colorBase.B;
            if (a + a0 > 255) { a = 255; } else { a = Math.Max(a + a0, 0); }
            if (r + r0 > 255) { r = 255; } else { r = Math.Max(r + r0, 0); }
            if (g + g0 > 255) { g = 255; } else { g = Math.Max(g + g0, 0); }
            if (b + b0 > 255) { b = 255; } else { b = Math.Max(b + b0, 0); }
 
            return Color.FromArgb(a, r, g, b);
        }
 
        //重写OnPaint
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);
            base.OnPaintBackground(e);
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
 
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
 
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            var path = GetRoundedRectPath(rect, radius);
 
            this.Region = new Region(path);
 
            Color baseColor;
            //Color borderColor;
            //Color innerBorderColor = this._baseColor;//Color.FromArgb(200, 255, 255, 255); ;
 
            switch (ControlState)
            {
                case ControlState.Hover:
                    baseColor = this.HoverColor;
                    break;
                case ControlState.Pressed:
                    baseColor = this.PressedColor;
                    break;
                case ControlState.Normal:
                    baseColor = this.NormalColor;
                    break;
                default:
                    baseColor = this.NormalColor;
                    break;
            }
 
            using (SolidBrush b = new SolidBrush(baseColor))
            {
                e.Graphics.FillPath(b, path);
                Font fo = new Font("宋体", 10.5F);
                Brush brush = new SolidBrush(this.ForeColor);
                StringFormat gs = new StringFormat();
                gs.Alignment = StringAlignment.Center; //居中
                gs.LineAlignment = StringAlignment.Center;//垂直居中
                e.Graphics.DrawString(this.Text, fo, brush, rect, gs);
                //  e.Graphics.DrawPath(p, path);
            }
        }
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();
            path.AddArc(arcRect, 180, 90);
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();
            return path;
        }
         protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
        }
    }

到此这篇关于C# Winform实现圆角无锯齿按钮的文章就介绍到这了,更多相关C# Winform内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Unity3D创建圆柱体的方法

    Unity3D创建圆柱体的方法

    这篇文章主要为大家详细介绍了Unity3D创建圆柱体的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • C# 三种方式实现Socket数据接收

    C# 三种方式实现Socket数据接收

    这篇文章主要给大家分享三种实现C# 实现Socket数据接收的方式,接下俩小编就来为大家详细介绍吧,需要的朋友可以参考一下
    2021-10-10
  • C# 泛型深入理解介绍

    C# 泛型深入理解介绍

    在上一个专题中介绍了C#2.0 中引入泛型的原因以及有了泛型后所带来的好处,然而上一专题相当于是介绍了泛型的一些基本知识的,对于泛型的性能为什么会比非泛型的性能高却没有给出理由,所以在这个专题就中将会介绍原因和一些关于泛型的其他知识
    2012-11-11
  • Unity Shader实现翻书效果

    Unity Shader实现翻书效果

    这篇文章主要为大家详细介绍了Unity Shader实现翻书效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • C#使用Socket快速判断数据库连接是否正常的方法

    C#使用Socket快速判断数据库连接是否正常的方法

    这篇文章主要介绍了C#使用Socket快速判断数据库连接是否正常的方法,涉及C#中socket操作的相关技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C#交换两个变量值的几种方法总结

    C#交换两个变量值的几种方法总结

    这篇文章介绍了C#交换两个变量值的几种方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-10-10
  • C#使用自带的email组件发送邮件的方法

    C#使用自带的email组件发送邮件的方法

    这篇文章主要介绍了C#使用自带的email组件发送邮件的方法,涉及C#发送邮件的相关技巧,需要的朋友可以参考下
    2015-05-05
  • C#调用SQL语句时乘号的用法

    C#调用SQL语句时乘号的用法

    这篇文章主要介绍了C#调用SQL语句时乘号的用法,可避免因符号引起的程序错误,是C#程序设计人员有必要掌握的,需要的朋友可以参考下
    2014-08-08
  • 基于StreamRead和StreamWriter的使用(实例讲解)

    基于StreamRead和StreamWriter的使用(实例讲解)

    下面小编就为大家分享一篇基于StreamRead和StreamWriter的使用实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-11-11
  • C#使用 Salt + Hash 来为密码加密

    C#使用 Salt + Hash 来为密码加密

    本文主要介绍了几种常见的破解密码的方法,为密码加盐(Salt)以及在.NET中的实现等。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01

最新评论