| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- //
- // System.Drawing.Size.cs
- //
- // Author:
- // Mike Kestner ([email protected])
- //
- // (C) 2001 Mike Kestner
- //
- using System;
- using System.Runtime.Serialization;
- using System.Runtime.InteropServices;
- using System.ComponentModel;
- namespace System.Drawing {
-
- [Serializable]
- [ComVisible (true)]
- [TypeConverter(typeof(SizeConverter))]
- public struct Size {
-
- // Private Height and width fields.
- int width, height;
- // -----------------------
- // Public Shared Members
- // -----------------------
- /// <summary>
- /// Empty Shared Field
- /// </summary>
- ///
- /// <remarks>
- /// An uninitialized Size Structure.
- /// </remarks>
-
- public static readonly Size Empty;
- /// <summary>
- /// Ceiling Shared Method
- /// </summary>
- ///
- /// <remarks>
- /// Produces a Size structure from a SizeF structure by
- /// taking the ceiling of the Width and Height properties.
- /// </remarks>
-
- public static Size Ceiling (SizeF value)
- {
- int w, h;
- checked {
- w = (int) Math.Ceiling (value.Width);
- h = (int) Math.Ceiling (value.Height);
- }
- return new Size (w, h);
- }
- /// <summary>
- /// Round Shared Method
- /// </summary>
- ///
- /// <remarks>
- /// Produces a Size structure from a SizeF structure by
- /// rounding the Width and Height properties.
- /// </remarks>
-
- public static Size Round (SizeF value)
- {
- int w, h;
- checked {
- w = (int) Math.Round (value.Width);
- h = (int) Math.Round (value.Height);
- }
- return new Size (w, h);
- }
- /// <summary>
- /// Truncate Shared Method
- /// </summary>
- ///
- /// <remarks>
- /// Produces a Size structure from a SizeF structure by
- /// truncating the Width and Height properties.
- /// </remarks>
-
- public static Size Truncate (SizeF value)
- {
- int w, h;
- checked {
- w = (int) value.Width;
- h = (int) value.Height;
- }
- return new Size (w, h);
- }
- /// <summary>
- /// Addition Operator
- /// </summary>
- ///
- /// <remarks>
- /// Addition of two Size structures.
- /// </remarks>
- public static Size operator + (Size sz1, Size sz2)
- {
- return new Size (sz1.Width + sz2.Width,
- sz1.Height + sz2.Height);
- }
-
- /// <summary>
- /// Equality Operator
- /// </summary>
- ///
- /// <remarks>
- /// Compares two Size objects. The return value is
- /// based on the equivalence of the Width and Height
- /// properties of the two Sizes.
- /// </remarks>
- public static bool operator == (Size sz_a, Size sz_b)
- {
- return ((sz_a.Width == sz_b.Width) &&
- (sz_a.Height == sz_b.Height));
- }
-
- /// <summary>
- /// Inequality Operator
- /// </summary>
- ///
- /// <remarks>
- /// Compares two Size objects. The return value is
- /// based on the equivalence of the Width and Height
- /// properties of the two Sizes.
- /// </remarks>
- public static bool operator != (Size sz_a, Size sz_b)
- {
- return ((sz_a.Width != sz_b.Width) ||
- (sz_a.Height != sz_b.Height));
- }
-
- /// <summary>
- /// Subtraction Operator
- /// </summary>
- ///
- /// <remarks>
- /// Subtracts two Size structures.
- /// </remarks>
- public static Size operator - (Size sz1, Size sz2)
- {
- return new Size (sz1.Width - sz2.Width,
- sz1.Height - sz2.Height);
- }
-
- /// <summary>
- /// Size to Point Conversion
- /// </summary>
- ///
- /// <remarks>
- /// Returns a Point based on the dimensions of a given
- /// Size. Requires explicit cast.
- /// </remarks>
- public static explicit operator Point (Size sz)
- {
- return new Point (sz.Width, sz.Height);
- }
- /// <summary>
- /// Size to SizeF Conversion
- /// </summary>
- ///
- /// <remarks>
- /// Creates a SizeF based on the dimensions of a given
- /// Size. No explicit cast is required.
- /// </remarks>
- public static implicit operator SizeF (Size sz)
- {
- return new SizeF (sz.Width, sz.Height);
- }
- // -----------------------
- // Public Constructors
- // -----------------------
- /// <summary>
- /// Size Constructor
- /// </summary>
- ///
- /// <remarks>
- /// Creates a Size from a Point value.
- /// </remarks>
-
- public Size (Point pt)
- {
- width = pt.X;
- height = pt.Y;
- }
- /// <summary>
- /// Size Constructor
- /// </summary>
- ///
- /// <remarks>
- /// Creates a Size from specified dimensions.
- /// </remarks>
-
- public Size (int width, int height)
- {
- this.width = width;
- this.height = height;
- }
- // -----------------------
- // Public Instance Members
- // -----------------------
- /// <summary>
- /// IsEmpty Property
- /// </summary>
- ///
- /// <remarks>
- /// Indicates if both Width and Height are zero.
- /// </remarks>
-
- [Browsable (false)]
- public bool IsEmpty {
- get {
- return ((width == 0) && (height == 0));
- }
- }
- /// <summary>
- /// Width Property
- /// </summary>
- ///
- /// <remarks>
- /// The Width coordinate of the Size.
- /// </remarks>
-
- public int Width {
- get {
- return width;
- }
- set {
- width = value;
- }
- }
- /// <summary>
- /// Height Property
- /// </summary>
- ///
- /// <remarks>
- /// The Height coordinate of the Size.
- /// </remarks>
-
- public int Height {
- get {
- return height;
- }
- set {
- height = value;
- }
- }
- /// <summary>
- /// Equals Method
- /// </summary>
- ///
- /// <remarks>
- /// Checks equivalence of this Size and another object.
- /// </remarks>
-
- public override bool Equals (object o)
- {
- if (!(o is Size))
- return false;
- return (this == (Size) o);
- }
- /// <summary>
- /// GetHashCode Method
- /// </summary>
- ///
- /// <remarks>
- /// Calculates a hashing value.
- /// </remarks>
-
- public override int GetHashCode ()
- {
- return width^height;
- }
- /// <summary>
- /// ToString Method
- /// </summary>
- ///
- /// <remarks>
- /// Formats the Size as a string in coordinate notation.
- /// </remarks>
-
- public override string ToString ()
- {
- return String.Format ("{{Width={0}, Height={1}}}", width, height);
- }
- }
- }
|