我的应用程序中有一些代码用于选择字体。它以可视方式显示当前字体的样本:
代码就在这里:
a.reshape(3,3,9,9).transpose((0,2,1,3)).reshape(27,27)
#array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 81, 82, 83, 84,
# 85, 86, 87, 88, 89, 162, 163, 164, 165, 166, 167, 168, 169,
# 170],
# [ 9, 10, 11, 12, 13, 14, 15, 16, 17, 90, 91, 92, 93,
# 94, 95, 96, 97, 98, 171, 172, 173, 174, 175, 176, 177, 178,
# 179],
# ...
# [558, 559, 560, 561, 562, 563, 564, 565, 566, 639, 640, 641, 642,
# 643, 644, 645, 646, 647, 720, 721, 722, 723, 724, 725, 726, 727,
# 728]])
现在,我有其他代码显示标准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
}
}
,用户还可以在其中设置字体颜色:
正如您所看到的,在此对话框中,它使用字体样本的透明背景,以便显示白色文本。但公平地说,由于我正在使用皮肤,它会出现更多。
在我的窗口我使用的是白色背景。所以你看到了我的问题。就是这样。您无法看到它。 :)
没有变得复杂,并且考虑到我是色盲(红色/绿色)的事实,我们可以使用一种简单的技术来始终确保 my 样本中的样本背景总是适合给定正在呈现的字体的颜色?
谢谢。
答案 0 :(得分:0)
根据指示我另一个问题的评论,我能够解决这个问题:
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 );
CBrush brBackground;
double dLumi = (0.2126 * GetRValue(m_crCurrentFontColour))
+ (0.7152*GetGValue(m_crCurrentFontColour)) + (0.0722*GetBValue(m_crCurrentFontColour));
if (dLumi <= 0.5)
brBackground.CreateSolidBrush(PALETTERGB(255, 255, 255));
else
brBackground.CreateSolidBrush(PALETTERGB(0, 0, 0));
pOldBrush = (CBrush*)dcMem.SelectObject(&brBackground);
// 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 a 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
brBackground.DeleteObject();
}
}