C#实现自定义单选和复选按钮样式

 更新时间:2022年12月23日 09:40:47   作者:芝麻粒儿  
这篇文章主要为大家详细介绍了如何利用C#实现定义单选和复选按钮样式,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下

实践过程

效果

代码

public partial class GlorifyCheckBox : CheckBox
    {
        public GlorifyCheckBox()
        {
            InitializeComponent();
            FontAspect = getAspect(); //获取当前控件文本的读取方向
        }

        #region 变量

        private bool FontAspect = false; //判断字体的方向
        private int Measurement = 255; //设置渐变的初始值
        LinearGradientBrush Periphery_br; //外圆的颜色
        LinearGradientBrush Central_br; //移入控件时中圆的颜色
        LinearGradientBrush NoCentral_br; //无操作时中圆的颜色

        #endregion

        #region 添加属性

        public enum StyleSort
        {
            Null = 0, //无
            Integer = 1, //整数
            Decimal = 2, //小数
        }

        private Color TPeripheryColor = Color.DarkBlue;

        [Browsable(true), Category("设置填充颜色"), Description("外圆的颜色")] //在“属性”窗口中显示DataStyle属性
        public Color PeripheryColor
        {
            get { return TPeripheryColor; }
            set
            {
                TPeripheryColor = value;
                this.Invalidate();
            }
        }

        private Color TCentralColor = Color.CornflowerBlue;

        [Browsable(true), Category("设置填充颜色"), Description("移入控件时中圆的颜色")] //在“属性”窗口中显示DataStyle属性
        public Color CentralColor
        {
            get { return TCentralColor; }
            set
            {
                TCentralColor = value;
                this.Invalidate();
            }
        }

        private Color TNoCentralColor = Color.PowderBlue;

        [Browsable(true), Category("设置填充颜色"), Description("无操作时中圆的颜色")] //在“属性”窗口中显示DataStyle属性
        public Color NoCentralColor
        {
            get { return TNoCentralColor; }
            set
            {
                TNoCentralColor = value;
                this.Invalidate();
            }
        }

        private Color TStippleColor = Color.DarkBlue;

        [Browsable(true), Category("设置填充颜色"), Description("选中后内圆的颜色")] //在“属性”窗口中显示DataStyle属性
        public Color StippleColor
        {
            get { return TStippleColor; }
            set
            {
                TStippleColor = value;
                this.Invalidate();
            }
        }

        #endregion

        #region 事件

        /// <summary>
        /// 控件在需要重绘时触发
        /// </summary>
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.FillRectangle(SystemBrushes.Control, e.ClipRectangle); //填充矩形
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //清除锯齿
            //获取左面图标的区域
            Rectangle boxrect = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y,
                SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);
            //获取绘制的文本的区域
            Rectangle strrect = new Rectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width,
                e.ClipRectangle.Y, e.ClipRectangle.Width + 5 - SystemInformation.SmallIconSize.Width,
                e.ClipRectangle.Height);
            if (FontAspect) //判断字体的读取方式
            {
                boxrect.X = e.ClipRectangle.X + e.ClipRectangle.Width - SystemInformation.SmallIconSize.Width; //设置椭圆的位置
                strrect.X = e.ClipRectangle.X; //设置字体位置
            }

            Point MousePos = this.PointToClient(Control.MousePosition); //获取鼠标的位置
            bool Above = e.ClipRectangle.Contains(MousePos); //获取鼠标是否在当前控件上

            DrawBox(e.Graphics, boxrect, Above); //绘制单选图案
            DrawText(e.Graphics, strrect); //绘制文字
            if (!Enabled)
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(127, SystemColors.Control)), e.ClipRectangle);
        }

        /// <summary>
        /// 鼠标移入控件的可见区域时触发
        /// </summary>
        protected override void OnMouseEnter(System.EventArgs e)
        {
            base.OnMouseEnter(e);
            this.Invalidate();
        }

        /// <summary>
        /// 鼠标移出控件的可见区域时触发
        /// </summary>
        protected override void OnMouseLeave(System.EventArgs e)
        {
            base.OnMouseLeave(e);
            this.Invalidate();
        }

        /// <summary>
        /// RightToLeft属性值更改时发生
        /// </summary>
        protected override void OnRightToLeftChanged(System.EventArgs e)
        {
            base.OnRightToLeftChanged(e);
            FontAspect = getAspect();
            this.Invalidate();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 绘制单选控件的图案
        /// </summary>
        /// <param g="Graphics">封装一个绘图的类对象</param>
        /// <param rect="Rectangle">单选图案的绘制区域</param> 
        /// <param Above="bool">断判鼠标是否在控件上方</param> 
        private void DrawBox(Graphics g, Rectangle rect, bool Above)
        {
            //设置外椭圆的渐变色
            int opacity = Measurement;
            Periphery_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 2, PeripheryColor),
                Color.FromArgb(opacity, PeripheryColor), LinearGradientMode.ForwardDiagonal);
            //设置中间椭圆形选中时的渐变色
            opacity = (int) (.4f * opacity + .5f);
            Central_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 10, CentralColor),
                Color.FromArgb(opacity, CentralColor), LinearGradientMode.ForwardDiagonal);
            //设置中间椭圆形无操作时的渐变色
            opacity = (int) (.4f * opacity + .5f);
            NoCentral_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 10, NoCentralColor),
                Color.FromArgb(opacity, NoCentralColor), LinearGradientMode.ForwardDiagonal);
            int size = this.Font.Height; //获取字体的高度
            //获取外椭圆的区域
            Rectangle box = new Rectangle(rect.X + ((rect.Width - size) / 2), rect.Y + ((rect.Height - size) / 2),
                size - 2, size - 2);
            Rectangle glyph = new Rectangle(box.X + 3, box.Y + 3, box.Width - 6, box.Height - 6); //设置内圆的绘制区域
            Rectangle right = new Rectangle(box.X, box.Y - 1, box.Width + 2, box.Height + 2);
            g.FillEllipse(new SolidBrush(SystemColors.Window), box); //以白色填充单选图案 

            if (this.CheckState != CheckState.Unchecked) //如果是选中状态
            {
                base.ForeColor = Color.DarkBlue;
                ControlPaint.DrawMenuGlyph(g, right, MenuGlyph.Checkmark, this.StippleColor, Color.White); //绘制对号
                g.DrawRectangle(new Pen(new SolidBrush(SystemColors.Control), (float) (3)), box); //绘制外椭圆
            }

            if (this.CheckState == CheckState.Indeterminate)
                g.FillRectangle(new SolidBrush(Color.FromArgb(127, SystemColors.Control)), right);

            if (Above && this.Enabled) //如果鼠标移入该控件
            {
                g.DrawRectangle(new Pen(Central_br, 2),
                    new Rectangle(box.X + 2, box.Y + 2, box.Width - 4, box.Height - 4)); //绘制中心椭圆
            }
            else
            {
                g.DrawRectangle(new Pen(NoCentral_br, 2),
                    new Rectangle(box.X + 2, box.Y + 2, box.Width - 4, box.Height - 4)); //绘制中心椭圆
            }

            g.DrawRectangle(new Pen(Periphery_br, (float) (1.5)), box); //绘制外椭圆
        }

        /// <summary>
        /// 绘制文本
        /// </summary>
        /// <param g="Graphics">封装一个绘图的类对象</param>
        /// <param rect="Rectangle">绘制文本的区域</param>
        private void DrawText(Graphics g, Rectangle rect)
        {
            StringFormat tem_StringF = new StringFormat();
            tem_StringF.Alignment = StringAlignment.Near;
            tem_StringF.LineAlignment = StringAlignment.Center; //文本居中对齐
            if (FontAspect)
                tem_StringF.FormatFlags = StringFormatFlags.DirectionRightToLeft; //按从左到右的顺序显示文本
            //g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, rect, tem_StringF);//绘制文本
            if (!FontAspect)
                g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, rect, tem_StringF); //绘制文本
            else
            {
                rect.X = rect.X - SystemInformation.SmallIconSize.Width / 2 + 2;
                g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, rect, tem_StringF);
            }
        }

        /// <summary>
        /// 获取文本的读取方向
        /// </summary>
        /// <return>布尔型</return>
        private bool getAspect()
        {
            bool tem_Aspect = SystemInformation.RightAlignedMenus;
            if (this.RightToLeft == RightToLeft.Yes) //从右到左进行读取
                tem_Aspect = true;
            if (this.RightToLeft == RightToLeft.No) //从左到右进行读取
                tem_Aspect = false;
            return tem_Aspect;
        }

        #endregion
    }

