|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis article explains how to disable windows themes in a .NET application to ensure uniform appearance across all versions of Windows. BackgroundA few months ago it has come to my attention that windows will paint forms and controls differently depending on the current Windows Theme that is active on the users PC. This does not end at a different window style or status bar, but actual colors paint differently. For example Color.Silver will look much lighter in Windows XP theme then it would in Windows Classic. Under Windows Vista Color.Silver is almost white. Some colors disappear completely under So began my search into a way of turning off windows themes in a .NET applications to allow for a more uniform look regardless of the developers and user’s PC. Using the codeIn order to disable Windows Themes you actually need to reference a Windows API DLL uxtheme.dll and set the theme to nothing. I could not find an easier way to do this. There does not seem to by any property exposed on the application or form level that allows you to disable themes. In order to make this process easier and to avoid pasting the same code in all of the forms in my application, I created a base form that contains the necessary code. using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace DisableWindowsThemesExample
{
public class BaseForm : Form
{
[DllImport("uxtheme", ExactSpelling = true, CharSet = CharSet.Unicode)]
public extern static Int32 SetWindowTheme (IntPtr hWnd, String textSubAppName, String textSubIdList);
public BaseForm()
{
SetWindowTheme(Handle, "", "");
Invalidate();
}
}
}
Then you simply inherit your form from the base form and call the base constructor. (The call to the base constructor is actually not necessary since it is implicit) public partial class ExampleForm : BaseForm
{
public ExampleForm(): base()
{
Furthermore in order to disable themes for your controls you have to set the rdoNonThemed.FlatStyle = FlatStyle.System; grpNonThemed.FlatStyle = FlatStyle.System; btnNonThemed.FlatStyle = FlatStyle.System; SetWindowTheme(grpNonThemed.Handle, "", ""); SetWindowTheme(rdoNonThemed.Handle, "", ""); SetWindowTheme(btnNonThemed.Handle, "", ""); Download the solution above to see a working example. History2008-May-28 Initial Version
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||