Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

XTimer - Timer and Stopwatch Utility with Source Code

, 20 Aug 2007
XTimer provides countdown timer and stopwatch features in a compact MFC dialog app.
xtimer_demo.zip
src
alarm1.wav
app.manifest
hans.ico
hightime.inl
more.ico
play.ico
playd.bmp
playu.bmp
reset.ico
start.ico
stop.ico
vs2005
vs6
Release
XTimer.exe
XTimer.dsp
XTimer.dsw
wav files
4beeps.wav
alarm1.wav
alarm2.wav
alarmclock.wav
alert.wav
beeps.wav
bell.wav
buzzer.wav
chirp.wav
ding.wav
dingdong.wav
fanfare.wav
gong.wav
ohoh.wav
onebell.wav
quackquack.wav
ringer.wav
the_timer_has_expired_f.wav
the_timer_has_expired_m.wav
trumpet.wav
xtimer_sounds.zip
alarmclock.wav
alert.wav
buzzer.wav
divedive.wav
europolice.wav
quackquack.wav
siren.wav
// XThemeHelper.cpp  Version 1.0
//
// Author:  Hans Dietrich
//          hdietrich@gmail.com
//
// Description:
//     XThemeHelper implements CXThemeHelper, a singleton helper class that
//     wraps the functions of UXTHEME.DLL.
//
// History
//     Version 1.0 - 2005 March 22
//     - Initial public release
//
// Public APIs:
//                  NAME                               DESCRIPTION
//     ------------------------------   -------------------------------------------
//     IsAppThemed()                    Reports whether the current application's 
//                                      user interface displays using visual styles.
//     IsThemeActive()                  Tests if a visual style for the current 
//                                      application is active.
//     IsThemeLibAvailable()            Test whether UXTHEME.DLL (and its functions) 
//                                      are accessible.
//     CloseThemeData()                 Closes the theme data handle.
//     DrawThemeBackground()            Draws the background image defined by the 
//                                      visual style for the specified control part.
//     DrawThemeText()                  Draws text using the color and font 
//                                      defined by the visual style.
//     GetThemeBackgroundContentRect()  Retrieves the size of the content area 
//                                      for the background defined by the visual 
//                                      style.
//     OpenThemeData()                  Opens the theme data for a window and 
//                                      its associated class.
//
// License:
//     This software is released into the public domain.  You are free to use
//     it in any way you like, except that you may not sell this source code.
//
//     This software is provided "as is" with no expressed or implied warranty.
//     I accept no liability for any damage or loss of business that this
//     software may cause.
//
///////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#define XTHEMEHELPER_CPP
#include "XThemeHelper.h"

///////////////////////////////////////////////////////////////////////////////
// class static data
PFNCLOSETHEMEDATA					CXThemeHelper::m_CloseThemeData = NULL;
PFNDRAWTHEMEBACKGROUND				CXThemeHelper::m_DrawThemeBackground = NULL;
PFNDRAWTHEMETEXT					CXThemeHelper::m_DrawThemeText = NULL;
PFNGETTHEMEBACKGROUNDCONTENTRECT	CXThemeHelper::m_GetThemeBackgroundContentRect = NULL;
PFNISAPPTHEMED						CXThemeHelper::m_IsAppThemed = NULL;
PFNISTHEMEACTIVE					CXThemeHelper::m_IsThemeActive = NULL;
PFNOPENTHEMEDATA					CXThemeHelper::m_OpenThemeData = NULL;
HMODULE 							CXThemeHelper::m_hThemeLib = NULL;
BOOL								CXThemeHelper::m_bThemeLibLoaded = FALSE;
int									CXThemeHelper::m_nUseCount = 0;

///////////////////////////////////////////////////////////////////////////////
// ctor
CXThemeHelper::CXThemeHelper()
{
	m_nUseCount++;

	//TRACE(_T("in CXThemeHelper: m_nUseCount=%d\n"), m_nUseCount);

	if (!m_bThemeLibLoaded)
	{
		Init();
	}
	else
	{
		//TRACE(_T("already initialized\n"));
	}
}

