gpt4 book ai didi

Delphi - 如何让 WrapText 表现得像 RichEdit.Lines.Text?

转载 作者:行者123 更新时间:2023-12-01 23:10:32 25 4
gpt4 key购买 nike

我使用 TRichEdit 来编辑文本。我打开了 WordWrap,它会自动在控件边缘自动换行文本,这样就不会出现滚动条。 RichEdit 宽度设置为适合 80 列。

只要单词之间偶尔有空格,WordWrap 就能很好地模拟这种行为。但是,如果您有一个非常长的单词,RichEdit 换行会自动中断它,而 WordWrap 不会!

如果我现在想访问这些换行,我可以通过 RichEdit.Lines.Text 获取它,它会自动将 CRLF 放在第 80 列。如果我使用 RichEdit.Text,我会得到没有任何内容的原始文本CRLF 适用于 WordWrap。

在自定义函数中完成这项任务并不简单,因为我已经尝试过,而且它不是一个小函数,但是是否有办法使用 TRichEdit 的包装函数?我有一个想法来创建隐藏的 RichEdit,但对于这项任务来说,这似乎很愚蠢,因为我必须计算它的宽度才能进行自动换行 - 我认为毫无意义。

问题 - 有没有办法强制 WordWrap 剪切无法换行的长单词?因此,如果一个单词的长度为 170 个字符,它将被包装成 3 行 80 + 80 + 10,但还要处理之前的单词以填充前后行?

最佳答案

试穿一下尺码:

procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);
const
MAX_LENGTH = 80;
var
nLineCount: integer;
sCurrentLine: string;
begin
for nLineCount := 0 to RichEdit1.Lines.Count - 1 do
begin
// no space in this line
if (pos(' ',RichEdit1.Lines.Strings[nLineCount]) = 0) then
begin
if (length(RichEdit1.Lines.Strings[nLineCount]) > MAX_LENGTH) then
begin
// hold the whole line
sCurrentLine := RichEdit1.Lines.Strings[nLineCount];
// the current line, make the max value
RichEdit1.Lines.Strings[nLineCount] := copy(sCurrentLine,1,MAX_LENGTH);
// build the next line
RichEdit1.Lines.Add(
copy(sCurrentLine,MAX_LENGTH + 1,1));
end;
end;
end;
end;

关于Delphi - 如何让 WrapText 表现得像 RichEdit.Lines.Text?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8936725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com