在TRichEdit中加载长RTF文本不起作用

时间:2017-04-28 11:42:04

标签: delphi delphi-xe4

如果将长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;

例如,使用以下RTF生成功能:

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中添加另一个字符,则不会发生任何事情
  • 如果我删除10个字符(每个退格),我之后可以添加10个字符......

TRichEdit是否有隐藏的字符限制?

2 个答案:

答案 0 :(得分:5)

Rich edit控件具有文本限制。

尝试使用EM_EXLIMITTEXT消息,该消息设置用户可以键入或粘贴到富编辑控件的文本量的上限。此消息还限制了流式RTF(PlainText = False)时可以流式传输到富编辑控件的文本量。但是在流式传输纯文本时不会限制控件。

e.g:

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()中实施的最高限额:RichEdit.DoSetMaxLength($7FFFFFF0); 但是,DoSetMaxLength() 未正确在源中使用,因为在加载流之前应将其称为。此外,DoSetMaxLength()根本不使用TRichEditStrings.LoadFromStream()。雷米mentioned this在他的答案评论中。

答案 1 :(得分:2)

除了kobik所说的:

TRichEdit.Lines.LoadFromStream()在内部使用EM_STREAMIN。当TRichEdit.PlainText为false时,LoadFromStream()将首先尝试将流数据加载为RTF,如果遇到任何错误,则会将流数据重新加载为纯文本。这就是您看到原始RTF代码出现的原因。

RTF是一种基于ASCII的格式,因此LoadFromStream()需要8位RTF数据(在PlainText=True的情况下,将尝试将其转换为Unicode)。尝试使用AnsiStringTMemoryStream代替(Unicode)StringTStringStream作为您的RTF流。

type
  TReadOnlyMemoryBufferStream = class(TCustomMemoryStream)
  public
    constructor Create(APtr: Pointer; ASize: NativeInt);
    function Write(const Buffer; Count: Longint): Longint; override;
  end;

constructor TReadOnlyMemoryBufferStream.Create(APtr: Pointer; ASize: NativeInt);
begin
  inherited Create;
  SetPointer(APtr, ASize);
end;

function TReadOnlyMemoryBufferStream.Write(const Buffer; Count: Longint): Longint;
begin
  Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  RtfText: AnsiString;
  Stream: TReadOnlyMemoryBufferStream;
begin
  RtfText := GenerateRtfText();

  Stream := TReadOnlyMemoryBufferStream.Create(PAnsiChar(RtfText), Length(RtfText));
  try
    RichEdit2.PlainText := False;
    RichEdit2.Lines.LoadFromStream(Stream);
    ...
  finally
    Stream.Free;
  end;
end;

function TForm1.GenerateRtfText: AnsiString;
var
  I: Integer;
  Stream: TMemoryStream;
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 := TMemoryStream.Create;
  try
    RichEdit1.PlainText := False;
    RichEdit1.Lines.SaveToStream(Stream);
    SetString(Result, PAnsiChar(Stream.Memory), Stream.Size);
  finally
    Stream.Free;
  end;
end;