Archive for October 2008

Scalable Windows Forms

Consider an application that should be able to adapt its font and form size to various needs: for users with weak eyesight, for viewing from different distances etc. You might think you need WPF to do that, but in fact also plain old Windows Forms has got mechanisms that automatically scale fonts and form layouts to different screen resolutions and font sizes. With a minimal amount of code you will be able to utilize this mechanism in your own application to make a fully scalable UI.

Scaling Support in Windows Forms

Today a Windows application might have to run in environments with several screen resolutions and system font sizes. As a programmer, what should you do to ensure your application would keep its layout and be consistent with the rest of the environment whatever the screen resolution might be? Well, you don’t have to do anything! Just leave your form’s AutoScaleMode property to Font for text-based forms (with labels, text boxes and buttons) or set it to Dpi for graphics-based forms, and Windows and the .NET Framework will scale your UI to fit the current environment. You can test your application by running it in different DPI settings. To change the DPI setting of your system, open Control Panel -> Display -> Settings -> Advanced -> General in XP or Control Panel -> Personalization -> Adjust font size (DPI) in Vista.

Scaling Your Own Application

Changing the DPI setting requires restarting the system so it isn’t very handy if your user just temporarily wants to see the application in a magnified or reduced view. This is why you might consider having individual UI scaling in your application. Thanks to the automatic scaling support in Windows Forms it will be quite easy and straightforward.

When you change the Font property of a Form (programmatically or in Designer) to a larger or smaller font, form dimensions and control layout will scale automatically provided that the AutoScaleMode property is set to Font. If you try this, you will notice that controls on this form might also have their font size changed. The problem is that the general font change only applies to those controls whose Font property has not been set explicitly. These controls assume the font of their parents (Form, GroupBox, Panel etc.), and automatic font change works only as far as this chain from form to controls is not broken.

To spread the relative font change to all controls you just need to add a few lines of code that goes through all controls on the form and changes the font of those who don’t share a font with their parent. This can be done by overriding the OnFontChanged method or implementing a FontChanged event handler. And that’s it!

The Sample Application

A tiny Visual Studio 2005 project with a launcher/control form and two scalable forms can be found in http://www.sysgen.fi/source/scalableforms. For your convenience it is available both as a ZIP archive and as separate files. The first scalable form isn’t fully scalable since the label “This form has got nothing special in it” does not scale because it does not share the font with the form. The second one scales nicely thanks to the OnFontChanged override.

When running the application, you may open any number of the two forms and see the effect of dragging the track bar slider. Please enjoy!