1 /* Licensed under the MIT/X11 license.
2 * Copyright (c) 2006-2008 the OpenTK team.
3 * This notice may not be removed.
4 * See license.txt for licensing detailed licensing details.
14 /// <summary>Contains information regarding a monitor's display resolution.</summary>
15 public class DisplayResolution
17 private Rectangle bounds;
19 internal DisplayResolution() { }
21 // Creates a new DisplayResolution object for the primary DisplayDevice.
22 internal DisplayResolution(int x, int y, int width, int height, int bitsPerPixel, float refreshRate)
24 // Refresh rate may be zero, since this information may not be available on some platforms.
27 throw new ArgumentOutOfRangeException("width", "Must be greater than zero.");
31 throw new ArgumentOutOfRangeException("height", "Must be greater than zero.");
33 if (bitsPerPixel <= 0)
35 throw new ArgumentOutOfRangeException("bitsPerPixel", "Must be greater than zero.");
39 throw new ArgumentOutOfRangeException("refreshRate", "Must be greater than, or equal to zero.");
42 this.bounds = new Rectangle(x, y, width, height);
43 this.BitsPerPixel = bitsPerPixel;
44 this.RefreshRate = refreshRate;
50 /// Creates a new DisplayResolution object for the specified DisplayDevice.
52 /// <param name="width">The requested width in pixels.</param>
53 /// <param name="height">The requested height in pixels.</param>
54 /// <param name="bitsPerPixel">The requested bits per pixel in bits.</param>
55 /// <param name="refreshRate">The requested refresh rate in hertz.</param>
56 /// <remarks>OpenTK will select the closest match between all available resolutions on the specified DisplayDevice.</remarks>
58 public DisplayResolution(int width, int height, int bitsPerPixel, float refreshRate, DisplayDevice device)
60 // Refresh rate may be zero, since this information may not be available on some platforms.
61 if (width <= 0) throw new ArgumentOutOfRangeException("width", "Must be greater than zero.");
62 if (height <= 0) throw new ArgumentOutOfRangeException("height", "Must be greater than zero.");
63 if (bitsPerPixel <= 0) throw new ArgumentOutOfRangeException("bitsPerPixel", "Must be greater than zero.");
64 if (refreshRate < 0) throw new ArgumentOutOfRangeException("refreshRate", "Must be greater than, or equal to zero.");
65 if (device == null) throw new ArgumentNullException("DisplayDevice", "Must be a valid DisplayDevice");
67 DisplayResolution res = device.SelectResolution(width, height, bitsPerPixel, refreshRate);
69 this.width = res.width;
70 this.height = res.height;
71 this.bits_per_pixel = res.bits_per_pixel;
72 this.refresh_rate = res.refresh_rate;
77 /// Gets a System.Drawing.Rectangle that contains the bounds of this display device.
79 [Obsolete("This property will return invalid results if a monitor changes resolution. Use DisplayDevice.Bounds instead.")]
80 [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
81 public Rectangle Bounds
83 get { return bounds; }
86 /// <summary>Gets a System.Int32 that contains the width of this display in pixels.</summary>
89 get { return bounds.Width; }
90 internal set { bounds.Width = value; }
93 /// <summary>Gets a System.Int32 that contains the height of this display in pixels.</summary>
96 get { return bounds.Height; }
97 internal set { bounds.Height = value; }
100 /// <summary>Gets a System.Int32 that contains number of bits per pixel of this display. Typical values include 8, 16, 24 and 32.</summary>
101 public int BitsPerPixel { get; internal set; }
104 /// Gets a System.Single representing the vertical refresh rate of this display.
106 public float RefreshRate { get; internal set; }
109 /// Returns a System.String representing this DisplayResolution.
111 /// <returns>A System.String representing this DisplayResolution.</returns>
112 public override string ToString()
114 #pragma warning disable 612,618
115 return String.Format("{0}x{1}@{2}Hz", Bounds, BitsPerPixel, RefreshRate);
116 #pragma warning restore 612,618
119 /// <summary>Determines whether the specified resolutions are equal.</summary>
120 /// <param name="obj">The System.Object to check against.</param>
121 /// <returns>True if the System.Object is an equal DisplayResolution; false otherwise.</returns>
122 public override bool Equals(object obj)
128 if (this.GetType() == obj.GetType())
130 DisplayResolution res = (DisplayResolution)obj;
132 Width == res.Width &&
133 Height == res.Height &&
134 BitsPerPixel == res.BitsPerPixel &&
135 RefreshRate == res.RefreshRate;
141 /// <summary>Returns a unique hash representing this resolution.</summary>
142 /// <returns>A System.Int32 that may serve as a hash code for this resolution.</returns>
143 public override int GetHashCode()
145 #pragma warning disable 612,618
146 return Bounds.GetHashCode() ^ BitsPerPixel ^ RefreshRate.GetHashCode();
147 #pragma warning restore 612,618
151 /// Compares two instances for equality.
153 /// <param name="left">The first instance.</param>
154 /// <param name="right">The second instance.</param>
155 /// <returns>True, if left equals right; false otherwise.</returns>
156 public static bool operator== (DisplayResolution left, DisplayResolution right)
158 if (((object)left) == null && ((object)right) == null)
162 else if ((((object)left) == null && ((object)right) != null) ||
163 (((object)left) != null && ((object)right) == null))
167 return left.Equals(right);
171 /// Compares two instances for inequality.
173 /// <param name="left">The first instance.</param>
174 /// <param name="right">The second instance.</param>
175 /// <returns>True, if left does not equal right; false otherwise.</returns>
176 public static bool operator !=(DisplayResolution left, DisplayResolution right)
178 return !(left == right);