I have some code in my application to select fonts. It shows a sample of the current font like this visually:
The code for that is here:
void CExportSettingsDlg::OnPaint()
{
CPaintDC dc( this ); // device context for painting
// This will draw the sample text for us using the current font
DrawSampleText( &dc );
// Do not call CDialog::OnPaint() for painting messages
}
void CExportSettingsDlg::DrawSampleText(CDC *pDC)
{
CDC dcMem;
CPen *pOldPen;
CBrush *pOldBrush;
CBitmap *pBmpOld;
CFont *pOldFont, fntSample;
LOGFONT lfSampleFont;
int nFontHeightPixels, iBackModeOld;
COLORREF crTextColourOld;
CString strSample;
ASSERT( pDC != NULL );
ASSERT( m_plfCurrentFont != NULL );
if( pDC != NULL && m_plfCurrentFont != NULL )
{
strSample.LoadString( IDS_STR_SAMPLE );
dcMem.CreateCompatibleDC( NULL );
// Select our gdi stuff into dc
pBmpOld = (CBitmap*)dcMem.SelectObject( &m_bmpSample );
pOldPen = (CPen*)dcMem.SelectStockObject( BLACK_PEN );
pOldBrush = (CBrush*)dcMem.SelectStockObject( WHITE_BRUSH );
// Create the font
fntSample.CreateFontIndirect( m_plfCurrentFont );
// Get it back out so that we can adjust the font size correctly
fntSample.GetLogFont( &lfSampleFont );
// Font height in pixels
nFontHeightPixels = theApp.GetFontHeightPixels(lfSampleFont.lfHeight, &dcMem);
// Update font
lfSampleFont.lfHeight = nFontHeightPixels;
fntSample.DeleteObject();
fntSample.CreateFontIndirect( &lfSampleFont );
// Erase display (will draw white rectangle with black border)
dcMem.Rectangle( m_rctSampleMem );
// Select our font and colour values and modes
pOldFont = (CFont*)dcMem.SelectObject( &fntSample );
crTextColourOld = dcMem.SetTextColor( m_crCurrentFontColour );
iBackModeOld = dcMem.SetBkMode( TRANSPARENT );
// Display the text
dcMem.DrawText( strSample, m_rctSampleText, DT_SINGLELINE|DT_VCENTER|DT_CENTER );
// Blast main memory dc to display
pDC->BitBlt( m_rctSample.left, m_rctSample.top,
m_rctSample.Width(), m_rctSample.Height(),
&dcMem, 0, 0, SRCCOPY );
// Restore old font and colour values and modes
dcMem.SelectObject( pOldFont );
dcMem.SetTextColor( crTextColourOld );
dcMem.SetBkMode( iBackModeOld );
dcMem.SelectObject( pBmpOld );
dcMem.SelectObject( pOldPen );
dcMem.SelectObject( pOldBrush );
// Release memory
dcMem.DeleteDC();
fntSample.DeleteObject();
// brushes and pens don't need deleting as they are stock objects
}
}
Now, I have other code that displays a standard CFontDialog
and on there the user can also set the font colour:
As you can see, on this dialog it uses a transparent background for the font sample so the white text shows up. But to be fair, it is showing up more because of the skinning I am using.
On my window I am using a white background. So you see my problem. That is just it. You can't see it. :)
Without getting complicated, and considering the fact I am colour blind (red / greens) is there a simple technique we can use to always ensure that the sample background in my sample will always be suitable given the colour of the font being rendered?
Thank you.