Click here to Skip to main content
15,918,243 members
Articles / Programming Languages / C#
Article

Detecting XP Themes

Rate me:
Please Sign up or sign in to vote.
3.50/5 (6 votes)
18 Mar 20042 min read 87.6K   26   10
How To Detect Theme Settings in Windows XP

Sample image

Introduction

Support for XP Themes is known to be somewhat haphazard - the Explorer bypasses the theme DLL, UxTheme, for example. Microsoft refuses to open themes to developers for fear rampant new themes will cause "application incompatibilities."

I choose not to mess with UxTheme and the like, but I would like my applications to be aware when themes are in use. Because there are only four, at least right now, it is feasible to write code that reacts to the user's choice of theme. The user can choose "Windows Classic" or "Windows XP." If they choose the latter, they can select Blue (the default), Olive Green, or Silver color schemes.

[Added]

Thanks to Konrad Windszus and Rene Koenig, who point out that peeking in the registry is brittle and likely to break in future versions. That's an important disclaimer, along with this: Do not modify the registry hoping to change the theme.

I revised the code to illustrate Konrad's suggestion of calling UxTheme's IsThemeActive() method and Rene's IsAppThemed(). I still can't find an easy way to get UxTheme to tell me whether the user has chosen Blue, Green, or Silver.

Use the Registry

The answer is found in the registry, but it's not obvious. To save you experimenting, I'll just tell you:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ThemeManager

ThemeActive is "1" if Windows XP, "0" if Windows Classic.

If ThemeActive is "1", ColorName will be "NormalColor" for blue, "HomeStead" for olive green, or "Metallic" for silver. (All of these data values are type REG_SZ, or strings, by the way.)

The following C# code illustrates reading the settings. Other languages are quite similar.

C#
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
C#
class Class1 { 
static void Main()

{
    Console.WriteLine("CurrentTheme returns {0}", CurrentTheme());
    Console.WriteLine("UxTheme::IsThemeActive returns {0}", IsThemeActive());
    Console.WriteLine("UxTheme::IsAppThemed returns {0}", IsAppThemed());
    Console.Read();

}
C#
public enum Theme

{
    WindowsClassic,
    XPBlue,
    XPGreen,
    XPSilver

}
C#
public static Theme CurrentTheme()

{
    RegistryKey key = 
        Registry.CurrentUser.OpenSubKey(
            @"Software\Microsoft\Windows\CurrentVersion\ThemeManager");
    if (key != null) 
    {
        if ("1" == (string) key.GetValue("ThemeActive"))
        {
            string s = (string) key.GetValue("ColorName");
            if (s != null)
            {
                if (String.Compare(s, "NormalColor", true) == 0)
                    return Theme.XPBlue;
                if (String.Compare(s, "HomeStead", true) == 0)
                    return Theme.XPGreen;
                if (String.Compare(s, "Metallic", true) == 0)
                    return Theme.XPSilver;
            }
        } 
    }
    return Theme.WindowsClassic;

}
C#
[DllImport("UxTheme")]

static extern bool IsThemeActive();
C#
[DllImport("UxTheme")]

static extern bool IsAppThemed();
}    // end Class1

References

This article was inspired by an anonymous CodeProject article on XP-style buttons: http://www.codeproject.com/cs/miscctrl/JPEnhancedXPButton.asp, based on the work of someone named Joaqs. The anonymous article adds a "BtnStyle" property for the programmer to specify Blue, Silver, or Olive, but offers no hints how to detect the current state.

History

  • 03/21/04 ad Revised
  • 03/19/04 ad Initial

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Alastair Dallas is a programmer, systems architect, and information designer with over 20 years experience at companies including Ashton-Tate, Lotus, Netscape, Documentum, and a wide variety of startups. Languages include C#, C, C++, Java, JavaScript, Perl, Python, and VB (in alphabetical order) as well as DHTML, XML, and related technologies. He has written 3 books, 2 on computer topics, and is President of Infospect Press.

Comments and Discussions

 
GeneralWM_THEMECHANGED Notification Pin
dmex14-Mar-10 12:38
dmex14-Mar-10 12:38 
QuestionThe managed way? Pin
Jan Kučera9-Feb-06 5:10
Jan Kučera9-Feb-06 5:10 
AnswerRe: The managed way? Pin
Ash230-Mar-06 6:22
Ash230-Mar-06 6:22 
GeneralProblem with some visual components Pin
Carlos Eugênio X. Torres16-Dec-05 8:05
Carlos Eugênio X. Torres16-Dec-05 8:05 
I'm using Application.EnableVisualStyles(); on my application and it really works just as is.
But some components still not appearing in XP theme, like tabPanels. The tabPage child object appears always with the normal Control back color (gray color as default).

So, using the code you posted

[DllImport("UxTheme")]
static extern bool IsThemeActive();
[DllImport("UxTheme")]
static extern bool IsAppThemed();

And at form1_Load I do a little trick to try to cover the problem:

private void form1_Load(object sender, System.EventArgs e)
{
if (IsThemeActive() && IsAppThemed())
{
tabPage1.BackColor = System.Drawing.SystemColors.ControlLightLight;
}

...
}

That color is very close to the color of tabPages on XP themes. So, when the app detects if is theme active, it changes the tabPage back color. It looks very nice. Smile | :)

If someone has a better way to solve it, tell me, please!

Other components that don't look like XP theme are DomainUpDown and NumericUpDown.
If someone knows how to force those ones to appear well, please tell me too! Smile | :)

Thanks.

Carlos Eugênio Torres
Software Analyst and Developer
Microsoft Certified Application Developer (MCAD.NET)
email: carloseugeniotorres@gmail.com
Generalan easier way with .net 1.1 Pin
benoxoft28-Apr-05 12:51
benoxoft28-Apr-05 12:51 
GeneralEasier way via IsThemeActive() Pin
Konrad Windszus20-Mar-04 1:05
Konrad Windszus20-Mar-04 1:05 
GeneralRe: Easier way via IsThemeActive() Pin
Rene Koenig20-Mar-04 2:45
Rene Koenig20-Mar-04 2:45 
GeneralRe: Easier way via IsThemeActive() Pin
Konrad Windszus21-Mar-04 2:58
Konrad Windszus21-Mar-04 2:58 
GeneralRe: Easier way via IsThemeActive() Pin
David Pritchard21-May-04 9:23
David Pritchard21-May-04 9:23 
GeneralInterresting! Pin
Carl Mercier19-Mar-04 15:44
Carl Mercier19-Mar-04 15:44 

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.