GDI + MeasureString()不正确修剪的文字 [英] GDI+ MeasureString() is incorrectly trimming text

查看:480
本文介绍了GDI + MeasureString()不正确修剪的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完成一个code,可以布局在屏幕上的文字。下面的C#code放在油漆人少的Windows窗体的事件处理程序是一个例子:

 的String [] S =新的字符串[] {示例文本,测试,这种布局,算法};
的PointF [] PTS =新的PointF [s.Length]
的PointF开始=新的PointF(10,10);
的StringFormat F =新的StringFormat(StringFormat.GenericTypographic);
浮动X = start.X;
浮Y = start.Y;
的for(int i = 0; I< pts.Length;我++)
{
    分由[i​​] =新的PointF(X,Y);
    SZ的SizeF = e.Graphics.MeasureString(S [I],字体,PTS [I],F);
    X + = sz.Width;
    e.Graphics.DrawString(S [I],字体,Brushes.Black,PTS [I]);
}

它工作正常,除了在取值阵列每一块文本前后修整空白。它应该显示如下:


  

示例文本来测试这个布局算法


但相反,它似乎是这样的:


  

样textto testthis layoutalgorithm


我已经证实, f.Trimming 属性设置为。我也能猜到,这将增加尾随并打开空白的字符串的措施,但它仍然修剪它。如何让 MeasureString 任何想法方法包括空白?字距否则是完全处理。


解决方案

 的StringFormat F =新的StringFormat(StringFormat.GenericTypographic)
                     {FormatFlags = StringFormatFlags.MeasureTrailingSpaces};

I am trying to complete a code that can layout text on the screen. The following C# code placed in the Paint event handler of an otherwise empty Windows Form is an example:

string[] s = new string[] { "Sample text ", "to test", " this layout ", "algorithm" };
PointF[] pts = new PointF[s.Length];
PointF start = new PointF(10, 10);
StringFormat f = new StringFormat(StringFormat.GenericTypographic);
float x = start.X;
float y = start.Y;
for (int i = 0; i < pts.Length; i++)
{
    pts[i] = new PointF(x, y);
    SizeF sz = e.Graphics.MeasureString(s[i], Font, pts[i], f);
    x += sz.Width;
    e.Graphics.DrawString(s[i], Font, Brushes.Black, pts[i]);
}

It works correctly except for trimming the whitespace before and after each piece of text in the s array. It should display like this:

Sample text to test this layout algorithm

But instead it appears like this:

Sample textto testthis layoutalgorithm

I have confirmed that the f.Trimming property is set to None. I would have guessed that this would add trailing and opening whitespace to the measure of the strings, but it still trims it. Any ideas about how to make the MeasureString method include the whitespace? The kerning otherwise is handled perfectly.

解决方案

StringFormat f = new StringFormat(StringFormat.GenericTypographic)
                     { FormatFlags = StringFormatFlags.MeasureTrailingSpaces };

这篇关于GDI + MeasureString()不正确修剪的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