禁用词在.NET中的DrawString包装线时,打破 [英] Disable word breaking when wrapping lines in .NET DrawString

查看:127
本文介绍了禁用词在.NET中的DrawString包装线时,打破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET来绘制一个字符串转换成一个有限的空间。我希望字符串是尽可能地大。我有字符串中没有问题分解成多行(如滞留在矩形内)。现在问题:我不想.NET打破串在不同的行中一个字的中间。 比如字符串Test打印在一个大的字体一行。字符串测试应该在一个较小的字体一行打印(而不是在其他一线TESTI和NG)和字符串Test测试应该在一个相当大的字体来打印两行。

任何人有关于如何限制.NET不打破我的话的想法?

我目前使用的是code是这样的:

 内部静态无效PaintString(字符串s,诠释的x,INT Y,INT高度,诠释了maxWidth,图形克,BOOL下划线)
    {
        fontstyle的FS = FontStyle.Bold;
        如果(下划线)
            FS | = FontStyle.Underline;
        字体FNT =新System.Drawing.Font(宋体,18,FS);
        的SizeF大小= g.MeasureString(S,FNT,了maxWidth);
        而(size.Height>高)
        {
            FNT =新System.Drawing.Font(宋体,fnt.Size  -  1,FS);
            大小= g.MeasureString(S,FNT,了maxWidth);
        }
        Y =(int)的(Y +高度/ 2  -  size.Height / 2);
        g.DrawString(S,FNT,新SolidBrush(Color.Black),新的Rectangle(X,Y,了maxWidth,高度));
    }
 

解决方案

你有什么似乎是正确的答案。我不认为有这个框架的方法可以做到这一切为你一个电话。如果你是撕心裂肺的按钮和文本在WinForm的另一种选择,你应该看看ButtonRenderer和TextRenderer类。当调用的DrawText或MeasureString还可以指定TextFormatFlags,这将允许你指定WorkBreak,单线或使用椭圆形截断。

I am using .NET to draw a string into a limited space. I want the string to be as big as possible. I have no problem in the string breaking up into more lines (if it stays inside the rectangle). Now the problem: I don't want .NET to break the string in different lines in the middle of a word. For example string "Test" prints on a single line in a big font. String "Testing" should print on a single line in a smaller font (and not "Testi" on one line and "ng" on another) and string "Test Test" should print on two lines in a fairly large font.

Anybody got ideas on how to restrict .NET not to break my words?

I'm currently using a code like this:

        internal static void PaintString(string s, int x, int y, int height, int maxwidth, Graphics g, bool underline)
    {
        FontStyle fs = FontStyle.Bold;
        if (underline)
            fs |= FontStyle.Underline;
        Font fnt = new System.Drawing.Font("Arial", 18, fs);
        SizeF size = g.MeasureString(s, fnt, maxwidth);
        while (size.Height > height)
        {
            fnt = new System.Drawing.Font("Arial", fnt.Size - 1, fs);
            size = g.MeasureString(s, fnt, maxwidth);
        }
        y = (int)(y + height / 2 - size.Height / 2);
        g.DrawString(s, fnt, new SolidBrush(Color.Black), new Rectangle(x, y, maxwidth, height));
    }

解决方案

What you have got there seems to be the right answer. I dont think there is a single call to the framework method that can do all that for you. Another option if you are rending button and text in winform, you should look at the ButtonRenderer and TextRenderer class. When calling DrawText or MeasureString you can also specify TextFormatFlags which will allow you to specify WorkBreak, SingleLine or using Ellipse truncation.

这篇关于禁用词在.NET中的DrawString包装线时,打破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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