5,659,906 members and growing! (18,673 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General License: The Code Project Open License (CPOL)

How to Disable Windows Themes using uxtheme.dll and SetWindowTheme

By Adam Berent

How to Disable Windows Themes to ensure uniform application appearance acrross all versions of Windows
C# (C# 1.0, C# 2.0, C# 3.0, C#), Windows (Windows, NT4, Win2K, WinXP, Win2003, Vista), WinForms

Posted: 29 May 2008
Updated: 17 Jul 2008
Views: 5,660
Bookmarked: 5 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.27 Rating: 2.92 out of 5
2 votes, 33.3%
1
0 votes, 0.0%
2
1 vote, 16.7%
3
2 votes, 33.3%
4
1 vote, 16.7%
5
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

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 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 Vista and are painted as white.

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

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 FlatSyle of the control to System and SetWindowTheme for that control.

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
2008-May-28 Fixed Download Link

License

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

About the Author

Adam Berent


www.adamberent.com
Location: Canada Canada

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralRule of ThumbmemberThe_Mega_ZZTer19:21 29 May '08  
QuestionRe: Rule of ThumbmemberAdam Berent4:28 30 May '08  
GeneralFixed Download Link.memberAdam Berent8:38 29 May '08  
GeneralDownload link brokenmember leppie 8:27 29 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Jul 2008
Editor:
Copyright 2008 by Adam Berent
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project