MoonToggle.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Text;
  5. using System.Windows.Forms;
  6. namespace Optimizer
  7. {
  8. public sealed class MoonToggle : CheckBox
  9. {
  10. private readonly Timer AnimationTimer = new Timer();
  11. private int CirclePos = 3;
  12. private int Alpha = 0;
  13. public MoonToggle()
  14. {
  15. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
  16. DoubleBuffered = true;
  17. Height = 22; Width = 46;
  18. AnimationTimer.Interval = 1;
  19. AnimationTimer.Tick += new EventHandler(AnimationTick);
  20. }
  21. protected override void OnHandleCreated(EventArgs e)
  22. {
  23. base.OnHandleCreated(e);
  24. AnimationTimer.Start();
  25. }
  26. protected override void OnResize(EventArgs e)
  27. {
  28. Height = 22; Width = 44;
  29. Invalidate();
  30. }
  31. protected override void OnPaint(PaintEventArgs pevent)
  32. {
  33. pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  34. pevent.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  35. pevent.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  36. pevent.Graphics.InterpolationMode = InterpolationMode.High;
  37. pevent.Graphics.Clear(Parent.BackColor);
  38. GraphicsPath backRect = new GraphicsPath();
  39. backRect.AddArc(new RectangleF(0.5f, 0.5f, Height - 1, Height - 1), 90, 180);
  40. backRect.AddArc(new RectangleF(Width - Height + 0.5f, 0.5f, Height - 1, Height - 1), 270, 180);
  41. backRect.CloseAllFigures();
  42. pevent.Graphics.FillPath(new SolidBrush(Options.BackAccentColor), backRect);
  43. pevent.Graphics.FillPath(new SolidBrush(Color.FromArgb(Alpha, Options.ForegroundColor.R, Options.ForegroundColor.G, Options.ForegroundColor.B)), backRect);
  44. pevent.Graphics.FillEllipse(new SolidBrush(Color.White), new RectangleF(CirclePos, 3, 16, 16));
  45. }
  46. private void AnimationTick(object sender, EventArgs e)
  47. {
  48. if (Checked)
  49. {
  50. if (CirclePos < 26)
  51. {
  52. CirclePos += 2;
  53. Invalidate();
  54. }
  55. if (Alpha < 255)
  56. {
  57. Alpha += 15;
  58. if (CirclePos > 26)
  59. Invalidate();
  60. }
  61. }
  62. else
  63. {
  64. if (CirclePos > 3)
  65. {
  66. CirclePos -= 2;
  67. Invalidate();
  68. }
  69. if (Alpha > 0)
  70. {
  71. Alpha -= 15;
  72. Invalidate();
  73. }
  74. }
  75. }
  76. }
  77. }