Click here to Skip to main content
Click here to Skip to main content
Go to top

How to accurately detect if an application is theme-enabled?

, 1 Jun 2005
Rate this:
Please Sign up or sign in to vote.
Describes a function that overcomes the inadequacies of IsAppThemed and IsThemeActive.

Fig. 1 : System wide and app-specific themes enabled

Fig. 2 : System wide themes enabled, app-specific themes disabled

Fig. 3 : System wide themes disabled. (App-specific doesn't matter)

Overview

When you develop custom controls that work differently depending on whether the application has themes enabled or not, you need to detect whether the currently running application is using themes or not. MSDN lists two functions IsAppThemed and IsThemeActive and I list below snippets from their documentation :-

IsAppThemed Function - Reports whether the current application's user interface displays using visual styles.
IsThemeActive Function - Tests if a visual style for the current application is active.

You'd think that all you need to do is to use one or both of these functions, wouldn't you? Guess what? On XP (even SP2) both these functions return TRUE if your system wide themes setting is enabled. Thus any system that's using an XP styled theme will always have these two functions returning TRUE.

I googled around and didn't find anything worthwhile. I saw a posting from Jeff Partch (MVP) who suggested calling GetModuleHandle on comctl32.dll and comparing this module handle with the handle returned by calling GetClassLongPtr on a known control handle with the GCLP_HMODULE flag. While I didn't actually try that out, I thought it'd be easier to call DllGetVersion on comctl32.dll and check if the version is 6 or greater.

The IsThemed function

The code lavishly uses LoadLibrary/GetProcAddress to avoid a dependency on the PSDK (the code now compiles on the default installation of VC++ 6).

#pragma once

#include "stdafx.h"
#include <Shlwapi.h>

BOOL IsThemed()
{
    BOOL ret = FALSE;
    OSVERSIONINFO ovi = {0};
    ovi.dwOSVersionInfoSize = sizeof ovi;
    GetVersionEx(&ovi);
    if(ovi.dwMajorVersion==5 && ovi.dwMinorVersion==1)
    {
        //Windows XP detected
        typedef BOOL WINAPI ISAPPTHEMED();
        typedef BOOL WINAPI ISTHEMEACTIVE();
        ISAPPTHEMED* pISAPPTHEMED = NULL;
        ISTHEMEACTIVE* pISTHEMEACTIVE = NULL;
        HMODULE hMod = LoadLibrary(_T("uxtheme.dll"));
        if(hMod)
        {
            pISAPPTHEMED = reinterpret_cast<ISAPPTHEMED*>(
                GetProcAddress(hMod,_T("IsAppThemed")));
            pISTHEMEACTIVE = reinterpret_cast<ISTHEMEACTIVE*>(
                GetProcAddress(hMod,_T("IsThemeActive")));
            if(pISAPPTHEMED && pISTHEMEACTIVE)
            {
                if(pISAPPTHEMED() && pISTHEMEACTIVE())                
                {                
                    typedef HRESULT CALLBACK DLLGETVERSION(DLLVERSIONINFO*);
                    DLLGETVERSION* pDLLGETVERSION = NULL;

                    HMODULE hModComCtl = LoadLibrary(_T("comctl32.dll"));
                    if(hModComCtl)
                    {
                        pDLLGETVERSION = reinterpret_cast<DLLGETVERSION*>(
                            GetProcAddress(hModComCtl,_T("DllGetVersion")));
                        if(pDLLGETVERSION)
                        {
                            DLLVERSIONINFO dvi = {0};
                            dvi.cbSize = sizeof dvi;
                            if(pDLLGETVERSION(&dvi) == NOERROR )
                            {
                                ret = dvi.dwMajorVersion >= 6;
                            }
                        }
                        FreeLibrary(hModComCtl);                    
                    }
                }
            }
            FreeLibrary(hMod);
        }
    }    
    return ret;
}

Using the code

if(IsThemed())
    m_bThemed = true;
else
    m_bThemed = false;

History

  • June 02, 2005 - Article first published

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

Share

About the Author

Nish Sivakumar

United States United States
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
 
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com.
 
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
 
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
 
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Comments and Discussions

 
QuestionIs there a reason to check IsAppThemed / IsThemeActive first? Pinmemberpeterchen24-Mar-09 7:11 
GeneralSuggestion: No need to check comctl32.dll version PinmemberSumit Kapoor2-Aug-06 20:38 
GeneralRe: Suggestion: No need to check comctl32.dll version PinmemberAnil K P10-Jun-07 23:54 
GeneralRe: Suggestion: No need to check comctl32.dll version PinmemberKeith Worden17-Jul-08 2:40 
GeneralUnicode PinmemberWarren D Stevens13-Nov-05 2:34 
GeneralRe: Unicode PinstaffNishant Sivakumar13-Nov-05 2:38 
GeneralMaking code Vista (or later) friendly PinmemberWarren D Stevens11-Nov-05 7:21 
GeneralRe: Making code Vista (or later) friendly PinmemberYongchun8-Jan-06 0:28 
GeneralRe: Making code Vista (or later) friendly PinmemberWarren D Stevens11-Jan-06 5:21 
GeneralRe: Making code Vista (or later) friendly PinmemberKeith Worden17-Jul-08 2:37 
GeneralRe: Making code Vista (or later) friendly PinmemberWarren Stevens17-Jul-08 17:21 
GeneralRe: Making code Vista (or later) friendly PinmemberKeith Worden18-Jul-08 0:11 
GeneralIsAppThemed returns FALSE for a Windows XP (Modified) theme PinsussBrian KS6-Oct-05 6:52 
Questionxp+ ? PinmemberUnruled Boy2-Jun-05 2:51 
AnswerRe: xp+ ? PinstaffNishant Sivakumar2-Jun-05 5:42 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 2 Jun 2005
Article Copyright 2005 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid