RectangleEx.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3. namespace Microsoft.Drawing
  4. {
  5. /// <summary>
  6. /// 矩形帮助类
  7. /// </summary>
  8. public static class RectangleEx
  9. {
  10. /// <summary>
  11. /// 调整矩形非空,影响原来矩形
  12. /// </summary>
  13. /// <param name="rect">要调整的矩形</param>
  14. public static void MakeNotEmpty(ref Rectangle rect)
  15. {
  16. if (rect.Width < 1)
  17. rect.Width = 1;
  18. if (rect.Height < 1)
  19. rect.Height = 1;
  20. }
  21. /// <summary>
  22. /// 获取矩形是否可见
  23. /// </summary>
  24. /// <param name="rect">矩形</param>
  25. /// <returns>宽度大于0并且高度大于0返回true,否则返回false</returns>
  26. public static bool IsVisible(Rectangle rect)
  27. {
  28. return rect.Width > 0 && rect.Height > 0;
  29. }
  30. /// <summary>
  31. /// 在指定的方向放大矩形,不影响原来的矩形
  32. /// </summary>
  33. /// <param name="rect">矩形</param>
  34. /// <param name="align">方向</param>
  35. /// <param name="value">放大量</param>
  36. /// <returns>放大后的矩形</returns>
  37. public static Rectangle Inflate(Rectangle rect, TabAlignment align, int value)
  38. {
  39. switch (align)
  40. {
  41. case TabAlignment.Top:
  42. rect.Y -= value;
  43. rect.Height += value;
  44. break;
  45. case TabAlignment.Bottom:
  46. rect.Height += value;
  47. break;
  48. case TabAlignment.Left:
  49. rect.X -= value;
  50. rect.Width += value;
  51. break;
  52. case TabAlignment.Right:
  53. rect.Width += value;
  54. break;
  55. default:
  56. break;
  57. }
  58. return rect;
  59. }
  60. /// <summary>
  61. /// 在指定的方向和反向放大矩形,不影响原来的矩形
  62. /// </summary>
  63. /// <param name="rect">矩形</param>
  64. /// <param name="align">方向</param>
  65. /// <param name="value">放大量</param>
  66. /// <param name="revalue">反向放大量</param>
  67. /// <returns>放大后矩形</returns>
  68. public static Rectangle Inflate(Rectangle rect, TabAlignment align, int value, int revalue)
  69. {
  70. switch (align)
  71. {
  72. case TabAlignment.Top:
  73. rect.Y -= value;
  74. rect.Height += value + revalue;
  75. break;
  76. case TabAlignment.Bottom:
  77. rect.Y -= revalue;
  78. rect.Height += revalue + value;
  79. break;
  80. case TabAlignment.Left:
  81. rect.X -= value;
  82. rect.Width += value + revalue;
  83. break;
  84. case TabAlignment.Right:
  85. rect.X -= revalue;
  86. rect.Width += revalue + value;
  87. break;
  88. default:
  89. break;
  90. }
  91. return rect;
  92. }
  93. /// <summary>
  94. /// 在指定的方向的两侧放大矩形,不影响原来的矩形
  95. /// </summary>
  96. /// <param name="rect">矩形</param>
  97. /// <param name="align">方向</param>
  98. /// <param name="value">放大量</param>
  99. /// <returns>放大后矩形</returns>
  100. public static Rectangle InflateSide(Rectangle rect, TabAlignment align, int value)
  101. {
  102. int half = value / 2;
  103. switch (align)
  104. {
  105. case TabAlignment.Top:
  106. case TabAlignment.Bottom:
  107. rect.X -= half;
  108. rect.Width += value;
  109. break;
  110. case TabAlignment.Left:
  111. case TabAlignment.Right:
  112. rect.Y -= half;
  113. rect.Height += value;
  114. break;
  115. default:
  116. break;
  117. }
  118. return rect;
  119. }
  120. /// <summary>
  121. /// 按指定边调整矩形,使在该方向上的大小为指定值
  122. /// </summary>
  123. /// <param name="rect">矩形</param>
  124. /// <param name="align">方向</param>
  125. /// <param name="value">大小</param>
  126. /// <returns>调整后矩形</returns>
  127. public static Rectangle Adjust(Rectangle rect, TabAlignment align, int value)
  128. {
  129. switch (align)
  130. {
  131. case TabAlignment.Top:
  132. rect.Y += (rect.Height - value);
  133. rect.Height = value;
  134. break;
  135. case TabAlignment.Bottom:
  136. rect.Height = value;
  137. break;
  138. case TabAlignment.Left:
  139. rect.X += (rect.Width - value);
  140. rect.Width = value;
  141. break;
  142. case TabAlignment.Right:
  143. rect.Width = value;
  144. break;
  145. default:
  146. break;
  147. }
  148. return rect;
  149. }
  150. /// <summary>
  151. /// 调整矩形使其在指定方向上与标准矩形对齐,并加上偏移量,不影响原来的矩形
  152. /// </summary>
  153. /// <param name="rect">矩形</param>
  154. /// <param name="rectStand">标准矩形</param>
  155. /// <param name="align">方向</param>
  156. /// <param name="offset">偏移量</param>
  157. /// <returns>调整后的矩形</returns>
  158. public static Rectangle Align(Rectangle rect, Rectangle rectStand, TabAlignment align, int offset)
  159. {
  160. int value;
  161. switch (align)
  162. {
  163. case TabAlignment.Top:
  164. value = rect.Y - rectStand.Y + offset;
  165. rect.Y -= value;
  166. rect.Height += value;
  167. break;
  168. case TabAlignment.Bottom:
  169. value = rectStand.Bottom - rect.Bottom + offset;
  170. rect.Height += value;
  171. break;
  172. case TabAlignment.Left:
  173. value = rect.X - rectStand.X + offset;
  174. rect.X -= value;
  175. rect.Width += value;
  176. break;
  177. case TabAlignment.Right:
  178. value = rectStand.Right - rect.Right + offset;
  179. rect.Width += value;
  180. break;
  181. default:
  182. break;
  183. }
  184. return rect;
  185. }
  186. /// <summary>
  187. /// 矩形加上Padding后的新矩形
  188. /// </summary>
  189. /// <param name="rect">矩形</param>
  190. /// <param name="padding">外边距</param>
  191. /// <returns>新矩形</returns>
  192. public static Rectangle Add(Rectangle rect, Padding padding)
  193. {
  194. return new Rectangle(rect.Left - padding.Left, rect.Top - padding.Top, rect.Width + padding.Horizontal, rect.Height + padding.Vertical);
  195. }
  196. /// <summary>
  197. /// 矩形减去Padding后的新矩形
  198. /// </summary>
  199. /// <param name="rect">矩形</param>
  200. /// <param name="padding">内边距</param>
  201. /// <returns>新矩形</returns>
  202. public static Rectangle Subtract(Rectangle rect, Padding padding)
  203. {
  204. return new Rectangle(rect.Left + padding.Left, rect.Top + padding.Top, rect.Width - padding.Horizontal, rect.Height - padding.Vertical);
  205. }
  206. /// <summary>
  207. /// 将此矩形的位置调整指定的量。返回新矩形,不影响原来值。
  208. /// </summary>
  209. /// <param name="rect">矩形</param>
  210. /// <param name="pos">该位置的偏移量</param>
  211. /// <returns>偏移后的新矩形</returns>
  212. public static Rectangle Offset(Rectangle rect, Point pos)
  213. {
  214. rect.Offset(pos);
  215. return rect;
  216. }
  217. /// <summary>
  218. /// 将矩形的位置调整指定的量。返回新矩形,不影响原来值。
  219. /// </summary>
  220. /// <param name="rect">矩形</param>
  221. /// <param name="x">水平偏移量</param>
  222. /// <param name="y">垂直偏移量</param>
  223. /// <returns>偏移后的新矩形</returns>
  224. public static Rectangle Offset(Rectangle rect, int x, int y)
  225. {
  226. rect.Offset(x, y);
  227. return rect;
  228. }
  229. }
  230. }