MoonButton.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 MoonButton : Button
  8. {
  9. private int _borderRadius = 5;
  10. public MoonButton()
  11. {
  12. BackColor = Color.White;
  13. ForeColor = Color.Black;
  14. FlatStyle = FlatStyle.Flat;
  15. FlatAppearance.BorderSize = 0;
  16. }
  17. private GraphicsPath GetFigurePath(Rectangle rect, int radius)
  18. {
  19. GraphicsPath path = new GraphicsPath();
  20. float curveSize = radius * 2F;
  21. path.StartFigure();
  22. path.AddArc(rect.X, rect.Y, curveSize, curveSize, 180, 90);
  23. path.AddArc(rect.Right - curveSize, rect.Y, curveSize, curveSize, 270, 90);
  24. path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 90);
  25. path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 90, 90);
  26. path.CloseFigure();
  27. return path;
  28. }
  29. protected override void OnPaint(PaintEventArgs pevent)
  30. {
  31. base.OnPaint(pevent);
  32. pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  33. pevent.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  34. pevent.Graphics.InterpolationMode = InterpolationMode.High;
  35. Rectangle rectSurface = ClientRectangle;
  36. Rectangle rectBorder = Rectangle.Inflate(rectSurface, 0, 0);
  37. using (GraphicsPath pathSurface = GetFigurePath(rectSurface, _borderRadius))
  38. using (GraphicsPath pathBorder = GetFigurePath(rectBorder, _borderRadius))
  39. using (Pen penSurface = new Pen(Parent.BackColor, 2))
  40. {
  41. Region = new Region(pathSurface);
  42. pevent.Graphics.DrawPath(penSurface, pathSurface);
  43. }
  44. }
  45. protected override void OnResize(EventArgs e)
  46. {
  47. base.OnResize(e);
  48. if (_borderRadius > Height)
  49. _borderRadius = Height;
  50. }
  51. }
  52. }