1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok (pbartok@novell.com)
29 // NOTE: We made a concious decision to have only a single 'screen'
30 // due to the differences in platforms. On Win32 we could gather
31 // all information, but not for X11 (and possibly Mac). So for now
32 // we'll stick with a single screen, but the functions are still
33 // written to support multiple screens, simply beef up the all_screens
34 // assignment to get multiples
35 // To support multiples, we need to use GetMonitorInfo API on Win32
40 namespace System.Windows.Forms {
42 #region Local Variables
43 private static Screen[] all_screens = { new Screen(true, "Mono MWF Primary Display", SystemInformation.VirtualScreen, SystemInformation.WorkingArea) };
45 private Rectangle bounds;
46 private Rectangle workarea;
49 #endregion // Local Variables
54 this.bounds = SystemInformation.WorkingArea;
57 private Screen(bool primary, string name, Rectangle bounds, Rectangle workarea) {
58 this.primary = primary;
61 this.workarea = workarea;
63 #endregion // Constructors
65 #region Public Static Properties
66 public static Screen[] AllScreens {
72 public static Screen PrimaryScreen {
74 return all_screens[0];
77 #endregion // Public Static Properties
79 #region Public Instance Properties
80 public Rectangle Bounds {
86 public string DeviceName {
98 public Rectangle WorkingArea {
100 return this.workarea;
103 #endregion // Public Instance Properties
105 #region Public Static Methods
106 public static Screen FromControl(Control control) {
107 return Screen.FromPoint(control.Location);
110 public static Screen FromHandle(IntPtr hwnd) {
113 control = Control.FromHandle(hwnd);
114 if (control != null) {
115 return Screen.FromPoint(control.Location);
117 return Screen.PrimaryScreen;
120 public static Screen FromPoint(Point point) {
121 for (int i = 0; i < all_screens.Length; i++) {
122 if (all_screens[i].Bounds.Contains(point)) {
123 return all_screens[i];
126 return Screen.PrimaryScreen;
129 public static Screen FromRectangle(Rectangle rect) {
130 return Screen.FromPoint(new Point(rect.Left, rect.Top));
133 public static Rectangle GetBounds(Control ctl) {
134 return Screen.FromControl(ctl).Bounds;
137 public static Rectangle GetBounds(Point pt) {
138 return Screen.FromPoint(pt).Bounds;
141 public static Rectangle GetBounds(Rectangle rect) {
142 return Screen.FromRectangle(rect).Bounds;
145 public static Rectangle GetWorkingArea(Control ctl) {
146 return Screen.FromControl(ctl).WorkingArea;
149 public static Rectangle GetWorkingArea(Point pt) {
150 return Screen.FromPoint(pt).WorkingArea;
153 public static Rectangle GetWorkingArea(Rectangle rect) {
154 return Screen.FromRectangle(rect).WorkingArea;
156 #endregion // Public Static Methods
158 #region Public Instance Methods
159 public override bool Equals(object obj) {
161 Screen s = (Screen)obj;
163 if (name.Equals(s.name) && (primary == s.primary) && (bounds.Equals(s.Bounds)) && (workarea.Equals(s.workarea))) {
170 public override int GetHashCode() {
171 return base.GetHashCode ();
174 public override string ToString() {
175 return "Screen[Bounds={" + this.Bounds + "} WorkingArea={" + this.WorkingArea + "} Primary={" + this.Primary + "} DeviceName=" + this.DeviceName;
179 #endregion // Public Instance Methods