Getting browser screen & window information in a Silverlight 2 app

24 June 2008

While working on visual verification and user interface testing support for some Silverlight tests, I came up with this simple file that abstracts out the Firefox/Safari and Internet Explorer property differences.  The browsers all expose this information in one way or another.

Although mostly integer values, numbers are returned as doubles to stay in line with how the HTML DOM bridge APIs work.

The properties exposed off of the static class are:

BrowserScreenInformation
ClientWidth, ClientHeight Current window's inner size
ScrollLeft, ScrollTop Window's scrolled portion relative to the page
ScreenWidth, ScreenHeight Screen resolution
AvailableScreenWidth, AvailableScreenHeight Real estate of the screen excluding the task bar or dock
ScreenPositionLeft, ScreenPositionTop Location, in pixels, of the window's origin on the screen

Maybe someone will find this useful for their project!

using System;
using System.Windows;
using System.Windows.Browser;

namespace YourNamespaceHere
{
    /// <summary>
    /// Provides screen information about the browser
    /// </summary>
    /// <remarks>A simple proxy to JavaScript window and screen variables, 
    /// abstracts away common web browser differences</remarks>
    public static class BrowserScreenInformation
    {

        /// <summary>
        /// During static instantiation, only the Netscape flag is checked
        /// </summary>
        static BrowserScreenInformation()
        {
            _isNavigator = HtmlPage.BrowserInformation.Name.Contains("Netscape");
        }

        /// <summary>
        /// Flag indicating Navigator/Firefox/Safari or Internet Explorer
        /// </summary>
        private static bool _isNavigator;

        /// <summary>
        /// Provides quick access to the window.screen ScriptObject
        /// </summary>
        private static ScriptObject Screen
        {
            get
            {
                ScriptObject screen = (ScriptObject)HtmlPage.Window.GetProperty("screen");
                
                if (screen == null)
                {
                    throw new InvalidOperationException();
                }

                return screen;
            }
        }

        /// <summary>
        /// Gets the window object's client width
        /// </summary>
        public static double ClientWidth
        {
            get
            {
                return _isNavigator ? (double)HtmlPage.Window.GetProperty("innerWidth")
                    : (double)HtmlPage.Document.Body.GetProperty("clientWidth");
            }

        }

        /// <summary>
        /// Gets the window object's client height
        /// </summary>
        public static double ClientHeight
        {
            get
            {
                return _isNavigator ? (double)HtmlPage.Window.GetProperty("innerHeight")
                    : (double)HtmlPage.Document.Body.GetProperty("clientHeight");
            }
        }

        /// <summary>
        /// Gets the current horizontal scrolling offset
        /// </summary>
        public static double ScrollLeft
        {
            get
            {
                return _isNavigator ? (double)HtmlPage.Window.GetProperty("pageXOffset")
                    : (double)HtmlPage.Document.Body.GetProperty("scrollLeft");
            }
        }

        /// <summary>
        /// Gets the current vertical scrolling offset
        /// </summary>
        public static double ScrollTop
        {
            get
            {
                return _isNavigator ? (double)HtmlPage.Window.GetProperty("pageYOffset")
                    : (double)HtmlPage.Document.Body.GetProperty("scrollHeight");
            }
        }

        /// <summary>
        /// Gets the width of the entire display
        /// </summary>
        public static double ScreenWidth
        {
            get
            {
                return (double)Screen.GetProperty("width");
            }
        }

        /// <summary>
        /// Gets the height of the entire display
        /// </summary>
        public static double ScreenHeight
        {
            get
            {
                return (double)Screen.GetProperty("height");
            }
        }

        /// <summary>
        /// Gets the width of the available screen real estate, excluding the dock 
        /// or task bar
        /// </summary>
        public static double AvailableScreenWidth
        {
            get
            {
                return (double)Screen.GetProperty("availWidth");
            }
        }

        /// <summary>
        /// Gets the height of the available screen real estate, excluding the dock 
        /// or task bar
        /// </summary>
        public static double AvailableScreenHeight
        {
            get
            {
                return (double)Screen.GetProperty("availHeight");
            }
        }

        /// <summary>
        /// Gets the absolute left pixel position of the window in display coordinates
        /// </summary>
        public static double ScreenPositionLeft
        {
            get
            {
                return _isNavigator ? (double)HtmlPage.Window.GetProperty("screenX")
                    : (double)HtmlPage.Window.GetProperty("screenLeft");
            }
        }

        /// <summary>
        /// Gets the absolute top pixel position of the window in display coordinates
        /// </summary>
        public static double ScreenPositionTop
        {
            get
            {
                return _isNavigator ? (double)HtmlPage.Window.GetProperty("screenY")
                    : (double)HtmlPage.Window.GetProperty("screenTop");
            }
        }

    }
}

Jeff Wilcox is a Software Engineer at Microsoft in the Open Source Programs Office (OSPO), helping Microsoft engineers use, contribute to and release open source at scale.

comments powered by Disqus