///////////////////////////////////////////////////////////////////////////////
// Init
void CXThemeHelper::Init()
{
	//TRACE(_T("in CXThemeHelper::Init\n"));

	m_hThemeLib = LoadLibrary(_T("UXTHEME.DLL"));

	if (m_hThemeLib)
	{
		m_CloseThemeData                = (PFNCLOSETHEMEDATA)GetProcAddress(m_hThemeLib,
																			"CloseThemeData");
		m_DrawThemeBackground           = (PFNDRAWTHEMEBACKGROUND)GetProcAddress(m_hThemeLib,
																			"DrawThemeBackground");
		m_DrawThemeText                 = (PFNDRAWTHEMETEXT)GetProcAddress(m_hThemeLib,
																			"DrawThemeText");
		m_GetThemeBackgroundContentRect = (PFNGETTHEMEBACKGROUNDCONTENTRECT)GetProcAddress(m_hThemeLib,
																			"GetThemeBackgroundContentRect");
		m_IsAppThemed                   = (PFNISAPPTHEMED)GetProcAddress(m_hThemeLib,
																			"IsAppThemed");
		m_IsThemeActive                 = (PFNISTHEMEACTIVE)GetProcAddress(m_hThemeLib,
																			"IsThemeActive");
		m_OpenThemeData                 = (PFNOPENTHEMEDATA)GetProcAddress(m_hThemeLib,
																			"OpenThemeData");
		if (m_CloseThemeData &&
			m_DrawThemeBackground &&
			m_DrawThemeText &&
			m_GetThemeBackgroundContentRect &&
			m_IsAppThemed &&
			m_IsThemeActive &&
			m_OpenThemeData)
		{
			//TRACE(_T("UXTHEME.DLL loaded ok\n"));
			m_bThemeLibLoaded = TRUE;
		}
		else
		{
			TRACE(_T("ERROR - failed to locate theme library function\n"));
			FreeLibrary(m_hThemeLib);
			m_hThemeLib = NULL;
		}
	}
	else
	{
		TRACE(_T("UXTHEME.DLL not accessible\n"));
	}
}

///////////////////////////////////////////////////////////////////////////////
// dtor
CXThemeHelper::~CXThemeHelper()
{
	//TRACE(_T("in CXThemeHelper::~CXThemeHelper\n"));

	if (m_nUseCount > 0)
		m_nUseCount--;

	if (m_nUseCount == 0)
	{
		//TRACE(_T("unloading theme dll\n"));
		if (m_hThemeLib)
			FreeLibrary(m_hThemeLib);
		m_hThemeLib = NULL;
		m_bThemeLibLoaded = FALSE;
	}
	//TRACE(_T("m_nUseCount=%d\n"), m_nUseCount);
}

///////////////////////////////////////////////////////////////////////////////
// CloseThemeData
BOOL CXThemeHelper::CloseThemeData(HTHEME hTheme)
{
	//TRACE(_T("in CXThemeHelper::CloseThemeData\n"));

	BOOL ok = FALSE;

	if (m_bThemeLibLoaded && hTheme)
	{
		HRESULT hr = m_CloseThemeData(hTheme);
		if (SUCCEEDED(hr))
			ok = TRUE;
	}

	return ok;
}

///////////////////////////////////////////////////////////////////////////////
// DrawThemeBackground
BOOL CXThemeHelper::DrawThemeBackground(HTHEME hTheme,
										HDC hdc,
										int iPartId,
										int iStateId,
										const RECT *pRect,
										const RECT *pClipRect)
{
	//TRACE(_T("in CXThemeHelper::DrawThemeBackground\n"));

	BOOL ok = FALSE;

	if (m_bThemeLibLoaded && hTheme)
	{
		HRESULT hr = m_DrawThemeBackground(hTheme, hdc, iPartId, iStateId, pRect,  pClipRect);
		if (SUCCEEDED(hr))
			ok = TRUE;
	}

	return ok;
}

