Using TRichEdit at runtime without defining a parent

后端 未结 4 1675
囚心锁ツ
囚心锁ツ 2020-12-02 19:02

I need to use a TRichEdit at runtime to perform the rtf to text conversion as discussed here. I succeded in doing this but I had to set a dummy form as parent if not I canno

4条回答
  •  旧巷少年郎
    2020-12-02 19:50

    This has been the most helpfull for me to get started with TRichEdit, but not with the conversion. This however works as expected and you don't need to set the Line Delimiter:

    // RTF to Plain:
    procedure TForm3.Button1Click(Sender: TObject);
    var
        l:TStringList;
        s:WideString;
        RE:TRichEdit;
        ss:TStringStream;
    begin
        ss := TStringStream.Create;
        s := Memo1.Text; // Input String
        RE := TRichEdit.CreateParented(HWND_MESSAGE);
        l := TStringList.Create;
        l.Add(s);
        ss.Position := 0;
        l.SaveToStream(ss);
        ss.Position := 0;
        RE.Lines.LoadFromStream(ss);
        Memo2.Text := RE.Text; // Output String
    end;
    
    // Plain to RTF:
    procedure TForm3.Button2Click(Sender: TObject);
    var
        RE:TRichEdit;
        ss:TStringStream;
    begin
        RE := TRichEdit.CreateParented(HWND_MESSAGE);
        RE.Text := Memo2.Text; // Input String
        ss := TStringStream.Create;
        ss.Position := 0;
        RE.Lines.SaveToStream(ss);
        ss.Position := 0;
        Memo1.Text := ss.ReadString(ss.Size); // Output String
    end;
    

    I'm using the TStringList "l" in the conversion to plain because somehow the TStringStream puts every single character in a new line.

    Edit: Made the code a bit nicer and removed unused variables.

提交回复
热议问题