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#自定义按钮样式内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C#中Winform 实现Ajax效果自定义按钮

    技术看点 WinForm自定义控件的使用 自定义控件gif动画的播放 需求及效果 又来一波 C# GDI自定义控件show .这个控件已经使用几年了,最近找出来重构一下.原来是没有边框的,那么导致导航的功能不是很突出.本来想加个效果:在执行单击时显示Loading动画,在执行完单击事件后恢复原样.这就是网页里见到的局部刷新,Ajax常用的场景.需求来自几年前一个智能储物柜项目,人机界面有个美工设计好的效果图,为了省事和通用,需要一个透明的按钮来实现导航的任务.就是控件只是设计时可见,运行时不可见

  • C# WPF 自定义按钮的方法

    本文介绍WPF一种自定义按钮的方法. 实现效果 使用图片做按钮背景: 自定义鼠标进入时效果: 自定义按压效果: 自定义禁用效果 实现效果如下图所示: 实现步骤 创建CustomButton.cs,继承自Button: 创建一个资源文件ButtonStyles.xaml: 在资源文件中设计按钮的Style: 在CustomButton.cs中添加Style中需要的依赖属性: 在程序中添加资源并引用(为了方便在不同的程序中引用自定义按钮,自定义按钮放在独立的类库中,应用程序中进行资源合并即可). 示

  • C#实现自定义圆角按钮的方法

    Winform中自带的button没有圆角属性,所以我们继承Button类,重写OnPaint事件来绘制圆角按钮. 1.绘制圆角按钮框需要用到系统自带的绘制方法:首先引入Gdi32.dll中的CreateRoundRectRgn方法.经过验证此方法在大量控件情况下使用,会导致GDI+绘图问题!现在改用自己绘制的圆角GraphicsPath进行填充显示,效果更好,圆角更圆润了! 重写OnPaint方法: protected override void OnPaint(PaintEventArgs

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

    目录 实践过程 效果 代码 实践过程 效果 代码 public partial class GlorifyCheckBox : CheckBox { public GlorifyCheckBox() { InitializeComponent(); FontAspect = getAspect(); //获取当前控件文本的读取方向 } #region 变量 private bool FontAspect = false; //判断字体的方向 private int Measurement = 2

  • jQuery实现按钮的点击 全选/反选 单选框/复选框 文本框 表单验证

    jQuery实现按钮的点击 全选/反选 单选框/复选框 文本框 表单验证 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> &l

  • Android编程实现带有单选按钮和复选按钮的dialog功能示例

    本文实例讲述了Android编程实现带有单选按钮和复选按钮的dialog.分享给大家供大家参考,具体如下: 带有单选按钮的dialog: package example.com.myapplication; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.

  • Android开发之获取单选与复选框的值操作示例

    本文实例讲述了Android开发之获取单选与复选框的值操作.分享给大家供大家参考,具体如下: 效果图: 布局文件: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout

  • Labelauty–jQuery单选框/复选框美化插件分享

    本文实例讲述了Labelauty–jQuery单选框/复选框美化插件,分享给大家供大家参考.具体如下: Labelauty–jQuery单选框/复选框美化插件,基于jQuery的一个非常小巧的插件,它除了能够实现单选框/复选框原本的选中.未选中.禁用等功能外,还能够设置选中和未选中的文本信息.标签的最小宽度等. 运行效果图:-------------------------------查看效果------------------------------------ 具体代码如下 <head>

  • JQuery操作单选按钮以及复选按钮示例

    单选按钮以及复选按钮在开发过程中会经常用到,下面我就来通过JQuery操作单选按钮和复选按钮: 单选按钮: 通过JQuery获取单选按钮对象我们总共有三种途径: ①ID:$("#radioId") ②NAME:$(":input[name='radioName']") ③TYPE:$("input[type=radio]"),可能在有的资料上面写的是:$(""input[@type=radio]""),这个

  • Vue.js表单标签中的单选按钮、复选按钮和下拉列表的取值问题

    Vue.js可以很方便的实现数据双向绑定,所以在处理表单,人机交互方面具有很大的优势.下面给大家介绍Vue.js表单标签中的单选按钮.复选按钮和下拉列表的取值问题. 摘要: 表单标签取值问题中,单选按钮.复选按钮和下拉列表都比较特殊.这里总结一下vue.js中关于单选按钮.复选按钮和下拉列表不同情况的取值特殊性问题. 表单标签取值问题中,单选按钮.复选按钮和下拉列表都比较特殊.这里总结一下vue.js中关于单选按钮.复选按钮和下拉列表不同情况的取值特殊性问题. 一.单选按钮 单选按钮:单选按钮用

  • 解决LayUI加上form.render()下拉框和单选以及复选框不出来的问题

    首先: 引入需要的css和js <link rel="stylesheet" href="${ctx}/adminthemes/version3/plugins/layui/css/layui.css" rel="external nofollow" /> <script type="text/javascript" src="${ctx}/adminthemes/version3/plugins/

  • Android单选多选按钮的使用方法

    本文实例为大家分享了Android单选多选按钮使用的具体代码,供大家参考,具体内容如下 一.单选按钮 单选按钮类:RadioButton android:checked="true"设置默认选中 单选按钮控件通常与RadioGroup搭配使用. RadioGroup是LinearLayout的子类,用于将多个单选按钮组合为一组. 同一按钮组内的单选按钮只能有一个被选中. 二.多选按钮 用法基本与Button相同 CheckBox对象.isChecked()方法可以用来判断复选按钮是否选

  • iOS tableView实现单选和多选的实例代码

    今天在项目中遇到了tableView的单选需求,现在总结一下,用一个简单的demo实现了简单的单选和多选两个功能.先看下效果图: 1:首先实现下单选 1:使用一个变量记录选中的行 @property (assign, nonatomic) NSIndexPath *selIndex; //单选选中的行 2:设置tableView数据,共2组,每组10行, - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { ret

随机推荐