2
我嘗試測量該區域在此代碼的字符串包圍每一個字符:MeasureCharacterRanges不適合角色?
void Draw(HDC dc)
{
using namespace Gdiplus;
Graphics graphics(dc);
wstring chars;
for(int i=32; i<127; ++i)
{
chars += i;
chars += 32;
}
Font font(L"Times New Roman", 30, FontStyleRegular, UnitPoint);
TextRenderingHint hint = TextRenderingHintAntiAlias ;
const StringFormat* pStringFormat = StringFormat::GenericTypographic();
StringFormat MeasureFormat(pStringFormat);
const float Offset = 60.f;
RectF layout(Offset, Offset, m_ScreenWidth - Offset*2, m_ScreenHeight - Offset*2);
std::vector<RectF> charSize;
for(size_t i=0; i<chars.size(); ++i)
{
if(i%2 == 0)
{
CharacterRange range(i, 1);
MeasureFormat.SetMeasurableCharacterRanges(1, &range);
Region region;
graphics.MeasureCharacterRanges(chars.c_str(), -1, &font, layout, &MeasureFormat, 1, ®ion);
RectF rec;
region.GetBounds(&rec, &graphics);
charSize.push_back(rec);
}
}
graphics.SetTextRenderingHint(hint);
for(size_t i=0 ; i<charSize.size(); ++i)
{
graphics.FillRectangle(&SolidBrush(0xaa998844), charSize[i]);
}
graphics.DrawString(chars.c_str(), -1, &font, layout, pStringFormat, &SolidBrush(0xff0000ff));
}
,這是結果:
正如你所看到的區域爲字母「F '和'g'並不完全包含這些字符。我該如何解決 ?