Click here to Skip to main content
15,893,644 members
Articles / Programming Languages / C#

Adding XP Themes to Custom .NET Controls

Rate me:
Please Sign up or sign in to vote.
4.76/5 (38 votes)
31 Aug 2003CPOL5 min read 231.7K   5.5K   130  
Rendering your own theme parts with the Windows XP UxTheme API
This article is about exploring the ability to create custom controls in the .NET platform, that make use of Windows XP's visual styles and themes. To that end, I have included a couple of fairly simple controls that mimic some of the behaviors that Windows Explorer implements in XP.
/////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2003 - Don Kackman
//
// Distribute and change freely, but please don't remove my name from the source 
//
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// The class CVisualStylesXp and the conept of theme browser app are borrowed from
// David Y Zhao. His example can be seen at http://www.codeproject.com/w2k/xpvisualstyle.asp
//
// questions - contact me at dkackman_2000@yahoo.com
//

#include "StdAfx.h"
#include ".\ThemeItemCollection.h"
#using <mscorlib.dll>
#include < vcclr.h >
#include "PropTable.h"

using namespace System;
using namespace System::Windows::Forms::Themes;

ThemeItemCollection::ThemeItemCollection( int startIndex ) :  m_StartIndex( startIndex )
{
}

ThemeItemCollection::~ThemeItemCollection(void)
{
}


int ThemeItemCollection::get_Count()
{
	if ( m_StartIndex == -1 )
		return 0;

	const wchar_t szParts[] = L"PARTS";
	const wchar_t szStates[] = L"STATES";

	int count = 0;

	PropTable table;

	for ( int i = m_StartIndex + 1; i < table.GetCount(); i++ )
	{
		if ( wcsstr( table[i], szParts ) )
			break;
		else if ( ( wcsstr( table[i], szStates ) ) )
			break;
		else
			count++;
	}

	return count;
}

int ThemeItemCollection::GetItemTableIndex( int localIndex )
{
	const wchar_t szParts[] = L"PARTS";
	const wchar_t szStates[] = L"STATES";

	int tableIndex = -1;

	if ( m_StartIndex != -1 )
	{
		PropTable table;

		int count = 0;
		for ( int i = m_StartIndex + 1; i < table.GetCount(); i++ )
		{
			if ( wcsstr( table[i], szParts ) )
				break;
			else if ( ( wcsstr( table[i], szStates ) ) )
				break;
			else if ( localIndex == count )
			{
				tableIndex = i;
				break;
			}
			
			count++;		
		}
	}

	if ( tableIndex == -1 )
		throw new System::IndexOutOfRangeException();

	return tableIndex;
}

int ThemeItemCollection::GetItemTableIndex( String* name )
{
	THROW_IF_NULL( name );

	const wchar_t szParts[] = L"PARTS";
	const wchar_t szStates[] = L"STATES";

	int tableIndex = -1;

	if ( m_StartIndex != -1 )
	{
		PropTable table;
		wchar_t __pin* wcName = PtrToStringChars( name->ToUpper() );

		for ( int i = m_StartIndex + 1; i < table.GetCount(); i++ )
		{
			if ( wcsstr( table[i], szParts ) )
				break;
			else if ( ( wcsstr( table[i], szStates ) ) )
				break;
			else if ( wcscmp( table[i], wcName ) == 0 )
			{
				tableIndex = i;	
				break;
			}
		}
	}

	if ( tableIndex == -1 )
		throw new System::IndexOutOfRangeException();

	return tableIndex;
}

void ThemeItemCollection::CopyTo( System::Array* array, int startIndex )
{
	THROW_IF_NULL( array );

	if ( array->Rank != 1 )
		throw new System::ArgumentException( "array", "Array must be one dimensional" );

	if ( startIndex < 0 )
		throw new System::ArgumentOutOfRangeException( "startIndex", "startIndex must be greater or equal to 0" );

	if ( startIndex + Count > array->Length )
		throw new System::ArgumentException( "array", "The array is not large enough to copy the collection contents into" );

	for ( int i = 0; i < Count; i++ )
		array->SetValue( GetEnumeratorItem( i ), startIndex + i );
}

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)


Written By
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions