精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。需要全文内容也请联系孙老师。
This article will demonstrate the use of MeasureCharacterRanges to draw a string a character at a time. Not very exciting, I hear you cry. Bear with me.
本文将演示使用MeasureCharacterRanges逐字符绘制字符串。我已经听到你尖叫了,但不要太兴奋。请耐心等待。
I wrote this short article after seeing a question posted on a newsgroup about being able to draw text along a curve. While drawing a character at a time is easy, I wanted to make sure the character spacing was maintained.
我写这篇简短的文章是因为,看到在新闻讨论组张贴的一个问题,关于能够沿着一条曲线绘制文本的问题。虽然有时绘制一个字符是很容易的,但我想确保字符的间距。
Just set up some text to work with: 设置一些文本处理:
string measureString = "This is a test string.";
int numChars = measureString.Length;
Initialize the character ranges array, this is used to delimit the blocks of characters in the string, in this example, each character is a 'range'.
初始化字符数组的范围,这在字符串中用来分隔字符块,在这个例子中,每个字符都是一个“范围”。
//
// Set up the characted ranger array.
Now, initialize the StringFormatFlags, I'm using the NoClips flag to ensure that the character is not clipped when drawing.
现在,初始化StringFormatFlags ,我使用NoClips标志,以确保在绘制时字符没有被省略一部分。
//
// Set up the string format
StringFormat stringFormat = new StringFormat();
stringFormat.FormatFlags = StringFormatFlags.NoClip;
stringFormat.SetMeasurableCharacterRanges(characterRanges);
Set up an array to hold the calculated regions: 建立一个数组来保存计算区域:
//
// Set up the array to accept the regions.
Region[] stringRegions = new Region[numChars];
for(int i = 0; i<numChars; i++)
characterRanges[i] = new CharacterRange(i, 1);
Create a font, and use MeasureCharacterRanges() to calculate the regions for the character ranges.
创建一个字体,并使用MeasureCharacterRanges()来计算字符的区域范围。
The regions returned by MeasureCharacterRanges are converted to rectangles by using the GetBounds() function. The rectangle can then be manipulated using offset or any other method to adjust its placement.
通过MeasureCharacterRanges返回的区域是通过使用getBounds()函数被转换为矩形。矩形可以使用偏移或其他可以调整布局的方法来操纵。
In this example, I offset the Y position using a random amount to give wavy text.
在这个例子中,我使用随机量给出一个波浪形的文本,偏移Y轴的位置。
// // The font to use.. 'using' will dispose of it for us using (Font stringFont = new Font("Times New Roman", 16.0F)) { // // Get the max width.. for the complete length SizeF size = g.MeasureString(measureString, stringFont ); // // Assume the string is in a stratight line, just to work out the // regions. We will adjust the containing rectangles later. RectangleF layoutRect = new RectangleF( 0.0f, 0.0f, size.Width, size.Height); // // Caluclate the regions for each character in the string. stringRegions = g.MeasureCharacterRanges( measureString, stringFont, layoutRect, stringFormat); // // Some random offsets, uncomment the DrawRectagle // if you want to see the bounding box. Random rand = new Random(); for ( int indx = 0 ; indx < numChars; indx++ ) { Region region = stringRegions[indx] as Region; RectangleF rect = region.GetBounds(g); rect.Offset( 0f, (float) rand.Next(100) / 10f); g.DrawString( measureString.Substring(indx,1), stringFont, Brushes.Yellow, rect, stringFormat ); // g.DrawRectangle( Pens.Red, Rectangle.Round( rect )); } }
As I said at the beginning, this is not the most efficient code, the calculations could be done outside the drawing routine and cached. However, I hope this demonstrates how easy it is to determine the character positions.
正如我在开始时说,这还不是最高效的代码,计算可以在绘图程序和缓存的范围之外被执行。然而,我希望这演示了确定字符位置是多么容易。