MoonToggle.cs 2.5 KB

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