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)
{
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
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.
Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.
Nish's Technology Blog :
voidnish.wordpress.com