RECT.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System.Drawing;
  2. using System.Runtime.InteropServices;
  3. namespace Microsoft.Win32
  4. {
  5. /// <summary>
  6. /// 矩形结构定义
  7. /// </summary>
  8. public static partial class NativeMethods
  9. {
  10. /// <summary>
  11. /// 矩形结构
  12. /// </summary>
  13. [StructLayout(LayoutKind.Sequential)]
  14. public struct RECT
  15. {
  16. /// <summary>
  17. /// 左上角水平坐标
  18. /// </summary>
  19. public int left;
  20. /// <summary>
  21. /// 左上角垂直坐标
  22. /// </summary>
  23. public int top;
  24. /// <summary>
  25. /// 右下角水平坐标
  26. /// </summary>
  27. public int right;
  28. /// <summary>
  29. /// 右下角垂直坐标
  30. /// </summary>
  31. public int bottom;
  32. /// <summary>
  33. /// 构造函数
  34. /// </summary>
  35. /// <param name="left">左上角水平坐标</param>
  36. /// <param name="top">左上角垂直坐标</param>
  37. /// <param name="right">右下角水平坐标</param>
  38. /// <param name="bottom">右下角垂直坐标</param>
  39. public RECT(int left, int top, int right, int bottom)
  40. {
  41. this.left = left;
  42. this.top = top;
  43. this.right = right;
  44. this.bottom = bottom;
  45. }
  46. /// <summary>
  47. /// 构造函数
  48. /// </summary>
  49. /// <param name="r">Rectangle结构</param>
  50. public RECT(Rectangle r)
  51. {
  52. this.left = r.Left;
  53. this.top = r.Top;
  54. this.right = r.Right;
  55. this.bottom = r.Bottom;
  56. }
  57. /// <summary>
  58. /// 构造函数
  59. /// </summary>
  60. /// <param name="x">左上角水平坐标</param>
  61. /// <param name="y">左上角垂直坐标</param>
  62. /// <param name="width">宽度</param>
  63. /// <param name="height">高度</param>
  64. /// <returns>矩形结构</returns>
  65. public static NativeMethods.RECT FromXYWH(int x, int y, int width, int height)
  66. {
  67. return new NativeMethods.RECT(x, y, x + width, y + height);
  68. }
  69. /// <summary>
  70. /// 宽度
  71. /// </summary>
  72. public int Width
  73. {
  74. get
  75. {
  76. return this.right - this.left;
  77. }
  78. }
  79. /// <summary>
  80. /// 高度
  81. /// </summary>
  82. public int Height
  83. {
  84. get
  85. {
  86. return this.bottom - this.top;
  87. }
  88. }
  89. /// <summary>
  90. /// 左上角
  91. /// </summary>
  92. public Point Location
  93. {
  94. get
  95. {
  96. return new Point(this.left, this.top);
  97. }
  98. }
  99. /// <summary>
  100. /// 右下角
  101. /// </summary>
  102. public Point BottomRight
  103. {
  104. get
  105. {
  106. return new Point(this.right, this.bottom);
  107. }
  108. }
  109. /// <summary>
  110. /// 大小
  111. /// </summary>
  112. public Size Size
  113. {
  114. get
  115. {
  116. return new Size(this.right - this.left, this.bottom - this.top);
  117. }
  118. }
  119. /// <summary>
  120. /// 转换为 System.Drawing.Rectangle.
  121. /// </summary>
  122. /// <returns>System.Drawing.Rectangle</returns>
  123. public Rectangle ToRectangle()
  124. {
  125. return new Rectangle(this.left, this.top, this.right - this.left, this.bottom - this.top);
  126. }
  127. /// <summary>
  128. /// 是否包含指定点
  129. /// </summary>
  130. /// <param name="pt">点</param>
  131. /// <returns>包含返回true,否则返回false</returns>
  132. public bool Contains(NativeMethods.POINT pt)
  133. {
  134. return this.Contains(pt.x, pt.y);
  135. }
  136. /// <summary>
  137. /// 是否包含指定点
  138. /// </summary>
  139. /// <param name="pt">点</param>
  140. /// <returns>包含返回true,否则返回false</returns>
  141. public bool Contains(Point pt)
  142. {
  143. return this.Contains(pt.X, pt.Y);
  144. }
  145. /// <summary>
  146. /// 是否包含指定坐标
  147. /// </summary>
  148. /// <param name="x">水平坐标</param>
  149. /// <param name="y">垂直坐标</param>
  150. /// <returns>包含返回true,否则返回false</returns>
  151. public bool Contains(int x, int y)
  152. {
  153. return ((this.left <= x) && (x < this.right) && (this.top <= y) && (y < this.bottom));
  154. }
  155. }
  156. }
  157. }