2 // System.Drawing.SizeF.cs
5 // Mike Kestner (mkestner@speakeasy.net)
7 // Copyright (C) 2001 Mike Kestner
8 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Globalization;
36 using System.Runtime.InteropServices;
37 using System.ComponentModel;
39 namespace System.Drawing
45 // Private height and width fields.
48 // -----------------------
49 // Public Shared Members
50 // -----------------------
53 /// Empty Shared Field
57 /// An uninitialized SizeF Structure.
60 public static readonly SizeF Empty;
67 /// Addition of two SizeF structures.
70 public static SizeF operator + (SizeF sz1, SizeF sz2)
72 return new SizeF (sz1.Width + sz2.Width,
73 sz1.Height + sz2.Height);
81 /// Compares two SizeF objects. The return value is
82 /// based on the equivalence of the Width and Height
83 /// properties of the two Sizes.
86 public static bool operator == (SizeF sz_a, SizeF sz_b)
88 return ((sz_a.Width == sz_b.Width) &&
89 (sz_a.Height == sz_b.Height));
93 /// Inequality Operator
97 /// Compares two SizeF objects. The return value is
98 /// based on the equivalence of the Width and Height
99 /// properties of the two Sizes.
102 public static bool operator != (SizeF sz_a, SizeF sz_b)
104 return ((sz_a.Width != sz_b.Width) ||
105 (sz_a.Height != sz_b.Height));
109 /// Subtraction Operator
113 /// Subtracts two SizeF structures.
116 public static SizeF operator - (SizeF sz1, SizeF sz2)
118 return new SizeF (sz1.Width - sz2.Width,
119 sz1.Height - sz2.Height);
123 /// SizeF to PointF Conversion
127 /// Returns a PointF based on the dimensions of a given
128 /// SizeF. Requires explicit cast.
131 public static explicit operator PointF (SizeF sz)
133 return new PointF (sz.Width, sz.Height);
137 // -----------------------
138 // Public Constructors
139 // -----------------------
142 /// SizeF Constructor
146 /// Creates a SizeF from a PointF value.
149 public SizeF (PointF pt)
156 /// SizeF Constructor
160 /// Creates a SizeF from an existing SizeF value.
163 public SizeF (SizeF sz)
170 /// SizeF Constructor
174 /// Creates a SizeF from specified dimensions.
177 public SizeF (float width, float height)
183 // -----------------------
184 // Public Instance Members
185 // -----------------------
192 /// Indicates if both Width and Height are zero.
196 public bool IsEmpty {
198 return ((wd == 0.0) && (ht == 0.0));
207 /// The Width coordinate of the SizeF.
224 /// The Height coordinate of the SizeF.
227 public float Height {
241 /// Checks equivalence of this SizeF and another object.
244 public override bool Equals (object o)
249 return (this == (SizeF) o);
253 /// GetHashCode Method
257 /// Calculates a hashing value.
260 public override int GetHashCode ()
262 return (int) wd ^ (int) ht;
265 public PointF ToPointF ()
267 return new PointF (wd, ht);
270 public Size ToSize ()
278 return new Size (w, h);
286 /// Formats the SizeF as a string in coordinate notation.
289 public override string ToString ()
291 return string.Format ("{{Width={0}, Height={1}}}", wd.ToString (CultureInfo.InvariantCulture),
\r
292 ht.ToString (CultureInfo.InvariantCulture));