Click here to Skip to main content
16,004,991 members
Articles / Desktop Programming / Windows Forms
Article

How to Disable Windows Themes Using uxtheme.dll and SetWindowTheme

Rate me:
Please Sign up or sign in to vote.
3.45/5 (6 votes)
17 Jul 2008CPOL2 min read 67.7K   1.1K   17   4
How to disable Windows themes to ensure uniform application appearance acrross all versions of Windows.

DisableWindowsThemesExample

Introduction

This article explains how to disable Windows themes in a .NET application to ensure uniform appearance across all versions of Windows.

Background

A 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 user's PC.

This does not end at a different window style or status bar, but actual colors painted differently. For example, Color.Silver will look much lighter in the Windows XP theme than it would in Windows Classic. Under Windows Vista, Color.Silver is almost white. Some colors disappear completely under Vista, and are painted as white.

So began my search for a way of turning off Windows themes in a .NET applications to allow for a more uniform look regardless of the developer's and the user’s PCs.

Using the code

In 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.

C#
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.)

C#
public partial class ExampleForm : BaseForm
{
    public ExampleForm(): base()
    {

Furthermore, in order to disable themes for your controls, you have to set the FlatSyle of the control to System and call SetWindowTheme for that control.

C#
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.

History

  • 2008-May-28 - Initial version, fixed download link.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada

Comments and Discussions

 
GeneralRule of Thumb PinPopular
The_Mega_ZZTer29-May-08 18:21
The_Mega_ZZTer29-May-08 18:21 
The reason Color.Silver was looking different across different OSs is NOT because of themes, it is because of the user's color preferences. The default change between XP and Vista and depending on the theme the user sets, but even with themes off these settings still apply. Why are these separate from themes?

1) They determine which colors to use when themeing is disabled.
2) They help programs which do not support themes to blend in as best as possible, although they can only do so much.
3) They allow the user finer control, if themeing is disabled, over how the OS looks.

Here is a good third-party app which allows finer end-user tweaking of color preferences than Windows does.

Now, for the reason why your Color.Silver looks different... when the background color changes, the foreground color is going to have less or more contrast. As the background color gets closer to Color.Silver the text is going to be harder and harder to read. This doesn't have much to do with themeing.

A good rule of thumb is to never mix system colors (IE the user-chosen colors) such as the default SystemColors.Window that is applied to every window background, with manually specified colors, such as Color.Silver. Either use both system colors or both manually specified colors. IF you do use system colors try to keep to common combinations (window with windowtext, control with controltext, highlight and hightlighttext) and if you use other combos make sure they are still readable and look nice in luna, aero, and windows classic.

This guy talks more about it, read the comments too.

I would encourage you to also stick with using themes so that your app fits with other running apps across ALL OSs, not just pre-XP.

I am sure this article will be useful for some people in disabling themeing for their app, even if only to test how it looks unthemed without changing their themeing settings.
QuestionRe: Rule of Thumb Pin
Adam Berent30-May-08 3:28
Adam Berent30-May-08 3:28 
GeneralFixed Download Link. Pin
Adam Berent29-May-08 7:38
Adam Berent29-May-08 7:38 
GeneralDownload link broken Pin
leppie29-May-08 7:27
leppie29-May-08 7:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.