Click here to Skip to main content
15,898,134 members
Articles / Desktop Programming / ATL

SP Numeric Edit Control

Rate me:
Please Sign up or sign in to vote.
4.78/5 (11 votes)
16 Nov 200511 min read 75K   3.6K   36  
Masked numeric edit ActiveX control.
/////////////////////////////////////////////////////////////////////////////////////////
//	Project:		SP Numeric Edit Control 1.0
//
//	File:			spOleFont.cpp
//
//	Developer(s):	Sergei Pavlovsky
//					sergei_vp@hotmail.com
//					Copyright (c) 2004
//
//	Description:	Implementation of COleFont.
//
//	Platforms:		Win32
//
//	This code may be used in compiled form in any way you desire. This file may be 
//	redistributed unmodified by any means PROVIDING it is not sold for profit without 
//	the authors written consent, and providing that this notice and the authors name 
//	is included. If the source code in this file is used in any commercial application 
//	then acknowledgement must be made to the author of this file (in whatever form 
//	you wish).
//
//	This file is provided "as is" with no expressed or implied warranty. The author 
//	accepts no liability for any damage/loss of business that this product may cause.
/////////////////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "spOleFont.h"

namespace SP
{

/////////////////////////////////////////////////////////////////////////////////////////
// COleFont class implementation
/////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
// Utilities

HRESULT COleFont::CreateFromLogFont(const LOGFONT& lf, HDC hDC, REFIID riid, 
									IFont** ppiFont)
{
	ATLASSERT( !::IsBadWritePtr(ppiFont, sizeof(IFont*)) );

	DWORD cchBuf = lstrlen(lf.lfFaceName) + 1;
	OLECHAR szFontName[LF_FACESIZE] = OLESTR("");

#if defined(UNICODE)
	::lstrcpyn(szFontName, lf.lfFaceName, cchBuf);
#else
	if ( !::MultiByteToWideChar(CP_ACP, 0, lf.lfFaceName, cchBuf, szFontName, cchBuf) )
		return  E_FAIL;
#endif // defined(UNICODE)

	FONTDESC fd;
	fd.cbSizeofstruct = sizeof(FONTDESC);
	fd.lpstrName = szFontName;

	LONG lHeight = (lf.lfHeight < 0) ? -lf.lfHeight : lf.lfHeight;

	fd.cySize.int64 = (Int32x32To64(lHeight, 720000))/GetDeviceCaps(hDC, LOGPIXELSY);

	fd.sWeight = (SHORT)lf.lfWeight; 
	fd.sCharset = lf.lfCharSet; 
	fd.fItalic = lf.lfItalic; 
	fd.fUnderline = lf.lfUnderline; 
	fd.fStrikethrough = lf.lfStrikeOut; 

	HRESULT hr = ::OleCreateFontIndirect(&fd, riid, (void**)ppiFont);
	ATLASSERT( SUCCEEDED(hr) );
	if FAILED(hr)   
		return E_FAIL;
 
	return S_OK;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Operations

HFONT COleFont::CreateHFONT() const
{
	ATLASSERT( m_piFont );

	HFONT hFont = NULL;

	HRESULT hr = m_piFont->get_hFont(&hFont);
	ATLASSERT( SUCCEEDED(hr) );
	if FAILED(hr)
		return NULL;

	hr = m_piFont->AddRefHfont(hFont);
	ATLASSERT( SUCCEEDED(hr) );
	if FAILED(hr)
		return NULL;

	return hFont;
}

void COleFont::FreeHFONT(HFONT hFont) const
{
	ATLASSERT( hFont );
	ATLASSERT( m_piFont );

	HRESULT hr = m_piFont->ReleaseHfont(hFont);
	ATLASSERT( SUCCEEDED(hr) );
}


/////////////////////////////////////////////////////////////////////////////////////////
// Accessors & mutators

// Font
HRESULT COleFont::GetFont(IFont** ppiFont) const
{
	if ( !ppiFont )
		return E_POINTER;

	*ppiFont = m_piFont;

	if ( *ppiFont )
		(*ppiFont)->AddRef();

	return S_OK;
}

void COleFont::SetFont(IFont* piFont)
{
	if ( m_piFont )
		m_piFont->Release();

	m_piFont = piFont;

	if ( m_piFont )
		m_piFont->AddRef();
}

HRESULT COleFont::SetFontCopy(IFont* piFont)
{
	IFont* piFontCopy = NULL;

	if ( piFont )
	{
		HRESULT hr = piFont->Clone(&piFontCopy);
		ATLASSERT( SUCCEEDED(hr) );
		if FAILED(hr)  return E_FAIL;
	}
	else
	{
		m_piFont = NULL;
	}

	if ( m_piFont )
		m_piFont->Release();

	m_piFont = piFontCopy;

	return S_OK;
}

// FontDisp
HRESULT COleFont::GetFontDisp(IFontDisp** ppiFontDisp) const    
{
	if ( !ppiFontDisp )
		return E_POINTER;

	if ( m_piFont )
	{
		HRESULT hr = m_piFont->QueryInterface(IID_IFontDisp, 
						reinterpret_cast<void**>(ppiFontDisp));
		ATLASSERT( SUCCEEDED(hr) );
		if FAILED(hr)
			return E_FAIL;
	}
	else
	{
		*ppiFontDisp = NULL;
	}

	return S_OK;
}

HRESULT COleFont::SetFontDisp(IFontDisp* piFontDisp)
{
	IFont* piFont = NULL;

	if ( piFontDisp )
	{
		HRESULT hr = piFontDisp->QueryInterface(IID_IFont, 
						reinterpret_cast<void**>(&piFont));
		ATLASSERT( SUCCEEDED(hr) );
		if FAILED(hr)
			return E_FAIL;
	}

	if ( m_piFont )
		m_piFont->Release();

	m_piFont = piFont;

	return S_OK;
}

HRESULT COleFont::SetFontDispCopy(IFontDisp* piFontDisp)
{
	IFont* piFontCopy = NULL;

	if ( piFontDisp )
	{
		IFont* piOriginalFont = NULL;

		HRESULT hr = piFontDisp->QueryInterface(IID_IFont, 
						reinterpret_cast<void**>(&piOriginalFont));
		ATLASSERT( SUCCEEDED(hr) );
		if FAILED(hr)
			return E_FAIL;

		hr = piOriginalFont->Clone(&piFontCopy);
		piOriginalFont->Release();
		ATLASSERT( SUCCEEDED(hr) );
		if FAILED(hr)
			return E_FAIL;
	}

	if ( m_piFont )
		m_piFont->Release();

	m_piFont = piFontCopy;

	return S_OK;
}

// HFONT
HRESULT COleFont::SetHFONT(HFONT hFont, HDC hDC)
{
	ATLASSERT( hFont );

	LOGFONT lf;
	BOOL bRes = ::GetObject(hFont, sizeof(LOGFONT), &lf);
	ATLASSERT( bRes );
	if ( !bRes )
		return E_FAIL;

	IFont*	piFont = NULL;
	HRESULT hr = CreateFromLogFont(lf, hDC, IID_IFont, &piFont);
	ATLASSERT( SUCCEEDED(hr) );
	if FAILED(hr)
		return E_FAIL;

	if ( m_piFont )
		m_piFont->Release();

	m_piFont = piFont;

	return S_OK;
}

// LOGFONT
HRESULT COleFont::SetLogFont(const LOGFONT& lf, HDC hDC)
{
	IFont*	piFont = NULL;
 	HRESULT hr = CreateFromLogFont(lf, hDC, IID_IFont, &piFont);
	ATLASSERT( SUCCEEDED(hr) );
	if FAILED(hr)
		return E_FAIL;

	if ( m_piFont )
		m_piFont->Release();

	m_piFont = piFont;

	return S_OK;
}

}; // SP namespace

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions