我用CreateFontIndirect()和GetTextMetrics()创建了新字体,返回不同宽度的符号。例如:
获取DC:
HDC ReferenceDC = GetDC( NULL );
SetMapMode( ReferenceDC, MM_HIMETRIC );
m_nFontHeight_HM = font.lfHeight * INCH_IN_HIMETRIC / 72 / MILLIMETER_IN_HIMETRIC;
可变字体已保存字体参数:
LOGFONT lf = font;
lf.lfHeight = -m_nFontHeight_HM;
创建并选择字体:
guards::tlGDIGuard< HFONT > fontGuard( ReferenceDC, CreateFontIndirect( &lf ) );
if ( !fontGuard.GetObject() )
{
throw system_exception_ex( exceptions::EXCEPTION_TYPE_NT );
}
获取Textmetric: (已经有不同的XP和Win 7)
TEXTMETRIC TextMetric;
GetTextMetrics( ReferenceDC, &TextMetric );
m_nFontHeight_HM = -lf.lfHeight;
OUTLINETEXTMETRIC TextMetrics;
if ( !GetOutlineTextMetrics( ReferenceDC, sizeof( TextMetrics ), &TextMetrics ) )
{
throw system_exception_ex( exceptions::EXCEPTION_TYPE_NT );
}
const unsigned int nFirstChar = TextMetrics.otmTextMetrics.tmFirstChar;
const unsigned int nLastChar = TextMetrics.otmTextMetrics.tmLastChar;
const unsigned int nCharsCount = nLastChar - nFirstChar + 1;
获取字符宽度:
boost::scoped_array< ABC > pCharWidths( new ABC[ nCharsCount ] );
if ( !GetCharABCWidths( ReferenceDC, nFirstChar, nLastChar, pCharWidths.get() ) )
{
throw system_exception_ex( exceptions::EXCEPTION_TYPE_NT );
}
m_CharactersWidthsArray.resize( nLastChar + 1, 0 );
long WideCharCount = 0;
m_nTabWhidth = 0;
for ( unsigned int nChar = nFirstChar; nChar <= nLastChar; ++nChar )
{
const int nCharWidth = pCharWidths[ nChar - nFirstChar ].abcA + pCharWidths[ nChar - nFirstChar ].abcB + pCharWidths[ nChar - nFirstChar ].abcC;
m_CharactersWidthsArray[ nChar ] = nCharWidth;
if ( nCharWidth )
{
m_nTabWhidth += nCharWidth;
WideCharCount++;
}
}