GraphicsPathHelper.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. namespace LYFZ.OtherExpansion.SkinClass
  5. {
  6. public static class GraphicsPathHelper
  7. {
  8. public static GraphicsPath CreatePath(Rectangle rect, int radius, RoundStyle style, bool correction)
  9. {
  10. GraphicsPath path = new GraphicsPath();
  11. int radiusCorrection = correction ? 1 : 0;
  12. switch (style)
  13. {
  14. case RoundStyle.None:
  15. path.AddRectangle(rect);
  16. break;
  17. case RoundStyle.All:
  18. path.AddArc(rect.X, rect.Y, radius, radius, 180f, 90f);
  19. path.AddArc(rect.Right - radius - radiusCorrection, rect.Y, radius, radius, 270f, 90f);
  20. path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0f, 90f);
  21. path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90f, 90f);
  22. break;
  23. case RoundStyle.Left:
  24. path.AddArc(rect.X, rect.Y, radius, radius, 180f, 90f);
  25. path.AddLine(rect.Right - radiusCorrection, rect.Y, rect.Right - radiusCorrection, rect.Bottom - radiusCorrection);
  26. path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90f, 90f);
  27. break;
  28. case RoundStyle.Right:
  29. path.AddArc(rect.Right - radius - radiusCorrection, rect.Y, radius, radius, 270f, 90f);
  30. path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0f, 90f);
  31. path.AddLine(rect.X, rect.Bottom - radiusCorrection, rect.X, rect.Y);
  32. break;
  33. case RoundStyle.Top:
  34. path.AddArc(rect.X, rect.Y, radius, radius, 180f, 90f);
  35. path.AddArc(rect.Right - radius - radiusCorrection, rect.Y, radius, radius, 270f, 90f);
  36. path.AddLine(rect.Right - radiusCorrection, rect.Bottom - radiusCorrection, rect.X, rect.Bottom - radiusCorrection);
  37. break;
  38. case RoundStyle.Bottom:
  39. path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0f, 90f);
  40. path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90f, 90f);
  41. path.AddLine(rect.X, rect.Y, rect.Right - radiusCorrection, rect.Y);
  42. break;
  43. case RoundStyle.BottomLeft:
  44. path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90f, 90f);
  45. path.AddLine(rect.X, rect.Y, rect.Right - radiusCorrection, rect.Y);
  46. path.AddLine(rect.Right - radiusCorrection, rect.Y, rect.Right - radiusCorrection, rect.Bottom - radiusCorrection);
  47. break;
  48. case RoundStyle.BottomRight:
  49. path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0f, 90f);
  50. path.AddLine(rect.X, rect.Bottom - radiusCorrection, rect.X, rect.Y);
  51. path.AddLine(rect.X, rect.Y, rect.Right - radiusCorrection, rect.Y);
  52. break;
  53. }
  54. path.CloseFigure();
  55. return path;
  56. }
  57. }
  58. }