1 using System; 2 using System.Net; 3 using System.Windows; 4 using System.Windows.Browser; 5 using System.Windows.Controls; 6 using System.Windows.Documents; 7 using System.Windows.Ink; 8 using System.Windows.Input; 9 using System.Windows.Media; 10 using System.Windows.Media.Animation; 11 using System.Windows.Shapes; 12 13 namespace TWSL.Common 14 { 15 /// <summary> 16 /// 浏览器屏幕信息类 17 /// </summary> 18 public class Browser 19 { 20 /// <summary> 21 /// During static instantiation, only the Netscape flag is checked 22 /// </summary> 23 static Browser() 24 { 25 _isNavigator = HtmlPage.BrowserInformation.Name.Contains("Netscape"); 26 } 27 28 /// <summary> 29 /// Flag indicating Navigator/Firefox/Safari or Internet Explorer 30 /// </summary> 31 private static bool _isNavigator; 32 33 /// <summary> 34 /// Provides quick access to the window.screen ScriptObject 35 /// </summary> 36 private static ScriptObject Screen 37 { 38 get 39 { 40 ScriptObject screen = (ScriptObject)HtmlPage.Window.GetProperty("screen"); 41 42 if (screen == null) 43 { 44 throw new InvalidOperationException(); 45 } 46 47 return screen; 48 } 49 } 50 51 /// <summary> 52 /// Gets the window object's client width 53 /// </summary> 54 public static double ClientWidth 55 { 56 get 57 { 58 return _isNavigator ? (double)HtmlPage.Window.GetProperty("innerWidth") 59 : (double)HtmlPage.Document.Body.GetProperty("clientWidth"); 60 } 61 62 } 63 64 /// <summary> 65 /// Gets the window object's client height 66 /// </summary> 67 public static double ClientHeight 68 { 69 get 70 { 71 return _isNavigator ? (double)HtmlPage.Window.GetProperty("innerHeight") 72 : (double)HtmlPage.Document.Body.GetProperty("clientHeight"); 73 } 74 } 75 76 /// <summary> 77 /// Gets the current horizontal scrolling offset 78 /// </summary> 79 public static double ScrollLeft 80 { 81 get 82 { 83 return _isNavigator ? (double)HtmlPage.Window.GetProperty("pageXOffset") 84 : (double)HtmlPage.Document.Body.GetProperty("scrollLeft"); 85 } 86 } 87 88 /// <summary> 89 /// Gets the current vertical scrolling offset 90 /// </summary> 91 public static double ScrollTop 92 { 93 get 94 { 95 return _isNavigator ? (double)HtmlPage.Window.GetProperty("pageYOffset") 96 : (double)HtmlPage.Document.Body.GetProperty("scrollHeight"); 97 } 98 } 99 100 /// <summary> 101 /// Gets the width of the entire display 102 /// </summary> 103 public static double ScreenWidth 104 { 105 get 106 { 107 return (double)Screen.GetProperty("width"); 108 } 109 } 110 111 /// <summary> 112 /// Gets the height of the entire display 113 /// </summary> 114 public static double ScreenHeight 115 { 116 get 117 { 118 return (double)Screen.GetProperty("height"); 119 } 120 } 121 122 /// <summary> 123 /// Gets the width of the available screen real estate, excluding the dock 124 /// or task bar 125 /// </summary> 126 public static double AvailableScreenWidth 127 { 128 get 129 { 130 return (double)Screen.GetProperty("availWidth"); 131 } 132 } 133 134 /// <summary> 135 /// Gets the height of the available screen real estate, excluding the dock 136 /// or task bar 137 /// </summary> 138 public static double AvailableScreenHeight 139 { 140 get 141 { 142 return (double)Screen.GetProperty("availHeight"); 143 } 144 } 145 146 /// <summary> 147 /// Gets the absolute left pixel position of the window in display coordinates 148 /// </summary> 149 public static double ScreenPositionLeft 150 { 151 get 152 { 153 return _isNavigator ? (double)HtmlPage.Window.GetProperty("screenX") 154 : (double)HtmlPage.Window.GetProperty("screenLeft"); 155 } 156 } 157 158 /// <summary> 159 /// Gets the absolute top pixel position of the window in display coordinates 160 /// </summary> 161 public static double ScreenPositionTop 162 { 163 get 164 { 165 return _isNavigator ? (double)HtmlPage.Window.GetProperty("screenY") 166 : (double)HtmlPage.Window.GetProperty("screenTop"); 167 } 168 } 169 170 } 171 }