无法在TRichEdit中加载长RTF文本 [英] Loading long RTF-Text in TRichEdit does not work
问题描述
如果将较长的RTF序列(例如150000个字符)流式传输到 TRichEdit
控件(在XE4中),则该控件不会显示文本,而是显示原始RTF代码:
{\rtf1\ansi\ansicpg1252\deff0 ...
出什么问题了?
程序TForm1.Button1Click(Sender:TObject);
var
RtfText:字符串;
流:TStringStream;
开始
RtfText:= GenerateRtfText();
Stream:= TStringStream.Create(RtfText);
试试
RichEdit2.PlainText:= False;
RichEdit2.Lines.LoadFromStream(Stream); //< ---错误:RichEdit显示原始RTF代码
//如果RtfText太长
,如果StartsText('{\rtf',RichEdit2.Lines.Text)然后
开始
ShowMessage('哦,不,不转换!');
// WORKAROUND:第二次尝试似乎可行...
//Stream.Position:= 0;
//RichEdit2.Lines.LoadFromStream(Stream);
结尾;
最后
流。免费;
结尾;
结尾;
例如,具有以下RTF生成功能:
函数TForm1.GenerateRtfText:字符串;
var
I:整数;
流:TStringStream;
const
DOES_WORK_COUNT = 10000;
DOES_NOT_WORK_COUNT = 15000;
begin
//填充
RichEdit1.Lines.BeginUpdate;
试试
//对于I:= 0到DOES_WORK_COUNT做
对于I:= 0到DOES_NOT_WORK_COUNT做
RichEdit1.Lines.Add(IntToStr(I));
最后
RichEdit1.Lines.EndUpdate;
结尾;
//转换为RTF
Stream:= TStringStream.Create;
试试
RichEdit1.Lines.SaveToStream(Stream);
结果:= Stream.DataString;
最后
流。免费;
结尾;
结尾;
已编辑:即使复制和粘贴也无法正常工作:
这就是我所做的:
- 我复制了RichEdit1的生成内容(第1行) ..15000(具有数字1..15000)到notpad.exe中以删除任何RTF
- 我将记事本的内容复制到RichEdit2中
结果:
- 仅正确显示12773行。最后一行只有
12
- 如果我尝试向TRichEdit中添加其他字符,则什么也没发生
- 如果我删除了10个字符(每个退格键),则以后可以添加正好10个字符...
是否存在隐藏字符
丰富的编辑控件具有文本限制。
尝试使用 EM_EXLIMITTEXT
消息,它为用户可以输入或粘贴到丰富编辑中的文本量设置了上限控制。此消息还限制了在流式传输RTF时可以流式传输到丰富编辑控件中的文本量( PlainText = False
)。但不限制流式传输纯文本时的控制。
例如:
const
RE_MAX_TEXT_SIZE = 256000;
SendMessage(RichEdit1.Handle,EM_EXLIMITTEXT,0,RE_MAX_TEXT_SIZE);
或:
SendMessage(RichEdit1.Handle,EM_EXLIMITTEXT,0,$ 7FFFFFF0);
获取 TRichEditStrings.LoadFromFile()$中实现的最大限制c $ c>:
RichEdit.DoSetMaxLength($ 7FFFFFF0);
但是, DoSetMaxLength()
是在源中使用不正确,因为应在流加载之前将其称为。此外, TRichEditStrings.LoadFromStream()
完全不使用 DoSetMaxLength()
。雷米在评论中提及了他的答案。
If a long RTF-sequenz (eg 150 000 chars) is streamed into a TRichEdit
control (in XE4), the control does not display the text but instead shows the raw RTF code:
{\rtf1\ansi\ansicpg1252\deff0...
What is wrong?
procedure TForm1.Button1Click(Sender: TObject);
var
RtfText: string;
Stream: TStringStream;
begin
RtfText := GenerateRtfText();
Stream := TStringStream.Create(RtfText);
try
RichEdit2.PlainText := False;
RichEdit2.Lines.LoadFromStream(Stream); //<--- ERROR: RichEdit displays raw RTF-Code
// if RtfText is too long
if StartsText('{\rtf', RichEdit2.Lines.Text) then
begin
ShowMessage('Oh no, not converted!');
//WORKAROUND: 2nd try seems to work...
//Stream.Position := 0;
//RichEdit2.Lines.LoadFromStream(Stream);
end;
finally
Stream.Free;
end;
end;
For instance with following RTF generation function:
function TForm1.GenerateRtfText: string;
var
I: Integer;
Stream: TStringStream;
const
DOES_WORK_COUNT = 10000;
DOES_NOT_WORK_COUNT = 15000;
begin
//Fill
RichEdit1.Lines.BeginUpdate;
try
//for I := 0 to DOES_WORK_COUNT do
for I := 0 to DOES_NOT_WORK_COUNT do
RichEdit1.Lines.Add(IntToStr(I));
finally
RichEdit1.Lines.EndUpdate;
end;
//Convert to RTF
Stream := TStringStream.Create;
try
RichEdit1.Lines.SaveToStream(Stream);
Result := Stream.DataString;
finally
Stream.Free;
end;
end;
Edited: Even copy and paste does not work correctly:
This is what I did:
- I copied the generated content of RichEdit1 (lines 1..15000 with numbers 1..15000) into notpad.exe to remove any RTF
- I copied the content of notepad into RichEdit2
Result:
- only 12773 lines are displayed correctly. The last line is only
12
- if I try to add another char into the TRichEdit nothing happens
- if I remove 10 chars (per backspace) I can add exactly 10 chars afterwards...
Is there a hidden character limit for TRichEdit?
Rich edit control has a text limit.
Try using EM_EXLIMITTEXT
message, which sets an upper limit to the amount of text the user can type or paste into a rich edit control. This message also limit the amount of text that you can stream into a rich edit control when streaming RTF (PlainText = False
). but does not limit the control when streaming plain text.
e.g.:
const
RE_MAX_TEXT_SIZE = 256000;
SendMessage(RichEdit1.Handle, EM_EXLIMITTEXT, 0, RE_MAX_TEXT_SIZE);
or:
SendMessage(RichEdit1.Handle, EM_EXLIMITTEXT, 0, $7FFFFFF0);
for the maximum limit as implemented in TRichEditStrings.LoadFromFile()
: RichEdit.DoSetMaxLength($7FFFFFF0);
However, DoSetMaxLength()
is not correctly used in the sources, as it should be called before the stream is loaded. Also, DoSetMaxLength()
is not used at all for TRichEditStrings.LoadFromStream()
. Remy mentioned this in the comments of his answers.
这篇关于无法在TRichEdit中加载长RTF文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!