自定义颜色显示的CheckBox

自定义颜色显示的CheckBox

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace ColorCheckControls
{
    public class CustomCheckBox: CheckBox
    {
        private Color _CheckColor;

        private void PaintHandler(object sender, PaintEventArgs args)
        {
            if (this.Checked)
            {
                Point pt = new Point();

                if (this.CheckAlign == ContentAlignment.BottomCenter)
                {
                    pt.X = () - ;
                    pt.Y = ;
                }

                if (this.CheckAlign == ContentAlignment.BottomLeft)
                {
                    pt.X = ;
                    pt.Y = ;
                }

                if (this.CheckAlign == ContentAlignment.BottomRight)
                {
                    pt.X = ;
                    pt.Y = ;
                }

                if (this.CheckAlign == ContentAlignment.MiddleCenter)
                {
                    pt.X = () - ;
                    pt.Y = () - ;
                }

                if (this.CheckAlign == ContentAlignment.MiddleLeft)
                {
                    pt.X = ;
                    pt.Y = () - ;
                }

                if (this.CheckAlign == ContentAlignment.MiddleRight)
                {
                    pt.X = ;
                    pt.Y = () - ;
                }

                if (this.CheckAlign == ContentAlignment.TopCenter)
                {
                    pt.X = () - ;
                    pt.Y = ;
                }

                if (this.CheckAlign == ContentAlignment.TopLeft)
                {
                    pt.X = ;
                    pt.Y = ;
                }

                if (this.CheckAlign == ContentAlignment.TopRight)
                {
                    pt.X = ;
                    pt.Y = ;
                }

                DrawCheck(args.Graphics, this.CheckColor, pt);
            }
        }

        private void DrawCheck(Graphics g, Color c, Point pt)
        {
            Pen pen = new Pen(c);
            g.DrawLine(pen, pt.X, pt.Y + , pt.X + , pt.Y + );
            g.DrawLine(pen, pt.X, pt.Y + , pt.X + , pt.Y + );
            g.DrawLine(pen, pt.X, pt.Y + , pt.X + , pt.Y + );
            g.DrawLine(pen, pt.X + , pt.Y + , pt.X + , pt.Y - );
            g.DrawLine(pen, pt.X + , pt.Y + , pt.X + , pt.Y);
            g.DrawLine(pen, pt.X + , pt.Y + , pt.X + , pt.Y + );
        }

        public CustomCheckBox()
        {
            this._CheckColor = ForeColor;
            this.Paint += new PaintEventHandler(this.PaintHandler);
        }

        [Description("CheckBox复选框颜色")]
        public Color CheckColor
        {
            get
            {
                return _CheckColor;
            }
            set
            {
                _CheckColor = value;
                Invalidate();
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace ColorCheckControls
{
    public class CustomColorRadioButton: RadioButton
    {
        private Color _CheckColor;

        private void PaintHandler(object sender, PaintEventArgs args)
        {
            if (this.Checked)
            {
                Point pt = new Point();
                if (CheckAlign == ContentAlignment.BottomCenter)
                {
                    pt.X = () - ;
                    pt.Y = ;
                }

                if (CheckAlign == ContentAlignment.BottomLeft)
                {
                    pt.X = ;
                    pt.Y = ;
                }

                if (CheckAlign == ContentAlignment.BottomRight)
                {
                    pt.X = ;
                    pt.Y = ;
                }

                if (CheckAlign == ContentAlignment.MiddleCenter)
                {
                    pt.X = () - ;
                    pt.Y = () - ;
                }

                if (CheckAlign == ContentAlignment.MiddleLeft)
                {
                    pt.X = ;
                    pt.Y = () - ;
                }

                if (CheckAlign == ContentAlignment.MiddleRight)
                {
                    pt.X = ;
                    pt.Y = () - ;
                }

                if (CheckAlign == ContentAlignment.TopCenter)
                {
                    pt.X = () - ;
                    pt.Y = ;
                }
                if (CheckAlign == ContentAlignment.TopLeft)
                {
                    pt.X = ;
                    pt.Y = ;
                }
                if (CheckAlign == ContentAlignment.TopRight)
                {
                    pt.X = ;
                    pt.Y = ;
                }

                DrawCheck(args.Graphics, this.CheckColor, pt);
            }
        }

        private void DrawCheck(Graphics g, Color c, Point pt)
        {
            /*
            Pen pen = new Pen(c);
            g.DrawLine(pen, pt.X, pt.Y + 1, pt.X + 4, pt.Y + 1);
            g.DrawLine(pen, pt.X-1, pt.Y + 2, pt.X + 5, pt.Y + 2);
            g.DrawLine(pen, pt.X, pt.Y + 3, pt.X + 4, pt.Y + 3);
            g.DrawLine(pen, pt.X + 1, pt.Y, pt.X + 1, pt.Y + 4);
            g.DrawLine(pen, pt.X + 2, pt.Y-1, pt.X + 2, pt.Y + 5);
            g.DrawLine(pen, pt.X + 3, pt.Y, pt.X + 3, pt.Y + 4);
             * */
            Brush brush = new SolidBrush(c);
            g.FillEllipse(brush, pt.X-, pt.Y-, , );
        }

        public CustomColorRadioButton()
        {
            this._CheckColor = this.ForeColor;
            this.Paint += new PaintEventHandler(this.PaintHandler);
        }
        [Description("按钮颜色")]
        public Color CheckColor
        {
            get
            {
                return _CheckColor;
            }
            set
            {
                _CheckColor = value;
                Invalidate();
            }
        }
    }
}

点我下载

上一篇:JS获取节点的兄弟,父级,子级元素的方法(js获取子级获取到换行与空格元素-FF)


下一篇:Android开源代码解读-基于SackOfViewAdapter类实现类似状态通知栏的布局