如题,我也不知道为什么,测试代码:
1
using
namespace Gdiplus;
2
3 wstring strLine = L
"
1个你好啊2个你好啊3个你好啊4个你好啊5个你好啊6个你好啊7个你好啊8个你好啊
";
4
const
int wordCount = strLine.size();
5
6 CPaintDC paintDc(
this);
7 Graphics g(paintDc.GetSafeHdc());
8 Gdiplus::Font myfont(L
"
宋体
",
16, Gdiplus::FontStyleBold);
9
10
const
int region_count = wordCount +
1;
11 Gdiplus::CharacterRange *wordRange =
new Gdiplus::CharacterRange[region_count];
//
最后一个是整行的范围
12
13
//
构建 region
14
int wbegIndex =
0;
15
int wordSize;
16
for (
int wordIndex =
0; wordIndex < wordCount; ++wordIndex){
17 wordRange[wordIndex].First = wbegIndex;
18 wordRange[wordIndex].Length =
1;
19 wbegIndex +=
1;
20 }
21
22 wordRange[wordCount].First =
0;
23 wordRange[wordCount].Length = strLine.size();
24
25
//
measure region
26
Region *wordRegion =
new Region[region_count];
27
28 StringFormat stringFormat;
29 stringFormat.SetMeasurableCharacterRanges(region_count, wordRange);
30
31 Gdiplus::RectF measureRect(
0,
0, INT_MAX,
100);
32 g.MeasureCharacterRanges(strLine.c_str(), strLine.size(), &myfont, measureRect, &stringFormat, region_count, wordRegion );
33
34 delete[] wordRange;
35
36
//
获取每个词的rect
37
RectF *wordRect =
new Gdiplus::RectF[region_count];
38
39
for (
int i =
0; i < region_count; ++i){
40 wordRegion[i].GetBounds(wordRect + i, &g);
41 }
42
43 delete[] wordRegion;
44 delete[] wordRect;
要解决这个问题也很简单,在调用MeasureCharacterRanges时,使用如下代码:
1
int regionIndex =
0;
2
while (regionIndex < region_count){
3
int count = region_count - regionIndex;
4
if (count >
31) count =
31;
5
6 stringFormat->SetMeasurableCharacterRanges(count, wordRange + regionIndex);
7 g.MeasureCharacterRanges(strLine.c_str(), strLine.size(), &myfont, measureRect, &stringFormat, count, wordRegion + regionIndex );
8
9 regionIndex += count;
10 }