Click here to Skip to main content
15,896,111 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 402.6K   11.5K   141  
C++ class library.
//
// font.cpp
//
// (C) Copyright 2000 Jan van den Baard.
//     All Rights Reserved.
//

#include "font.h"
#include "getdc.h"

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

// Constructor. Initializes to NULL.
ClsFont::ClsFont()
{
}

// Constructor. Copy constructor.
ClsFont::ClsFont( const ClsFont& cFont )
{
	// Copy the font.
	operator=(( HFONT )cFont.m_hGdiObject );
}

// Constructor. Copy from a handle.
ClsFont::ClsFont( HFONT hFont )
{
	// Copy the font.
	operator=( hFont );
}

// Constructor. Create from a LOGFONT.
ClsFont::ClsFont( const LOGFONT *pLogFont )
{
	CreateFontIndirect( pLogFont );
}

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

// Create a font using a LOGFONT structure.
BOOL ClsFont::CreateFontIndirect( const LOGFONT *pLogFont )
{
	// Attach the font.
	Attach( ::CreateFontIndirect( pLogFont ));
	return ( BOOL )( m_hGdiObject ? TRUE : FALSE );
}

// Create a specified font.
BOOL ClsFont::CreateFont( int nHeight, int nWidth, int nEscapement, int nOrientation,
			  int fnWeight, DWORD fdwItalic, DWORD fdwUnderline, DWORD fdwStrikeOut, 
			  DWORD fdwCharSet, DWORD fdwOutputPrecision, DWORD fdwClipPrecision,
			  DWORD fdwQuality, DWORD fdwPitchAndFamily, LPCTSTR lpszFace )
{
	// Attach the font.
	Attach( ::CreateFont( nHeight, nWidth, nEscapement, nOrientation,
			      fnWeight, fdwItalic, fdwUnderline, fdwStrikeOut, 
			      fdwCharSet, fdwOutputPrecision, fdwClipPrecision,
			      fdwQuality, fdwPitchAndFamily, lpszFace ));
	return ( BOOL )( m_hGdiObject ? TRUE : FALSE );
}

// Create a specific font.
BOOL ClsFont::CreatePointFont( LPCTSTR pszTypeFace, UINT nHeight, DWORD dwFlags /* = CPFF_NORMAL */ )
{
	LOGFONT		lf = { 0 };
	ClsGetDC	dc( NULL );

	// Valid display device context?
	if ( dc.IsValid())
	{
		// Setup LOGFONT structure.
		lf.lfHeight		= -MulDiv( nHeight, dc.GetDeviceCaps( LOGPIXELSY ), 72);
		lf.lfWeight		= dwFlags & CPFF_BOLD ? FW_BOLD	: FW_DONTCARE;
		lf.lfItalic		= ( BYTE )( dwFlags & CPFF_ITALIC    ? 1 : 0 );
		lf.lfUnderline		= ( BYTE )( dwFlags & CPFF_UNDERLINE ? 1 : 0 );
		lf.lfStrikeOut		= ( BYTE )( dwFlags & CPFF_STRIKEOUT ? 1 : 0 );
		lf.lfCharSet		= DEFAULT_CHARSET;
		lf.lfOutPrecision	= OUT_DEFAULT_PRECIS;
		lf.lfClipPrecision	= CLIP_DEFAULT_PRECIS;
		lf.lfQuality		= DEFAULT_QUALITY;
		lf.lfPitchAndFamily	= DEFAULT_PITCH | FF_DONTCARE;

		// Copy face name.
		_tcsncpy( lf.lfFaceName, pszTypeFace, LF_FACESIZE );

		// Create...
		return CreateFontIndirect( &lf );
	}
	return FALSE;
}

// Get the LOGFONT structure.
BOOL ClsFont::GetLogFont( LOGFONT *pLogFont ) const
{
	_ASSERT_VALID( m_hGdiObject ); // Must be valid.
	_ASSERT_VALID( pLogFont ); // Must be valid.

	// Get the information.
	return GetObject( sizeof( LOGFONT ), ( LPVOID )pLogFont ) == sizeof( LOGFONT ) ? TRUE : FALSE;
}

// Get a stock font.
BOOL ClsFont::GetStockFont( int nObject )
{
	// Get the stock font.
	HFONT	hFont = ( HFONT )GetStockObject( nObject );
	
	// Make sure it is a font.
	if ( hFont && ::GetObjectType( hFont ) != OBJ_FONT )
		return FALSE;

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

// Return the object of the handle.
ClsFont *ClsFont::FromHandle( HFONT hFont )
{
	return ( ClsFont * )ClsGdiObject::FromHandle( hFont );
}

// Operator to return the font handle.
ClsFont::operator HFONT() const
{
	// Return the handle.
	return ( HFONT )m_hGdiObject;
}

// Assignment operator.
BOOL ClsFont::operator=( const ClsFont& cFont )
{
	// Can't assign to ourself.
	if ( &cFont == this )
		return TRUE;

	// Copy the font.
	return operator=(( HFONT )cFont.m_hGdiObject );
}

// Assignment operator.
BOOL ClsFont::operator=( HFONT hFont )
{
	// Get font information.
	LOGFONT		lf = { 0 };

	if ( hFont && // ::GetObjectType( hFont ) == OBJ_FONT &&
	     ::GetObject( hFont, sizeof( LOGFONT ), ( void * )&lf ))
	     // Create the object.
	     return CreateFontIndirect( &lf );

	return FALSE;
}

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