Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #33: How to Scale your entire App and its Elements to your Browsers Size

Let’s say you have a Silverlight application that you want to be scaled to the same width and height of your browser window. This way the application takes up the entire window and not just a fixed sized within the window. To illustrate this I have created a sample application that has a number of random UI elements on it.

You can preview and run this application here: http://silverlight.services.live.com/invoke/66033/Page%20Scaling/iframe.html

Also, the following screen shots show this sample application scaled to different sizes in the browser (tall, normal, wide). As you can see, each UI element in the application is scaled proportionally to the browser size.

 image imageimage

In order to accomplish this, all you have to do is add a RenderTransform of the type ScaleTransform to your Grid or Canvas of your Silverlight control.

For example, add the following code to your Page.xaml file:

<Canvas>
    <Canvas.RenderTransform>
        <ScaleTransform x:Name="CanvasScale" ScaleX=”1” ScaleY=”1”></ScaleTransform>
    </Canvas.RenderTransform>
</Canvas>

Setting ScaleX and ScaleY to “1” is equivelent to a 100% scale. If you set the ScaleX and ScaleY to “0.33” the control would render at 1/3 its original.

Now, as demonstrated in Tip of the Day #9 monitor for your browser resize in your Page.xaml.cs file. Set the CanvasScale ScaleX and ScaleY to be a new percentage of the original width and height:

namespace ScaleTransform
{
    public partial class Page : UserControl
    {
        private int _startingWidth = 800;
        private int _startingHeight = 600;
 
        public Page()
        {
            InitializeComponent();
 
            App.Current.Host.Content.Resized += new EventHandler(Content_Resized);
        }
        void Content_Resized(object sender, EventArgs e)
        {
            double height = App.Current.Host.Content.ActualHeight;   
            double width = App.Current.Host.Content.ActualWidth;
 
            CanvasScale.ScaleX = width / _startingWidth;
            CanvasScale.ScaleY = height / _startingHeight;
        }
       
    }
}

 

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Let’s say you have a Silverlight application that you want to be scaled to the same width and height

# August 27, 2008 1:55 AM

esh said:

What about scrollbars? How do you handle them? Will they appear on resize?

Thanks

# August 27, 2008 2:34 AM

Silverlight news for August 27, 2008 said:

Pingback from  Silverlight news for August 27, 2008

# August 27, 2008 3:35 AM

billvo said:

This example works well with a fairly large range of browser window sizes.  I found that it fails to fill the client area of the browser by sizing an IE7 window to 770x811.  I got the same result with FireFox 3 by with a slightly taller window, matching up the HTML display spaces.  In both cases the bottom edge of the "Don't Click Me" button is cut off and white space fills in above the status bar.  At 1143x230, the right side is cut off.

# August 27, 2008 8:40 AM

Dew Drop - August 27, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - August 27, 2008 | Alvin Ashcraft's Morning Dew

# August 27, 2008 8:50 AM

mike.snow said:

Hey Billvo, I am seeing the same thing and will file a bug for it. Thanks!

Esh- Scrollbars should also scale, everything should.

# August 27, 2008 10:23 AM

samcov said:

In any real implementation, you would probably want to maintain the proper aspect ratio.

# August 27, 2008 1:07 PM

mike.snow said:

True, this is probably a corner case.

# August 27, 2008 1:12 PM

Community Blogs said:

Michael Washington with Desktop Instances in SilverlightDesktop, and Mike Snow scaling his entire app

# August 27, 2008 7:07 PM

Silverlight 2.0 RTM? Silverlight vNext « Tales from a Trading Desk said:

Pingback from  Silverlight 2.0 RTM? Silverlight vNext &laquo; Tales from a Trading Desk

# August 28, 2008 4:42 PM

Visual Web Developer Team Blog said:

&#160; Silverlight Tip of the&#160; Day #39 Title : How to Create a Zoom Toolbar Demo: silverlight.services.live.com/.../iframe.html

# September 11, 2008 1:21 AM