Created
March 16, 2018 01:21
-
-
Save mjs3339/0559b473755a37b2629845a702f67273 to your computer and use it in GitHub Desktop.
C# Shaded Ellipse Control Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Ellipse : Control | |
{ | |
private readonly IContainer components = null; | |
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; | |
private readonly Color[] _surroundcolors = {Color.FromArgb(0, 135, 206, 250)}; | |
public Ellipse() | |
{ | |
InitializeComponent(); | |
SetStyle(ControlStyles.DoubleBuffer | |
| ControlStyles.AllPaintingInWmPaint | |
| ControlStyles.ResizeRedraw | |
| ControlStyles.UserPaint | |
| ControlStyles.SupportsTransparentBackColor, true); | |
} | |
[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); | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing) | |
components?.Dispose(); | |
base.Dispose(disposing); | |
} | |
private void InitializeComponent() | |
{ | |
SuspendLayout(); | |
Name = "Ellipse"; | |
Size = new Size(10, 10); | |
_lightColor = ControlPaint.Light(Color.DodgerBlue, .4f); | |
_darkColor = ControlPaint.Dark(_lightColor, .2f); | |
_darkDarkColor = ControlPaint.DarkDark(_lightColor); | |
ResumeLayout(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment