Ellipse.cs

Ellipse Control

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
public class Ellipse : Control
{
    private readonly Color[] _surroundcolors        = {Color.FromArgb(0, 135, 206, 250)};
    private          Color   _centerreflectioncolor = Color.FromArgb(180, 135, 206, 250);
    private          Color   _color                 = Color.DodgerBlue;
    private          Color   _lightColor, _darkColor, _darkDarkColor;
    private          bool    _on           = true;
    private          bool    _reflectionon = true;
    public Ellipse()
    {
        SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
        Name           = "Ellipse";
        Size           = new Size(10, 10);
        _lightColor    = ControlPaint.Light(Color.DodgerBlue, .4f);
        _darkColor     = ControlPaint.Dark(_lightColor, .2f);
        _darkDarkColor = ControlPaint.DarkDark(_lightColor);
    }
    [DefaultValue(typeof(Color), "30, 144, 255")]
    public Color Color
    {
        get => _color;
        set
        {
            _color         = value;
            _lightColor    = ControlPaint.Light(value, .4f);
            _darkColor     = ControlPaint.Dark(_lightColor, .2f);
            _darkDarkColor = ControlPaint.DarkDark(_lightColor);
            Invalidate();
        }
    }
    [DefaultValue(typeof(Color), "180, 135, 206, 250")]
    public Color ReflectionColor
    {
        get => _centerreflectioncolor;
        set
        {
            _centerreflectioncolor = value;
            Invalidate();
        }
    }
    [DefaultValue(typeof(bool), "true")]
    public bool On
    {
        get => _on;
        set
        {
            _on = value;
            Invalidate();
        }
    }
    [DefaultValue(typeof(bool), "true")]
    public bool ReflectionOn
    {
        get => _reflectionon;
        set
        {
            _reflectionon = value;
            Invalidate();
        }
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        var offScreenBmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
        using (var g = Graphics.FromImage(offScreenBmp))
        {
            g.SmoothingMode = SmoothingMode.HighQuality;
            DrawControl(g, On);
            e.Graphics.DrawImageUnscaled(offScreenBmp, 0, 0);
        }
    }
    private void DrawControl(Graphics g, bool on)
    {
        var lightColor = on ? _color : Color.FromArgb(180, _darkColor);
        var darkColor  = on ? _darkColor : _darkDarkColor;
        var width      = Width  - (Padding.Left + Padding.Right);
        var height     = Height - (Padding.Top  + Padding.Bottom);
        var diameter   = Math.Min(width, height);
        diameter = Math.Max(diameter - 1, 1);
        var rectangle = new Rectangle(Padding.Left, Padding.Top, diameter, diameter);
        g.FillEllipse(new SolidBrush(darkColor), rectangle);
        var path = new GraphicsPath();
        path.AddEllipse(rectangle);
        var pathBrush = new PathGradientBrush(path)
        {
            CenterColor    = lightColor,
            SurroundColors = new[] {Color.FromArgb(0, lightColor)}
        };
        g.FillEllipse(pathBrush, rectangle);
        if (_reflectionon)
        {
            var offset    = Convert.ToInt32(diameter        * .15F);
            var diameter1 = Convert.ToInt32(rectangle.Width * .8F);
            var whiteRect = new Rectangle(rectangle.X - offset, rectangle.Y - offset, diameter1, diameter1);
            var path1     = new GraphicsPath();
            path1.AddEllipse(whiteRect);
            var pathBrush1 = new PathGradientBrush(path)
            {
                CenterColor    = _centerreflectioncolor,
                SurroundColors = _surroundcolors
            };
            g.FillEllipse(pathBrush1, whiteRect);
        }
        g.SetClip(ClientRectangle);
        if (On) g.DrawEllipse(new Pen(Color.FromArgb(85, Color.Black), 1F), rectangle);
    }
}