GraphicsPathHelper.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. namespace LYFZ.ComponentLibrary
  7. {
  8. /// <summary>
  9. /// 建立圆角路径的样式。
  10. /// </summary>
  11. public enum RoundStyle
  12. {
  13. /// <summary>
  14. /// 四个角都不是圆角。
  15. /// </summary>
  16. None = 0,
  17. /// <summary>
  18. /// 四个角都为圆角。
  19. /// </summary>
  20. All = 1,
  21. /// <summary>
  22. /// 左边两个角为圆角。
  23. /// </summary>
  24. Left = 2,
  25. /// <summary>
  26. /// 右边两个角为圆角。
  27. /// </summary>
  28. Right = 3,
  29. /// <summary>
  30. /// 上边两个角为圆角。
  31. /// </summary>
  32. Top = 4,
  33. /// <summary>
  34. /// 下边两个角为圆角。
  35. /// </summary>
  36. Bottom = 5,
  37. /// <summary>
  38. /// 左下角为圆角。
  39. /// </summary>
  40. BottomLeft = 6,
  41. /// <summary>
  42. /// 右下角为圆角。
  43. /// </summary>
  44. BottomRight = 7,
  45. }
  46. public static class GraphicsPathHelper
  47. {
  48. /// <summary>
  49. /// 建立带有圆角样式的路径。
  50. /// </summary>
  51. /// <param name="rect">用来建立路径的矩形。</param>
  52. /// <param name="_radius">圆角的大小。</param>
  53. /// <param name="style">圆角的样式。</param>
  54. /// <param name="correction">是否把矩形长宽减 1,以便画出边框。</param>
  55. /// <returns>建立的路径。</returns>
  56. public static GraphicsPath CreatePath(
  57. Rectangle rect, int radius, RoundStyle style, bool correction)
  58. {
  59. GraphicsPath path = new GraphicsPath();
  60. int radiusCorrection = correction ? 1 : 0;
  61. switch (style)
  62. {
  63. case RoundStyle.None:
  64. path.AddRectangle(rect);
  65. break;
  66. case RoundStyle.All:
  67. path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
  68. path.AddArc(
  69. rect.Right - radius - radiusCorrection,
  70. rect.Y,
  71. radius,
  72. radius,
  73. 270,
  74. 90);
  75. path.AddArc(
  76. rect.Right - radius - radiusCorrection,
  77. rect.Bottom - radius - radiusCorrection,
  78. radius,
  79. radius, 0, 90);
  80. path.AddArc(
  81. rect.X,
  82. rect.Bottom - radius - radiusCorrection,
  83. radius,
  84. radius,
  85. 90,
  86. 90);
  87. break;
  88. case RoundStyle.Left:
  89. path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
  90. path.AddLine(
  91. rect.Right - radiusCorrection, rect.Y,
  92. rect.Right - radiusCorrection, rect.Bottom - radiusCorrection);
  93. path.AddArc(
  94. rect.X,
  95. rect.Bottom - radius - radiusCorrection,
  96. radius,
  97. radius,
  98. 90,
  99. 90);
  100. break;
  101. case RoundStyle.Right:
  102. path.AddArc(
  103. rect.Right - radius - radiusCorrection,
  104. rect.Y,
  105. radius,
  106. radius,
  107. 270,
  108. 90);
  109. path.AddArc(
  110. rect.Right - radius - radiusCorrection,
  111. rect.Bottom - radius - radiusCorrection,
  112. radius,
  113. radius,
  114. 0,
  115. 90);
  116. path.AddLine(rect.X, rect.Bottom - radiusCorrection, rect.X, rect.Y);
  117. break;
  118. case RoundStyle.Top:
  119. path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
  120. path.AddArc(
  121. rect.Right - radius - radiusCorrection,
  122. rect.Y,
  123. radius,
  124. radius,
  125. 270,
  126. 90);
  127. path.AddLine(
  128. rect.Right - radiusCorrection, rect.Bottom - radiusCorrection,
  129. rect.X, rect.Bottom - radiusCorrection);
  130. break;
  131. case RoundStyle.Bottom:
  132. path.AddArc(
  133. rect.Right - radius - radiusCorrection,
  134. rect.Bottom - radius - radiusCorrection,
  135. radius,
  136. radius,
  137. 0,
  138. 90);
  139. path.AddArc(
  140. rect.X,
  141. rect.Bottom - radius - radiusCorrection,
  142. radius,
  143. radius,
  144. 90,
  145. 90);
  146. path.AddLine(rect.X, rect.Y, rect.Right - radiusCorrection, rect.Y);
  147. break;
  148. case RoundStyle.BottomLeft:
  149. path.AddArc(
  150. rect.X,
  151. rect.Bottom - radius - radiusCorrection,
  152. radius,
  153. radius,
  154. 90,
  155. 90);
  156. path.AddLine(rect.X, rect.Y, rect.Right - radiusCorrection, rect.Y);
  157. path.AddLine(
  158. rect.Right - radiusCorrection,
  159. rect.Y,
  160. rect.Right - radiusCorrection,
  161. rect.Bottom - radiusCorrection);
  162. break;
  163. case RoundStyle.BottomRight:
  164. path.AddArc(
  165. rect.Right - radius - radiusCorrection,
  166. rect.Bottom - radius - radiusCorrection,
  167. radius,
  168. radius,
  169. 0,
  170. 90);
  171. path.AddLine(rect.X, rect.Bottom - radiusCorrection, rect.X, rect.Y);
  172. path.AddLine(rect.X, rect.Y, rect.Right - radiusCorrection, rect.Y);
  173. break;
  174. }
  175. path.CloseFigure();
  176. return path;
  177. }
  178. }
  179. }