You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
5.7 KiB

1 month ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. namespace Customize.Controls
  8. {
  9. public class RoundRectangle
  10. {
  11. #region Initializes
  12. /// <summary>
  13. /// (构造函数).Initializes a new instance of the <see cref="RoundRectangle"/> class.
  14. /// </summary>
  15. /// <param name="roundRect">The roundRect.</param>
  16. /// <param name="radius">The radius.</param>
  17. public RoundRectangle(Rectangle rect, int radius)
  18. : this(rect, new CornerRadius(radius))
  19. {
  20. }
  21. /// <summary>
  22. /// (构造函数).Initializes a new instance of the <see cref="RoundRectangle"/> class.
  23. /// </summary>
  24. /// <param name="roundRect">The roundRect.</param>
  25. /// <param name="_CornerRadius">The corner radius.</param>
  26. public RoundRectangle(Rectangle rect, CornerRadius cornerRadius)
  27. {
  28. this.Rect = rect;
  29. this.CornerRadius = cornerRadius;
  30. }
  31. #endregion
  32. #region Properties
  33. /// <summary>
  34. /// 获取或者设置矩形区域
  35. /// </summary>
  36. /// <value>The roundRect.</value>
  37. public Rectangle Rect { get; set; }
  38. /// <summary>
  39. /// 获取或者设置圆角值
  40. /// </summary>
  41. /// <value>The corner radius.</value>
  42. public CornerRadius CornerRadius { get; set; }
  43. #endregion
  44. #region Methods
  45. /// <summary>
  46. /// 获取该圆角矩形的GraphicsPath对象(圆角使用Bezier曲线实现)
  47. /// </summary>
  48. /// <returns>
  49. /// Return a data(or instance) of GraphicsPath.
  50. /// </returns>
  51. public GraphicsPath ToGraphicsBezierPath()
  52. {
  53. GraphicsPath path = new GraphicsPath();
  54. int x = this.Rect.X;
  55. int y = this.Rect.Y;
  56. int w = this.Rect.Width;
  57. int h = this.Rect.Height;
  58. path.AddBezier(x, y + this.CornerRadius.TopLeft, x, y, x + this.CornerRadius.TopLeft, y, x + this.CornerRadius.TopLeft, y);
  59. path.AddLine(x + this.CornerRadius.TopLeft, y, x + w - this.CornerRadius.TopRight, y);
  60. path.AddBezier(x + w - this.CornerRadius.TopRight, y, x + w, y, x + w, y + this.CornerRadius.TopRight, x + w, y + this.CornerRadius.TopRight);
  61. path.AddLine(x + w, y + this.CornerRadius.TopRight, x + w, y + h - this.CornerRadius.BottomRigth);
  62. path.AddBezier(x + w, y + h - this.CornerRadius.BottomRigth, x + w, y + h, x + w - this.CornerRadius.BottomRigth, y + h, x + w - this.CornerRadius.BottomRigth, y + h);
  63. path.AddLine(x + w - this.CornerRadius.BottomRigth, y + h, x + this.CornerRadius.BottomLeft, y + h);
  64. path.AddBezier(x + this.CornerRadius.BottomLeft, y + h, x, y + h, x, y + h - this.CornerRadius.BottomLeft, x, y + h - this.CornerRadius.BottomLeft);
  65. path.AddLine(x, y + h - this.CornerRadius.BottomLeft, x, y + this.CornerRadius.TopLeft);
  66. path.CloseFigure();
  67. return path;
  68. }
  69. /// <summary>
  70. /// 获取该圆角矩形的GraphicsPath对象(圆角使用矩形圆弧曲线曲线实现)
  71. /// </summary>
  72. /// <returns></returns>
  73. public GraphicsPath ToGraphicsArcPath()
  74. {
  75. GraphicsPath path = new GraphicsPath();
  76. int x = this.Rect.X;
  77. int y = this.Rect.Y;
  78. int w = this.Rect.Width;
  79. int h = this.Rect.Height;
  80. path.AddArc(x, y, this.CornerRadius.TopLeft, this.CornerRadius.TopLeft, 180, 90);
  81. path.AddArc(x + w - this.CornerRadius.TopRight, y, this.CornerRadius.TopRight, this.CornerRadius.TopRight, 270, 90);
  82. path.AddArc(x + w - this.CornerRadius.BottomRigth, y + h - this.CornerRadius.BottomRigth,
  83. this.CornerRadius.BottomRigth, this.CornerRadius.BottomRigth,
  84. 0, 90);
  85. path.AddArc(x, y + h - this.CornerRadius.BottomLeft, this.CornerRadius.BottomLeft, this.CornerRadius.BottomLeft, 90, 90);
  86. path.CloseFigure();
  87. return path;
  88. }
  89. /// <summary>
  90. /// 获取该圆角矩形的GraphicsPath对象(天使之翼的区域样式,主要用于Tabcontrol的标签样式)
  91. /// </summary>
  92. /// <returns>
  93. /// Return a data(or instance) of GraphicsPath.
  94. /// </returns>
  95. public GraphicsPath ToGraphicsAnglesWingPath()
  96. {
  97. GraphicsPath path = new GraphicsPath();
  98. int x = this.Rect.X;
  99. int y = this.Rect.Y;
  100. int w = this.Rect.Width;
  101. int h = this.Rect.Height;
  102. path.AddBezier(x, y + this.CornerRadius.TopLeft, x, y, x + this.CornerRadius.TopLeft, y, x + this.CornerRadius.TopLeft, y);
  103. path.AddLine(x + this.CornerRadius.TopLeft, y, x + w - this.CornerRadius.TopRight, y);
  104. path.AddBezier(x + w - this.CornerRadius.TopRight, y, x + w, y, x + w, y + this.CornerRadius.TopRight, x + w, y + this.CornerRadius.TopRight);
  105. path.AddLine(x + w, y + this.CornerRadius.TopRight, x + w, y + h - this.CornerRadius.BottomRigth);
  106. path.AddBezier(x + w, y + h - this.CornerRadius.BottomRigth, x + w, y + h, x + w + this.CornerRadius.BottomRigth, y + h, x + w + this.CornerRadius.BottomRigth, y + h);
  107. path.AddLine(x + w + this.CornerRadius.BottomRigth, y + h, x - this.CornerRadius.BottomLeft, y + h);
  108. path.AddBezier(x - this.CornerRadius.BottomLeft, y + h, x, y + h, x, y + h - this.CornerRadius.BottomLeft, x, y + h - this.CornerRadius.BottomLeft);
  109. path.AddLine(x, y + h - this.CornerRadius.BottomLeft, x, y + this.CornerRadius.TopLeft);
  110. path.CloseFigure();
  111. return path;
  112. }
  113. #endregion
  114. }
  115. }