我可以将TMemo的大小本身包含在文本中吗? [英] Can I make a TMemo size itself to the text it contains?

查看:118
本文介绍了我可以将TMemo的大小本身包含在文本中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您在表单设计器中编辑TLabel的标题时,会为您调整TLabel的大小。有什么办法可以在运行时得到一个TMemo吗?

When you edit a TLabel's caption in the form designer, it resizes the TLabel for you. Is there any way I can get a TMemo to do that, at runtime?

我想要能够使用TMemo,将其分配给它的.lines。文本属性,然后告诉它自己调整大小,不要超过一定的宽度,尽管它可以像想要的那样高。任何人都知道如何做?

I'd like to be able to take a TMemo, assign something to its .lines.text property, and then tell it to resize itself and not exceed a certain width, though it can get as tall as it wants to. Anyone know how to do that?

推荐答案

将备忘录的WordWrap属性设置为true,将文本转储到其中线,并将高度设置为行数和行高的乘积。但是您需要知道行高。

Set the WordWrap property of the memo to true, dump your text into it, count the lines, and set the height to the product of the line count and the line height. But you need to know the line height.

tMemo对象不会显示行高度属性,但如果在运行时不更改字体或字体大小,您可以在设计时实际确定线高度。

The tMemo object does not expose a line height property, but if you're not changing the font or font size at runtime, you can determine the line height experimentally at design time.

以下是用于设置线高度为13像素的备注的高度的代码。我还发现,我需要一个小常数来记录备忘录的顶部和底部边界。我将高度限制在30行(396像素)以保留在表单上。

Here's the code I used to set the height of a memo that had a line height of 13 pixels. I also found that I needed a small constant to account for the memo's top and bottom borders. I limited the height to 30 lines (396 pixels) to keep it on the form.

// Memo.WordWrap = true (at design time)
Memo.Text := <ANY AMOUNT OF TEXT>;
Memo.Height := min(19+Memo.Lines.Count*13,396); 

如果您绝对必须在运行时从对象中提取行高,则必须使用Someone's回答。或者,您可以使用一个tRichEdit对象,该对象具有一个包含Height属性的SelAttributes属性,赋予行高。

If you absolutely must extract the line height from the object at run time, then you must use 'Someone's answer. Or, you can use a tRichEdit object, which has a SelAttributes property containing a Height property giving the line height.

-Al。

这篇关于我可以将TMemo的大小本身包含在文本中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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