MoonToggle.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.ComponentModel;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5. namespace Optimizer {
  6. public sealed class MoonToggle : CheckBox {
  7. bool solidStyle = true;
  8. [Browsable(true)]
  9. public override string Text {
  10. get { return base.Text; }
  11. set { }
  12. }
  13. [DefaultValue(true)]
  14. public bool SolidStyle {
  15. get { return solidStyle; }
  16. set {
  17. solidStyle = value;
  18. this.Invalidate();
  19. }
  20. }
  21. public MoonToggle() {
  22. this.DoubleBuffered = true;
  23. this.MinimumSize = new Size(46, 22);
  24. this.ForeColor = Color.White;
  25. }
  26. private GraphicsPath GetFigurePath() {
  27. int arcSize = this.Height - 1;
  28. Rectangle leftArc = new Rectangle(0, 0, arcSize, arcSize);
  29. Rectangle rightArc = new Rectangle(this.Width - arcSize - 2, 0, arcSize, arcSize);
  30. GraphicsPath path = new GraphicsPath();
  31. path.StartFigure();
  32. path.AddArc(leftArc, 90, 180);
  33. path.AddArc(rightArc, 270, 180);
  34. path.CloseFigure();
  35. return path;
  36. }
  37. protected override void OnPaint(PaintEventArgs pevent) {
  38. int toggleSize = this.Height - 5;
  39. pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  40. pevent.Graphics.Clear(this.Parent.BackColor);
  41. if (this.Checked) //ON
  42. {
  43. if (solidStyle)
  44. pevent.Graphics.FillPath(new SolidBrush(OptionsHelper.ForegroundColor), GetFigurePath());
  45. else pevent.Graphics.DrawPath(new Pen(OptionsHelper.ForegroundColor, 2), GetFigurePath());
  46. //Draw the toggle
  47. pevent.Graphics.FillEllipse(new SolidBrush(OptionsHelper.TextColor),
  48. new Rectangle(this.Width - this.Height + 1, 2, toggleSize, toggleSize));
  49. }
  50. else //OFF
  51. {
  52. if (solidStyle)
  53. pevent.Graphics.FillPath(new SolidBrush(OptionsHelper.BackAccentColor), GetFigurePath());
  54. else pevent.Graphics.DrawPath(new Pen(OptionsHelper.BackAccentColor, 2), GetFigurePath());
  55. //Draw the toggle
  56. pevent.Graphics.FillEllipse(new SolidBrush(Color.White),
  57. new Rectangle(2, 2, toggleSize, toggleSize));
  58. }
  59. }
  60. }
  61. }