WinForm控件重写之RadioButton

1.效果预览

未选状态

选中状态

不可选状态

主要代码---具体将在gitub和gitee发布

private Image DrawCircle() {
            Graphics g = Graphics.FromImage(_dotImg);
            g.Clear(Color.White);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            Brush bush = new SolidBrush(_radioButtonColor);//填充的颜色
            g.FillEllipse(bush, 0, 0, 6, 6);
            Pen p = new Pen(_radioButtonColor, 2);
            g.DrawEllipse(p, 0, 0, 6, 6);
            return _dotImg;
        }

        private void DrawNormalCircle(Graphics g, Rectangle circleRect)
        {
            g.FillEllipse(Brushes.White, circleRect);
            using (Pen borderPen = new Pen(_radioButtonColor))
            {
                g.DrawEllipse(borderPen, circleRect);
            }
            if (Checked)
            {
                circleRect.Inflate(-2, -2);
                g.DrawImage(
                    DrawCircle(),
                    new Rectangle(circleRect.X + 1, circleRect.Y + 1, circleRect.Width - 1, circleRect.Height - 1),
                    0,
                    0,
                    _dotImg.Width,
                    _dotImg.Height,
                    GraphicsUnit.Pixel);
            }
        }

        private void DrawHighLightCircle(Graphics g, Rectangle circleRect)
        {
            DrawNormalCircle(g, circleRect);
            using (Pen p = new Pen(_radioButtonColor))
            {
                g.DrawEllipse(p, circleRect);

                circleRect.Inflate(1, 1);
                p.Color = _radioButtonColor;

                g.DrawEllipse(p, circleRect);
            }


        }

        private void DrawDisabledCircle(Graphics g, Rectangle circleRect)
        {
            Pen p = new Pen(_radioButtonColor);
            g.DrawEllipse(p, circleRect);
            if (Checked)
            {
                circleRect.Inflate(-2, -2);
                g.DrawImage(
                    DrawCircle(),
                    new Rectangle(circleRect.X + 1, circleRect.Y + 1, circleRect.Width - 1, circleRect.Height - 1),
                    0,
                    0,
                    _dotImg.Width,
                    _dotImg.Height,
                    GraphicsUnit.Pixel);
            }
        }
posted @ 2021-10-17 15:34  ZuoTian  阅读(414)  评论(0)    收藏  举报