MoonToggle.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 _circlePosX = 3, _circlePosY = 3;
  11. private int _circleColorR = 255, _circleColorG = 255, _circleColorB = 255;
  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 OnPaint(PaintEventArgs pevent)
  27. {
  28. pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  29. pevent.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  30. pevent.Graphics.InterpolationMode = InterpolationMode.High;
  31. pevent.Graphics.Clear(Parent.BackColor);
  32. GraphicsPath backRect = new GraphicsPath();
  33. backRect.AddArc(new RectangleF(0.5f, 0.5f, Height - 1, Height - 1), 90, 180);
  34. backRect.AddArc(new RectangleF(Width - Height + 0.5f, 0.5f, Height - 1, Height - 1), 270, 180);
  35. backRect.CloseAllFigures();
  36. pevent.Graphics.FillPath(new SolidBrush(Options.BackAccentColor), backRect);
  37. pevent.Graphics.FillPath(new SolidBrush(Color.FromArgb(_alpha, Options.ForegroundColor)), backRect);
  38. pevent.Graphics.FillEllipse(new SolidBrush(Color.FromArgb(_circleColorR, _circleColorG, _circleColorB)), new RectangleF(_circlePosX, _circlePosY, (Width / 2) - 7, Height - 6));
  39. }
  40. private void AnimationTick(object sender, EventArgs e)
  41. {
  42. if (Checked)
  43. {
  44. if (Options.TextColor == Color.Black)
  45. {
  46. if (_circleColorR != 0 && _circleColorG != 0 && _circleColorB != 0)
  47. {
  48. _circleColorR -= 15; _circleColorG -= 15; _circleColorB -= 15;
  49. Invalidate();
  50. }
  51. }
  52. else
  53. {
  54. if (_circleColorR != 255 && _circleColorG != 255 && _circleColorB != 255)
  55. {
  56. _circleColorR += 15; _circleColorG += 15; _circleColorB += 15;
  57. Invalidate();
  58. }
  59. }
  60. if (_circlePosX < (Width / 2) + 4)
  61. _circlePosX += 2; Invalidate();
  62. if (_alpha < 255)
  63. _alpha += 15; Invalidate();
  64. }
  65. else
  66. {
  67. if (_circleColorR != 255 && _circleColorG != 255 && _circleColorB != 255)
  68. {
  69. _circleColorR += 15; _circleColorG += 15; _circleColorB += 15;
  70. Invalidate();
  71. }
  72. if (_circlePosX > 3)
  73. _circlePosX -= 2; Invalidate();
  74. if (_alpha > 0)
  75. _alpha -= 15; Invalidate();
  76. }
  77. }
  78. }
  79. }