///////////////////////////////////////////////////////////////////////////////
// DrawThemeText
BOOL CXThemeHelper::DrawThemeText(HTHEME hTheme,
								  HDC hdc,
								  int iPartId,
								  int iStateId,
								  LPCTSTR lpszText,
								  DWORD dwTextFlags,
								  DWORD dwTextFlags2,
								  const RECT *pRect)
{
	//TRACE(_T("in CXThemeHelper::DrawThemeText\n"));

	BOOL ok = FALSE;

	if (m_bThemeLibLoaded && hTheme)
	{
		HRESULT hr = S_OK;

#ifdef _UNICODE

		hr = m_DrawThemeText(hTheme, hdc, iPartId, iStateId,
						lpszText, wcslen(lpszText),
						dwTextFlags, dwTextFlags2, pRect);

#else

		int nLen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpszText, _tcslen(lpszText)+1, NULL, 0);
		nLen += 2;
		WCHAR * pszWide = new WCHAR[nLen];
		if (pszWide)
		{
			MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpszText, _tcslen(lpszText)+1, pszWide, nLen);
			hr = m_DrawThemeText(hTheme, hdc, iPartId, iStateId,
							pszWide, wcslen(pszWide),
							dwTextFlags, dwTextFlags2, pRect);
			delete pszWide;
		}

#endif

		if (SUCCEEDED(hr))
			ok = TRUE;
	}

	return ok;
}

///////////////////////////////////////////////////////////////////////////////
// GetThemeBackgroundContentRect
BOOL CXThemeHelper::GetThemeBackgroundContentRect(HTHEME hTheme,
												  HDC hdc,
												  int iPartId,
												  int iStateId,
												  const RECT *pBoundingRect,
												  RECT *pContentRect)
{
	//TRACE(_T("in CXThemeHelper::GetThemeBackgroundContentRect\n"));

	BOOL ok = FALSE;

	if (m_bThemeLibLoaded && hTheme)
	{
		HRESULT hr = m_GetThemeBackgroundContentRect(hTheme, hdc, iPartId, 
							iStateId, pBoundingRect, pContentRect);
		if (SUCCEEDED(hr))
			ok = TRUE;
	}

	return ok;
}

///////////////////////////////////////////////////////////////////////////////
// IsAppThemed
BOOL CXThemeHelper::IsAppThemed()
{
	BOOL ok = FALSE;

	if (m_bThemeLibLoaded)
	{
		ok = m_IsAppThemed();
	}

	return ok;
}

///////////////////////////////////////////////////////////////////////////////
// IsThemeActive
BOOL CXThemeHelper::IsThemeActive()
{
	BOOL ok = FALSE;

	if (m_bThemeLibLoaded)
	{
		ok = m_IsThemeActive();
	}

	return ok;
}

///////////////////////////////////////////////////////////////////////////////
// OpenThemeData
HTHEME CXThemeHelper::OpenThemeData(HWND hWnd, LPCTSTR lpszClassList)
{
	//TRACE(_T("in CXThemeHelper::OpenThemeData\n"));

	HTHEME hTheme = NULL;

	if (m_bThemeLibLoaded)
	{

#ifdef _UNICODE

		hTheme = m_OpenThemeData(hWnd, lpszClassList);

#else

		int nLen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpszClassList, 
			_tcslen(lpszClassList)+1, NULL, 0);
		nLen += 2;
		WCHAR * pszWide = new WCHAR [nLen];
		if (pszWide)
		{
			MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpszClassList, 
				_tcslen(lpszClassList)+1, pszWide, nLen);
			hTheme = m_OpenThemeData(hWnd, pszWide);
			delete pszWide;
		}

#endif

	}

	//TRACE(_T("m_hTheme=%X\n"), hTheme);

	return hTheme;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.
 
Recently, I have moved to Los Angeles where I am doing consulting and development work.
 
For consulting and custom software development, please see www.hdsoft.org.






| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 20 Aug 2007
Article Copyright 2003 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid