Customizing the Look and Feel or your Windows Forms Applications

27. April 2012 17:05 by Mrojas in Controls, VB6 Migration, WinForms  //  Tags: , , , , , ,   //   Comments (0)

Windows forms is still a great technology, but by default is not as flexible as XAML or HTML where you can very easily modify the style of your controls.

But that is not entirely true.

Let's review which options do we have for customizing the look and feel of Windows Forms Applications.

 

Custom Look And Feel

Well the idea is simple. Centralize all the settings for your components and make sure to invoke that logic just after the initializeComponent() method call on the Windows Form constructor.

I will implement a very simple example:

First the class the implements the "theming logic":

using System.Windows.Forms;
using System.Drawing;
public class ApplicationLookAndFeel
{
	static void ApplyTheme(TextBox c)
	{
		c.Font = new Font("Arial",12.0f); c.BackColor=Color.Blue; c.ForeColor = Color.White;
	}
	static void ApplyTheme(Label c)
	{
		c.Font = new Font("Arial", 12.0f); c.BackColor = Color.Black; c.ForeColor = Color.White;
	}
	static void ApplyTheme(Form c)
	{
		c.Font = new Font("Arial", 12.0f); c.BackColor = Color.Black; c.ForeColor = Color.White; 
	}
	
	public static void UseTheme(Form form)
	{
		ApplyTheme(form);
		foreach (var c in form.Controls)
		{
			switch(c.GetType().ToString())
			{
				case "System.Windows.Forms.TextBox":
					ApplyTheme((TextBox)c);
					break;
				case "System.Windows.Forms.Label":
					ApplyTheme((Label)c);
					break;


			}
		}
	}
}
As you see is just very simple code that will just update the settings for all controls, and a simple call in all form constructors and your done:
public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
			ApplicationLookAndFeel.UseTheme(this);
		}
	}
A form like:
 

 

This approach is simple, but could require a lot of work if you have a lot of components, and switching to another look and feel will require a lot of changes, and it does not allow you to customize all of the form.

Customization of non client are (title bar, borders, requires a little more work). You can take a look at Szymon Kobalczyk work in codeplex. There is a lot of information about a setting custom borders for your forms and even a start of a form styling library. 

 

Styling Controls

There are some third-parties that provide some level of Theming/Styling. The main difference is the impact they have on existing applications. I categorize them a Little Changes/Big Changes

 

Little Changes

Visual Styler.NET from http://www.skin-soft.co.uk/

This is an interesting solution, because it allows you to style your Standard Windows Form controls, with very little changes. You just add a control on you main form and that all.  They provide some custom styles and you can built your own. They support styling of ThirdParty Controls but I am not sure how that will work.

 

 

For screen shots and details see: http://www.skin-soft.co.uk/Products/VisualStyler/Overview.aspx

 

More Changes

Telerik Visual Style Builder http://www.telerik.com/products/winforms/visual-style-builder.aspx

 

 

 

DevExpress WinForms Skins http://www.devexpress.com/Products/NET/Controls/WinForms/Skins/

 

 

Infragistics Application Styling http://www.infragistics.com/resources/application-styling.aspx#ApplicationStyling

 

 

I call this "more changes" because all of them are great styling components but in order to style your application you have to use THEIR components. That is change all your TextBox for RadTextBox in the case of Telerik, Button for RadButton, or change your Label for WinLabel and your Button for WinButton for Infragistics and TextEdit for DevExpress...

So it can be a lot of changes. The end result can be stunning, because all of these companies have very very good components, but it is a lot of changes and can affect your application.

 

 But as you can see, Windows Forms still has a lot to offer