[Silverlight]取得螢幕解析度(Getting browser screen & window information)

今天有人問我這樣的問題:

"Silverlight 可以依照Client端螢幕解析度大小,來自動調整Silverlight顯示大小嗎?"

那我第一個想法就是 : "OF COURSE!~"

今天有人問我這樣的問題:

"Silverlight 可以依照Client端螢幕解析度大小,來自動調整Silverlight顯示大小嗎?"

那我第一個想法就是 : "OF COURSE!~"

ex:

如果原本自己設計的 Silverlight 版型是 1024x768 的規格,也就是 LayoutRoot 的大小

如果改成 800x600 的解析度, 那你再Broswer 右邊就會出現討厭的捲軸

 

那我的想法,要做這個功能的話:

1) 先取得Client Broswer的解析度(重點)

2) 再用程式碼計算如何"縮放"你SL的元件(預設:LayoutRoot , 元件=Grid)

001         public static class BrowserScreenInformation
002         {
003
004             /// <summary>    
005             /// During static instantiation, only the Netscape flag is checked    
006             /// </summary>    
007             static BrowserScreenInformation()
008             {
009                 _isNavigator = HtmlPage.BrowserInformation.Name.Contains("Netscape");
010             }

011
012             /// <summary>    
013             /// Flag indicating Navigator/Firefox/Safari or Internet Explorer    
014             /// </summary>    

015             private static bool _isNavigator;
016
017             /// <summary>    
018             /// Provides quick access to the window.screen ScriptObject    
019             /// </summary>    

020             private static ScriptObject Screen
021             {
022                 get
023                 {
024                     ScriptObject screen = (ScriptObject)HtmlPage.Window.GetProperty("screen");
025
026                     if (screen == null)
027                     {
028                         throw new InvalidOperationException();
029                     }

030
031                     return screen;
032                 }

033             }

034
035             /// <summary>    
036             /// Gets the window object's client width    
037             /// </summary>    

038             public static double ClientWidth
039             {
040                 get
041                 {
042                     return _isNavigator ? (double)HtmlPage.Window.GetProperty("innerWidth")
043                         : (double)HtmlPage.Document.Body.GetProperty("clientWidth");
044                 }

045
046             }

047
048             /// <summary>    
049             /// Gets the window object's client height    
050             /// </summary>    

051             public static double ClientHeight
052             {
053                 get
054                 {
055                     return _isNavigator ? (double)HtmlPage.Window.GetProperty("innerHeight")
056                         : (double)HtmlPage.Document.Body.GetProperty("clientHeight");
057                 }

058             }

059
060             /// <summary>    
061             /// Gets the current horizontal scrolling offset    
062             /// </summary>    

063             public static double ScrollLeft
064             {
065                 get
066                 {
067                     return _isNavigator ? (double)HtmlPage.Window.GetProperty("pageXOffset")
068                         : (double)HtmlPage.Document.Body.GetProperty("scrollLeft");
069                 }

070             }

071
072             /// <summary>    
073             /// Gets the current vertical scrolling offset    
074             /// </summary>    

075             public static double ScrollTop
076             {
077                 get
078                 {
079                     return _isNavigator ? (double)HtmlPage.Window.GetProperty("pageYOffset")
080                         : (double)HtmlPage.Document.Body.GetProperty("scrollHeight");
081                 }

082             }

083
084             /// <summary>    
085             /// Gets the width of the entire display    
086             /// </summary>    

087             public static double ScreenWidth
088             {
089                 get
090                 {
091                     return (double)Screen.GetProperty("width");
092                 }

093             }

094
095             /// <summary>    
096             /// Gets the height of the entire display    
097             /// </summary>    

098             public static double ScreenHeight
099             {
100                 get
101                 {
102                     return (double)Screen.GetProperty("height");
103                 }

104             }

105
106             /// <summary>    
107             /// Gets the width of the available screen real estate, excluding the dock    
108             /// or task bar    
109             /// </summary>    

110             public static double AvailableScreenWidth
111             {
112                 get
113                 {
114                     return (double)Screen.GetProperty("availWidth");
115                 }

116             }

117
118             /// <summary>    
119             /// Gets the height of the available screen real estate, excluding the dock    
120             /// or task bar    
121             /// </summary>    

122             public static double AvailableScreenHeight
123             {
124                 get
125                 {
126                     return (double)Screen.GetProperty("availHeight");
127                 }

128             }

129
130             /// <summary>    
131             /// Gets the absolute left pixel position of the window in display coordinates    
132             /// </summary>    

133             public static double ScreenPositionLeft
134             {
135                 get
136                 {
137                     return _isNavigator ? (double)HtmlPage.Window.GetProperty("screenX")
138                         : (double)HtmlPage.Window.GetProperty("screenLeft");
139                 }

140             }

141
142             /// <summary>    
143             /// Gets the absolute top pixel position of the window in display coordinates    
144             /// </summary>    

145             public static double ScreenPositionTop
146             {
147                 get
148                 {
149                     return _isNavigator ? (double)HtmlPage.Window.GetProperty("screenY")
150                         : (double)HtmlPage.Window.GetProperty("screenTop");
151                 }

152             }

153
154         }
  

 

就這樣~將這個類別放在你的程式碼之中即可~(已測試OK)

 

參考網址: http://www.jeff.wilcox.name/2008/06/browserscreeninformation/