Click here to Skip to main content
15,885,870 members
Articles / Desktop Programming / MFC

ClassLib, A C++ class library

Rate me:
Please Sign up or sign in to vote.
4.80/5 (32 votes)
25 May 2005CPOL8 min read 399.6K   11.5K   141  
C++ class library.
//
// palette.cpp
//
// (C) Copyright 2000 Jan van den Baard.
//     All Rights Reserved.
//

#include "palette.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// Constructor.
ClsPalette::ClsPalette()
{
}

// Destructor. Does nothing.
ClsPalette::~ClsPalette()
{
}

// Create palette.
BOOL ClsPalette::CreatePalette( const LOGPALETTE *pLogPalette )
{
	// Attach the palette.
	Attach( ::CreatePalette( pLogPalette ));
	return ( BOOL )( m_hGdiObject ? TRUE : FALSE );
}

// Create half-tone palette.
BOOL ClsPalette::CreateHalftonePalette( HDC hDC )
{
	// Attach the palette.
	Attach( ::CreateHalftonePalette( hDC ));
	return ( BOOL )( m_hGdiObject ? TRUE : FALSE );
}

// Animate palette.
void ClsPalette::AnimatePalette( UINT nStartIndex, UINT nNumEntries, LPPALETTEENTRY pPaletteColors )
{
	_ASSERT_VALID( m_hGdiObject ); // Must be valid.

	// Animate.
	::AnimatePalette(( HPALETTE )m_hGdiObject, nStartIndex, nNumEntries, pPaletteColors );
}

// Get palette index.
UINT ClsPalette::GetNearestPaletteIndex( COLORREF crColor ) const
{
	_ASSERT_VALID( m_hGdiObject ); // Must be valid.

	// Get index.
	return ::GetNearestPaletteIndex(( HPALETTE )m_hGdiObject, crColor );
}

// Resize the palette.
BOOL ClsPalette::ResizePalette( UINT nNumEntries )
{
	_ASSERT_VALID( m_hGdiObject ); // Must be valid.

	// Resize...
	return ::ResizePalette(( HPALETTE )m_hGdiObject, nNumEntries );
}

// Realize the palette.
UINT ClsPalette::RealizePalette( HDC hDC )
{
	_ASSERT_VALID( m_hGdiObject ); // Must be valid.

	// Realize...
	return ::RealizePalette( hDC );
}

// Retrieve palette entries.
UINT ClsPalette::GetPaletteEntries( UINT nStartIndex, UINT nNumEntries, LPPALETTEENTRY pPaletteColors ) const
{
	_ASSERT_VALID( m_hGdiObject ); // Must be valid.

	// Get the entries.
	return ::GetPaletteEntries(( HPALETTE )m_hGdiObject, nStartIndex, nNumEntries, pPaletteColors );
}

// Set palette entries.
UINT ClsPalette::SetPaletteEntries( UINT nStartIndex, UINT nNumEntries, LPPALETTEENTRY pPaletteColors )
{
	_ASSERT_VALID( m_hGdiObject ); // Must be valid.

	// Set the entries.
	return ::SetPaletteEntries(( HPALETTE )m_hGdiObject, nStartIndex, nNumEntries, pPaletteColors );
}

// Get the entry count of the palette.
WORD ClsPalette::GetEntryCount() const
{
	_ASSERT_VALID( m_hGdiObject ); // Must be valid.
	WORD	wCount = 0;

	// Get the object count.
	GetObject( sizeof( WORD ), ( LPVOID )&wCount );
	return wCount;
}

// Get a stock palette.
BOOL ClsPalette::GetStockPalette( int nObject )
{
	// Get the stock palette.
	HPALETTE	hPalette = ( HPALETTE )GetStockObject( nObject );
	
	// Make sure it is a palette.
	if ( hPalette && ::GetObjectType( hPalette ) != OBJ_PAL )
		return FALSE;

	// Attach the object.
	Attach( hPalette );
	return TRUE;
}

// Return the object of the handle.
ClsPalette *ClsPalette::FromHandle( HPALETTE hPalette )
{
	return ( ClsPalette * )ClsGdiObject::FromHandle( hPalette );
}

// Operator to return the palette handle.
ClsPalette::operator HPALETTE() const
{
	// Return the handle.
	return ( HPALETTE )m_hGdiObject;
}

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
Software Developer (Senior)
Netherlands Netherlands
I have been programming for a hobby since 1985. I have started programming on the C= 64. After that I migrated to the C= Amiga which I traded in for a PC back in 1997 I believe. Back in 2000 I decided to lose a hobby and start developing software for a living.

Currently I am working mainly in developing software for building security and access control systems.

Comments and Discussions