2
0

Size.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // System.Drawing.Size.cs
  3. //
  4. // Author:
  5. // Mike Kestner ([email protected])
  6. //
  7. // (C) 2001 Mike Kestner
  8. //
  9. using System;
  10. using System.Runtime.Serialization;
  11. using System.Runtime.InteropServices;
  12. using System.ComponentModel;
  13. namespace System.Drawing {
  14. [Serializable]
  15. [ComVisible (true)]
  16. [TypeConverter(typeof(SizeConverter))]
  17. public struct Size {
  18. // Private Height and width fields.
  19. int width, height;
  20. // -----------------------
  21. // Public Shared Members
  22. // -----------------------
  23. /// <summary>
  24. /// Empty Shared Field
  25. /// </summary>
  26. ///
  27. /// <remarks>
  28. /// An uninitialized Size Structure.
  29. /// </remarks>
  30. public static readonly Size Empty;
  31. /// <summary>
  32. /// Ceiling Shared Method
  33. /// </summary>
  34. ///
  35. /// <remarks>
  36. /// Produces a Size structure from a SizeF structure by
  37. /// taking the ceiling of the Width and Height properties.
  38. /// </remarks>
  39. public static Size Ceiling (SizeF value)
  40. {
  41. int w, h;
  42. checked {
  43. w = (int) Math.Ceiling (value.Width);
  44. h = (int) Math.Ceiling (value.Height);
  45. }
  46. return new Size (w, h);
  47. }
  48. /// <summary>
  49. /// Round Shared Method
  50. /// </summary>
  51. ///
  52. /// <remarks>
  53. /// Produces a Size structure from a SizeF structure by
  54. /// rounding the Width and Height properties.
  55. /// </remarks>
  56. public static Size Round (SizeF value)
  57. {
  58. int w, h;
  59. checked {
  60. w = (int) Math.Round (value.Width);
  61. h = (int) Math.Round (value.Height);
  62. }
  63. return new Size (w, h);
  64. }
  65. /// <summary>
  66. /// Truncate Shared Method
  67. /// </summary>
  68. ///
  69. /// <remarks>
  70. /// Produces a Size structure from a SizeF structure by
  71. /// truncating the Width and Height properties.
  72. /// </remarks>
  73. public static Size Truncate (SizeF value)
  74. {
  75. int w, h;
  76. checked {
  77. w = (int) value.Width;
  78. h = (int) value.Height;
  79. }
  80. return new Size (w, h);
  81. }
  82. /// <summary>
  83. /// Addition Operator
  84. /// </summary>
  85. ///
  86. /// <remarks>
  87. /// Addition of two Size structures.
  88. /// </remarks>
  89. public static Size operator + (Size sz1, Size sz2)
  90. {
  91. return new Size (sz1.Width + sz2.Width,
  92. sz1.Height + sz2.Height);
  93. }
  94. /// <summary>
  95. /// Equality Operator
  96. /// </summary>
  97. ///
  98. /// <remarks>
  99. /// Compares two Size objects. The return value is
  100. /// based on the equivalence of the Width and Height
  101. /// properties of the two Sizes.
  102. /// </remarks>
  103. public static bool operator == (Size sz_a, Size sz_b)
  104. {
  105. return ((sz_a.Width == sz_b.Width) &&
  106. (sz_a.Height == sz_b.Height));
  107. }
  108. /// <summary>
  109. /// Inequality Operator
  110. /// </summary>
  111. ///
  112. /// <remarks>
  113. /// Compares two Size objects. The return value is
  114. /// based on the equivalence of the Width and Height
  115. /// properties of the two Sizes.
  116. /// </remarks>
  117. public static bool operator != (Size sz_a, Size sz_b)
  118. {
  119. return ((sz_a.Width != sz_b.Width) ||
  120. (sz_a.Height != sz_b.Height));
  121. }
  122. /// <summary>
  123. /// Subtraction Operator
  124. /// </summary>
  125. ///
  126. /// <remarks>
  127. /// Subtracts two Size structures.
  128. /// </remarks>
  129. public static Size operator - (Size sz1, Size sz2)
  130. {
  131. return new Size (sz1.Width - sz2.Width,
  132. sz1.Height - sz2.Height);
  133. }
  134. /// <summary>
  135. /// Size to Point Conversion
  136. /// </summary>
  137. ///
  138. /// <remarks>
  139. /// Returns a Point based on the dimensions of a given
  140. /// Size. Requires explicit cast.
  141. /// </remarks>
  142. public static explicit operator Point (Size sz)
  143. {
  144. return new Point (sz.Width, sz.Height);
  145. }
  146. /// <summary>
  147. /// Size to SizeF Conversion
  148. /// </summary>
  149. ///
  150. /// <remarks>
  151. /// Creates a SizeF based on the dimensions of a given
  152. /// Size. No explicit cast is required.
  153. /// </remarks>
  154. public static implicit operator SizeF (Size sz)
  155. {
  156. return new SizeF (sz.Width, sz.Height);
  157. }
  158. // -----------------------
  159. // Public Constructors
  160. // -----------------------
  161. /// <summary>
  162. /// Size Constructor
  163. /// </summary>
  164. ///
  165. /// <remarks>
  166. /// Creates a Size from a Point value.
  167. /// </remarks>
  168. public Size (Point pt)
  169. {
  170. width = pt.X;
  171. height = pt.Y;
  172. }
  173. /// <summary>
  174. /// Size Constructor
  175. /// </summary>
  176. ///
  177. /// <remarks>
  178. /// Creates a Size from specified dimensions.
  179. /// </remarks>
  180. public Size (int width, int height)
  181. {
  182. this.width = width;
  183. this.height = height;
  184. }
  185. // -----------------------
  186. // Public Instance Members
  187. // -----------------------
  188. /// <summary>
  189. /// IsEmpty Property
  190. /// </summary>
  191. ///
  192. /// <remarks>
  193. /// Indicates if both Width and Height are zero.
  194. /// </remarks>
  195. [Browsable (false)]
  196. public bool IsEmpty {
  197. get {
  198. return ((width == 0) && (height == 0));
  199. }
  200. }
  201. /// <summary>
  202. /// Width Property
  203. /// </summary>
  204. ///
  205. /// <remarks>
  206. /// The Width coordinate of the Size.
  207. /// </remarks>
  208. public int Width {
  209. get {
  210. return width;
  211. }
  212. set {
  213. width = value;
  214. }
  215. }
  216. /// <summary>
  217. /// Height Property
  218. /// </summary>
  219. ///
  220. /// <remarks>
  221. /// The Height coordinate of the Size.
  222. /// </remarks>
  223. public int Height {
  224. get {
  225. return height;
  226. }
  227. set {
  228. height = value;
  229. }
  230. }
  231. /// <summary>
  232. /// Equals Method
  233. /// </summary>
  234. ///
  235. /// <remarks>
  236. /// Checks equivalence of this Size and another object.
  237. /// </remarks>
  238. public override bool Equals (object o)
  239. {
  240. if (!(o is Size))
  241. return false;
  242. return (this == (Size) o);
  243. }
  244. /// <summary>
  245. /// GetHashCode Method
  246. /// </summary>
  247. ///
  248. /// <remarks>
  249. /// Calculates a hashing value.
  250. /// </remarks>
  251. public override int GetHashCode ()
  252. {
  253. return width^height;
  254. }
  255. /// <summary>
  256. /// ToString Method
  257. /// </summary>
  258. ///
  259. /// <remarks>
  260. /// Formats the Size as a string in coordinate notation.
  261. /// </remarks>
  262. public override string ToString ()
  263. {
  264. return String.Format ("{{Width={0}, Height={1}}}", width, height);
  265. }
  266. }
  267. }