自定义颜色显示的CheckBox_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 自定义颜色显示的CheckBox

自定义颜色显示的CheckBox

 2013/7/23 2:40:22  水果饮料  博客园  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Drawing;usingSystem.ComponentModel;namespaceColorCheckControls{publicclassCustomCheckBox: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 = (this.Width / 2) - 4;
                    pt.Y = this.Height - 11;
                }

                if (this.CheckAlign == ContentAlignment.BottomLeft)
                {
                    pt.X = 3;
                    pt.Y = this.Height - 11;
                }

                if (this.CheckAlign == ContentAlignment.BottomRight)
                {
                    pt.X = this.Width - 11;
                    pt.Y = this.Height - 11;
                }

                if (this.CheckAlign == ContentAlignment.MiddleCenter)
                {
                    pt.X = (this.Width / 2) - 4;
                    pt.Y = (this.Height / 2) - 4;
                }

                if (this.CheckAlign == ContentAlignment.MiddleLeft)
                {
                    pt.X = 3;
                    pt.Y = (this.Height / 2) - 4;
                }

                if (this.CheckAlign == ContentAlignment.MiddleRight)
                {
                    pt.X = this.Width - 11;
                    pt.Y = (this.Height / 2) - 4;
                }

                if (this.CheckAlign == ContentAlignment.TopCenter)
                {
                    pt.X = (this.Width / 2) - 4;
                    pt.Y = 3;
                }

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

                if (this.CheckAlign == ContentAlignment.TopRight)
                {
                    pt.X = this.Width - 11;
                    pt.Y = 3;
                }

                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 + 2, pt.X + 2, pt.Y + 5);
            g.DrawLine(pen, pt.X, pt.Y + 3, pt.X + 2, pt.Y + 6);
            g.DrawLine(pen, pt.X, pt.Y + 4, pt.X + 2, pt.Y + 7);
            g.DrawLine(pen, pt.X + 3, pt.Y + 4, pt.X + 6, pt.Y - 1);
            g.DrawLine(pen, pt.X + 3, pt.Y + 5, pt.X + 6, pt.Y);
            g.DrawLine(pen, pt.X + 3, pt.Y + 6, pt.X + 6, pt.Y + 2);
        }

        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 = (this.Width / 2) - 3;
                    pt.Y = this.Height - 9;
                }

                if (CheckAlign == ContentAlignment.BottomLeft)
                {
                    pt.X = 4;
                    pt.Y = this.Height - 9;
                }

                if (CheckAlign == ContentAlignment.BottomRight)
                {
                    pt.X = this.Width - 9;
                    pt.Y = this.Height - 9;
                }

                if (CheckAlign == ContentAlignment.MiddleCenter)
                {
                    pt.X = (this.Width / 2) - 3;
                    pt.Y = (this.Height / 2) - 3;
                }

                if (CheckAlign == ContentAlignment.MiddleLeft)
                {
                    pt.X = 4;
                    pt.Y = (this.Height / 2) - 3;
                }

                if (CheckAlign == ContentAlignment.MiddleRight)
                {
                    pt.X = this.Width - 9;
                    pt.Y = (this.Height / 2) - 3;
                }

                if (CheckAlign == ContentAlignment.TopCenter)
                {
                    pt.X = (this.Width / 2) - 3;
                    pt.Y = 4;
                }
                if (CheckAlign == ContentAlignment.TopLeft)
                {
                    pt.X = 4;
                    pt.Y = 4;
                }
                if (CheckAlign == ContentAlignment.TopRight)
                {
                    pt.X = this.Width - 9;
                    pt.Y = 4;
                }

                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-2, pt.Y-1, 7, 7);
        }

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

点我下载

上一篇: 三星10月将举行史上最大北美开发者大会 下一篇: 没有下一篇了!
发表评论
用户名: 匿名