MoonToggle.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  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. private readonly Timer AnimationTimer = new Timer();
  10. private int CirclePos = 3;
  11. private int CircleColorR = 255;
  12. private int CircleColorG = 255;
  13. private int CircleColorB = 255;
  14. private int Alpha = 0;
  15. public MoonToggle()
  16. {
  17. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
  18. DoubleBuffered = true;
  19. Height = 22; Width = 46;
  20. AnimationTimer.Interval = 1;
  21. AnimationTimer.Tick += new EventHandler(AnimationTick);
  22. }
  23. protected override void OnHandleCreated(EventArgs e)
  24. {
  25. base.OnHandleCreated(e);
  26. AnimationTimer.Start();
  27. }
  28. protected override void OnResize(EventArgs e)
  29. {
  30. Height = 22; Width = 44;
  31. Invalidate();
  32. }
  33. protected override void OnPaint(PaintEventArgs pevent)
  34. {
  35. pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  36. pevent.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  37. pevent.Graphics.InterpolationMode = InterpolationMode.High;
  38. pevent.Graphics.Clear(Parent.BackColor);
  39. GraphicsPath backRect = new GraphicsPath();
  40. backRect.AddArc(new RectangleF(0.5f, 0.5f, Height - 1, Height - 1), 90, 180);
  41. backRect.AddArc(new RectangleF(Width - Height + 0.5f, 0.5f, Height - 1, Height - 1), 270, 180);
  42. backRect.CloseAllFigures();
  43. pevent.Graphics.FillPath(new SolidBrush(Options.BackAccentColor), backRect);
  44. pevent.Graphics.FillPath(new SolidBrush(Color.FromArgb(Alpha, Options.ForegroundColor.R, Options.ForegroundColor.G, Options.ForegroundColor.B)), backRect);
  45. pevent.Graphics.FillEllipse(new SolidBrush(Color.FromArgb(CircleColorR, CircleColorG, CircleColorB)), new RectangleF(CirclePos, 3, 16, 16));
  46. }
  47. private void AnimationTick(object sender, EventArgs e)
  48. {
  49. if (Checked)
  50. {
  51. if (Options.TextColor == Color.Black)
  52. {
  53. if (CircleColorR != 0 && CircleColorG != 0 && CircleColorB != 0)
  54. {
  55. CircleColorR -= 15;
  56. CircleColorG -= 15;
  57. CircleColorB -= 15;
  58. Invalidate();
  59. }
  60. }
  61. else
  62. {
  63. if (CircleColorR != 255 && CircleColorG != 255 && CircleColorB != 255)
  64. {
  65. CircleColorR += 15;
  66. CircleColorG += 15;
  67. CircleColorB += 15;
  68. Invalidate();
  69. }
  70. }
  71. if (CirclePos < 26)
  72. {
  73. CirclePos += 2;
  74. Invalidate();
  75. }
  76. if (Alpha < 255)
  77. {
  78. Alpha += 15;
  79. Invalidate();
  80. }
  81. }
  82. else
  83. {
  84. if (CircleColorR != 255 && CircleColorG != 255 && CircleColorB != 255)
  85. {
  86. CircleColorR += 15;
  87. CircleColorG += 15;
  88. CircleColorB += 15;
  89. Invalidate();
  90. }
  91. if (CirclePos > 3)
  92. {
  93. CirclePos -= 2;
  94. Invalidate();
  95. }
  96. if (Alpha > 0)
  97. {
  98. Alpha -= 15;
  99. Invalidate();
  100. }
  101. }
  102. }
  103. }
  104. }