Опитвам се да напиша обвиващ клас около TRichEdit
, който може да кодира и декодира RTF към/от обикновен текст.
Ето какво съм написал досега:
type
TRTF = class
private
FRichEdit : TRichEdit;
procedure SetText(const AText: string);
function GetText: string;
class function Convert(const AInput: string; AEncode: Boolean): string; inline; static;
public
constructor Create;
destructor Destroy; override;
class function Decode(const AInput: string): string; static;
class function Encode(const AInput: string): string; static;
end;
constructor TRTF.Create;
begin
FRichEdit := TRichEdit.CreateParented(HWND_MESSAGE);
end;
destructor TRTF.Destroy;
begin
FRichEdit.Free;
inherited;
end;
function TRTF.GetText: string;
var
Stream: TStringStream;
begin
if FRichEdit.PlainText then begin
Stream := TStringStream.Create('', TEncoding.ANSI);
end else begin
Stream := TStringStream.Create('', TEncoding.ASCII);
end;
try
FRichEdit.Lines.SaveToStream(Stream, Stream.Encoding);
Result := Stream.DataString;
finally
Stream.Free;
end;
end;
procedure TRTF.SetText(const AText: string);
var
Stream: TStringStream;
begin
if FRichEdit.PlainText then begin
Stream := TStringStream.Create(AText, TEncoding.ANSI);
end else begin
Stream := TStringStream.Create(AText, TEncoding.ASCII);
end;
try
Stream.Seek(0, TSeekOrigin.soBeginning);
FRichEdit.Lines.LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
class function TRTF.Convert(const AInput: string; AEncode: Boolean): string;
var
RTF: TRTF;
begin
RTF := TRTF.Create;
try
RTF.FRichEdit.PlainText := AEncode;
RTF.SetText(AInput);
RTF.FRichEdit.PlainText := not AEncode;
Result := RTF.GetText;
finally
RTF.Free;
end;
end;
class function TRTF.Encode(const AInput: string): string;
begin
Result := Convert(AInput, True);
end;
class function TRTF.Decode(const AInput: string): string;
begin
Result := Convert(AInput, False);
end;
Това изглежда работи сравнително добре за всичко, което може да бъде представено от потребителската ANSI кодова страница по подразбиране.
Не успява (преобразува ги в ?
) за други знаци обаче.
Има ли лесен начин да активирам правилното боравене с Unicode в моя код?
Опитах се да използвам TEncoding.Unicode
и TEncoding.UTF8
като кодиране за TStringStream
, когато TRichEdit
е зададено на PlainText := True
, но това не работи.
TRichEdit
? - person Jerry Dodge   schedule 03.06.2013TRichEdit
. Вместо това Йенс трябва да използва контрола RichEdit без прозорци. - person Remy Lebeau   schedule 04.06.2013TRichEdit
? - person Jens Mühlenhoff   schedule 05.06.2013