如果将长RTF-sequenz(例如150 000个字符)流式传输到TRichEdit控件(在XE4中),则控件不会显示文本,而是显示原始RTF代码:
{\rtf1\ansi\ansicpg1252\deff0...
怎么了?
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;
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;
编辑:即使复制和粘贴无法正常工作:
这就是我做的:
>我将生成的RichEdit1内容(行号1..15000,数字1..15000)复制到notpad.exe中以删除任何RTF
>我将记事本的内容复制到RichEdit2中
结果:
>只能正确显示12773行.最后一行只有12
>如果我尝试在TRichEdit中添加另一个char,则没有任何反应
>如果我删除10个字符(每个退格键),我之后可以添加10个字符…
TRichEdit是否有隐藏的字符限制?
解决方法
丰富的编辑控件具有文本限制.
尝试使用EM_EXLIMITTEXT
消息,该消息设置用户可以键入或粘贴到富编辑控件的文本量的上限.此消息还限制在流式传输RTF时可以流式传输到富编辑控件的文本量(PlainText = False).但是在流式传输纯文本时不限制控件.
例如.:
const RE_MAX_TEXT_SIZE = 256000; SendMessage(RichEdit1.Handle,EM_EXLIMITTEXT,RE_MAX_TEXT_SIZE);
要么:
SendMessage(RichEdit1.Handle,$7FFFFFF0);
对于TRichEditStrings.LoadFromFile()中实现的最大限制:RichEdit.DoSetMaxLength($7FFFFFF0);
但是,DoSetMaxLength()未在源中正确使用,因为应在加载流之前调用它.此外,DoichMaxLength()根本不用于TRichEditStrings.LoadFromStream().雷米mentioned this在他的回答评论中.