gpt4 book ai didi

delphi - TRichEdit 支持 Unicode 吗?

转载 作者:行者123 更新时间:2023-12-01 22:23:47 24 4
gpt4 key购买 nike

我正在尝试围绕 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 处理?

TRichEdit 时,我尝试使用 TEncoding.UnicodeTEncoding.UTF8 作为 TStringStream 的编码> 设置为 PlainText := True,但这不起作用。

最佳答案

我认为你把这个问题过于复杂化了。根本不需要使用 PlainText 属性。将其保留为默认设置 True。然后,要读取/写入 RTF,请使用 LoadFromStream 和 SaveToStream。要读取/写入纯文本,请使用 Lines.Text 属性。

这已经是我能做到的最简单的了:

type
TRTF = class
strict private
class function CreateRichEdit: TRichEdit; static;
public
class function Decode(const AInput: string): string; static;
class function Encode(const AInput: string): string; static;
end;

class function TRTF.CreateRichEdit: TRichEdit;
begin
Result := TRichEdit.CreateParented(HWND_MESSAGE);
end;

class function TRTF.Encode(const AInput: string): string;
var
RichEdit: TRichEdit;
Stream: TStringStream;
begin
RichEdit := CreateRichEdit;
try
RichEdit.Lines.Text := AInput;
Stream := TStringStream.Create;
try
RichEdit.Lines.SaveToStream(Stream);
Result := Stream.DataString;
finally
Stream.Free;
end;
finally
RichEdit.Free;
end;
end;

class function TRTF.Decode(const AInput: string): string;
var
RichEdit: TRichEdit;
Stream: TStringStream;
begin
RichEdit := CreateRichEdit;
try
Stream := TStringStream.Create(AInput);
try
RichEdit.Lines.LoadFromStream(Stream);
Result := RichEdit.Lines.Text;
finally
Stream.Free;
end;
finally
RichEdit.Free;
end;
end;

关于delphi - TRichEdit 支持 Unicode 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16902084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com