Re: Drawing a CFont into a TGA file?
"Pat" <none@none.none> ha scritto nel messaggio
news:Xns9A432150332EBnone@140.99.99.130...
What I want to do now is add a function to draw text
using a CFont. I'm not even sure where to start. CFont doesn't have any
methods for drawing text to a bitmap, etc... And ideally, I'd like to
have
control over anti-aliasing options, etc...
I did some coding with DirectDraw. DirectDraw has the concept of "surface",
a DirectDraw surface is a drawing surface, where you can draw everything,
including text rendered with particular fonts.
If you have a DirectDraw surface, you can get its DC (device context) using
IDirectDrawSurfaceX::GetDC method (X is an integer and depends on the
version of DirectDraw you use).
Once you have the DC, you can use GDI to set parameters like text foreground
and background color (using APIs like SetTextColor and SetBkColor). And you
can also select a font into the DC, using SelectObject( hDC, hFont ) (hDC is
handle to DC, hFont is handle to font to select).
The TextOut function draws the text.
Once you have finished, you can use IDirectDrawSurfaceX::ReleaseDC to
release the DC.
You can integrate DirectDraw into MFC, as well (if you search on
CodeProject, you may find useful articles about that).
(Moreover, using DirectDraw for 2D graphics is fast.)
You can also find interesting code in DirectX SDK: there are files like
ddutil.h and ddutil.cpp that have useful functions and helper classes.
You may find something here:
http://tinyurl.com/32prq8
and this is the code of a useful method:
<code>
//-----------------------------------------------------------------------------
// Name: CSurface::DrawText()
// Desc: Draws a text string on a DirectDraw surface using hFont or the
default
// GDI font if hFont is NULL.
//-----------------------------------------------------------------------------
HRESULT CSurface::DrawText( HFONT hFont, TCHAR* strText,
DWORD dwOriginX, DWORD dwOriginY,
COLORREF crBackground, COLORREF crForeground )
{
HDC hDC = NULL;
HRESULT hr;
if( m_pdds == NULL || strText == NULL )
return E_INVALIDARG;
// Make sure this surface is restored.
if( FAILED( hr = m_pdds->Restore() ) )
return hr;
if( FAILED( hr = m_pdds->GetDC( &hDC ) ) )
return hr;
// Set the background and foreground color
SetBkColor( hDC, crBackground );
SetTextColor( hDC, crForeground );
if( hFont )
SelectObject( hDC, hFont );
// Use GDI to draw the text on the surface
TextOut( hDC, dwOriginX, dwOriginY, strText, _tcslen(strText) );
if( FAILED( hr = m_pdds->ReleaseDC( hDC ) ) )
return hr;
return S_OK;
}
</code>
HTH.
Giovanni