测量包裹的绳子 [英] Measure wrapped string

查看:65
本文介绍了测量包裹的绳子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个控件,该控件基本上允许我在彼此之间绘制不同的字符串. 但是,字符串的宽度不能大于控件的宽度. 为了解决该问题,我正在考虑将RectangleF对象传递给Graphics.DrawString方法.这将包装比传递的矩形的宽度宽的字符串.尽管这确实解决了如果字符串太大则无法看到整个字符串的问题,但是还有另一个问题.如果我要尝试这样的事情

I'm trying to create a Control that basically allows me to draw different strings underneath one another. However, the strings' width may not be larger than the control's. In order to solve that problem, I was thinking of passing a RectangleF object to the Graphics.DrawString method. That would wrap strings which are wider than the passed rectangle's width. Although this does solve the problem of not being able to see the whole string if it's too big, there is another problem. If I were to try something like this

Graphics g = e.Graphics; // Paint event
g.DrawString(someText, someFont, someBrush, new PointF(0, 0), someRectangleF);
g.DrawString(someMoreText, someFont, someBrush, new PointF(0, 12), someRectangleF);

问题是,如果将someText换行,第三行将在第一个文本上方绘制文本,从而使其难以/不可能被读取.

the problem would be that if someText gets wrapped, the third line will paint text over of the first text, thus making it hard/impossible to be read.

我一直在寻找解决这个问题的方法,但是我发现了一些有趣的链接,但是其中包括使用for循环来测量每个字符的宽度等等. 有没有更简单的方法可以做到这一点?

I was looking for a solution for this problem, and I found some interesting links, which however included the use of a for loop which would measure each character's width and so on. Is there any simpler ways of doing this?

推荐答案

可以使用

Can you can use the Graphics.MeasureString method to get the dimensions of the string and draw the next string accordingly?

SizeF size = g.MeasureString(someText, someFont, someRectangleF.Size.Width);
g.DrawString(someText, someFont, someBrush, new PointF(0, 0), someRectangleF);
g.DrawString(someMoreText, someFont, someBrush, new PointF(0, size.Height), someRectangleF);

这篇关于测量包裹的绳子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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