到此这篇关于C#实现自定义单选和复选按钮样式的文章就介绍到这了,更多相关C#自定义按钮样式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • UnityShader3实现转圈与冷却效果

    UnityShader3实现转圈与冷却效果

    这篇文章主要为大家详细介绍了UnityShader3实现转圈与冷却效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • C#使用Dynamic实现简化反射

    C#使用Dynamic实现简化反射

    这篇文章主要为大家详细介绍了C#如何使用Dynamic来实现简化反射,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解一下
    2023-07-07
  • c# 使用OpenCV识别硬币

    c# 使用OpenCV识别硬币

    这篇文章主要介绍了c# 使用OpenCV识别硬币的方法,帮助大家更好的利用c#进行深度学习,感兴趣的朋友可以了解下
    2020-12-12
  • WPF+ASP.NET SignalR实现简易在线聊天功能的示例代码

    WPF+ASP.NET SignalR实现简易在线聊天功能的示例代码

    这篇文章将以一个简单的聊天示例,简述如何通过WPF+ASP.NET SignalR实现消息后台通知,仅供学习分享使用,如有不足之处,还请指正
    2022-09-09
  • C#使用HtmlAgilityPack抓取糗事百科内容实例

    C#使用HtmlAgilityPack抓取糗事百科内容实例

    这篇文章主要介绍了C#使用HtmlAgilityPack抓取糗事百科内容的方法,实例分析了C#中HtmlAgilityPack的相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • 使用C#9中records作为强类型ID的实例教程

    使用C#9中records作为强类型ID的实例教程

    这篇文章主要给大家介绍了关于使用C#9中records作为强类型ID的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • C# 如何生成 DataMatrix 格式的二维码

    C# 如何生成 DataMatrix 格式的二维码

    该文主要是利用OnBarcode.dll 生成DataMatrix 格式的二维码的一些简单方法和操作技巧,对C# 如何生成 DataMatrix 格式的二维码相关知识感兴趣的朋友一起看看吧
    2021-11-11
  • c# 接口interface基础入门小例子

    c# 接口interface基础入门小例子

    用于描述类的功能,类似于契约,指示了类将:执行的工作,形参类型,返回结果类型,但本身没有执行的代码
    2013-04-04
  • C#生成防伪码的思路及源码分享

    C#生成防伪码的思路及源码分享

    生成防伪码其实挺简单,但是如果要考虑效率和不重复的话,就需要稍微动动脑子了,下面我来说说我的思路及源码
    2014-06-06
  • C#中跨线程访问控件的实现方法

    C#中跨线程访问控件的实现方法

    C#中不允许跨线程直接访问界面控件,即一个线程中如主线程创建的控件不允许被其他线程例如子线程直接访问,在一个线程中设置其他线程所有的控件属性通常有两种方法,本文将详细的给大家介绍一下,需要的朋友可以参考下
    2024-12-12

最新评论