TRichEdit 支持 Unicode 吗?

2024-01-10

我正在尝试编写一个包装类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 and TEncoding.UTF8作为编码TStringStream当。。。的时候TRichEdit被设定为PlainText := True,但这不起作用。


我认为你把这个问题过于复杂化了。没有必要使用PlainText财产根本。保留其默认设置True。然后,读取/写入 RTF 使用LoadFromStream and 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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TRichEdit 支持 Unicode 吗? 的相关文章

  • 如何使用用户输入来寻址 Pascal 中的特定变量(Eval/Exec?)

    我正在尝试在分形程序中做一些非常具体的事情隆起7X http apophysis 7x org 使用的脚本语言是Pascal 该项目是用德尔福写的 https svn code sf net p apophysis7x svn trunk
  • 获取字符、整数和日期字段的字段 oldValue 和 newValue

    我试图只保留表更改的历史记录 所以我想获取一个字段在更改为oldValue之前的值 然后获取它更改为newValue的值 两个值都应转换为字符串 因此 这是该表的一个示例 PartNumber Description 12345 Test
  • Delphi - 将物理路径(设备文件句柄)转换为虚拟路径

    我怎样才能转换像这样的路径 设备 HarddiskVolume3 Windows 进入其相应的虚拟路径 如本例中的 c Windows 我个人更喜欢原生方式 function GetHDDDevicesWithDOSPath TString
  • 如何在ggplot2中使用希腊符号?

    我的类别需要用希腊字母命名 我在用ggplot2 并且它与数据配合得很好 不幸的是 我无法弄清楚如何将这些希腊符号放在 x 轴上 在刻度线处 并使它们出现在图例中 有什么办法可以做到吗 更新 我看了一下link https github c
  • 什么是标准 unicode 字体?

    以下操作系统的标准 unicode 字体是什么 视窗XP 视窗Vista Window 7 按照标准 我的意思是它们存在于操作系统的全新安装中 无需将它们作为附加包安装 我一直在寻找同样的东西 看起来所有 Win 操作系统中只有一种字体 L
  • Delphi如何使用其他窗体中的类型?

    抱歉 这是一个非常新手的问题 我正在对这个庞大的应用程序进行维护 它有5种不同的形式 我们将全局变量放在一个单元 uGlobal 中 但我似乎无法从数据单元 uData 访问它 我有这个 Unit uGlobal type TmyType
  • 如何用 unicode 图像替换字符?

    我怎样才能更换一个 来自数据库到碧玉字段的文本中的字符 带有图像 目标如下图所示 就像是 F KN Zusatzinfo DV Einleitungstext replaceAll x254 哪里的x254是红色方块的ascii 代码 但上
  • 在Python中通过sys.stdout写入unicode字符串

    暂时假设一个人无法使用print 从而享受自动编码检测的好处 所以这给我们留下了sys stdout 然而 sys stdout太蠢了不做任何合理的编码 http bugs python org issue4947 现在人们阅读 Pytho
  • 是否有一个看起来像“钥匙”图标的 Unicode 字形? [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 Unicode 有一百万个类似图标的字形 但它们并不总是很容易搜索 因为我并不总是知道它们是什么样子 是否有一个看起来像 钥匙 的 Unicode 字
  • Java中的字节和字符转换

    如果我将一个字符转换为byte然后回到char 那个角色神秘地消失了 变成了别的东西 这怎么可能 这是代码 char a line 1 byte b byte a line 2 char c char b line 3 System out
  • DELPHI 和 WANT 或 NANT

    We use 巡航控制 net http confluence public thoughtworks org display CCNET Welcome to CruiseControl NET在 Delphi 2006 应用程序中进行持
  • 从 Delphi 调用 C# dll

    我用单一方法编写了 Net 3 5 dll 由Delphi exe调用 不幸的是它不起作用 步骤 1 使用以下代码创建 C 3 5 dll public class MyDllClass public static int MyDllMet
  • Delphi 5 的哈希表实现 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 您知道 Delphi 5 的良好且免费的哈希表实现吗 我需要在哈希表中组织大量数据 并且我有点担心在网
  • 查找Delphi项目中的所有编译错误

    我正在对我的 Delphi 项目进行一些重构 我希望能够做出改变 然后看看all项目中因该更改而中断的地方 类似于 Eclipse 列出项目的所有编译错误 在 Java 中 在 Delphi 中 我可以进行更改 然后重新编译我的项目 但编译
  • 如何从 Delphi 中的函数返回对象而不导致访问冲突?

    我有一个返回 TStringList 的 delphi 函数 但是当我返回一个值并尝试使用它时 我收到一个访问冲突错误 即 myStringList FuncStringList myStringList Items Count lt Th
  • TColorProperty德尔福柏林10.1.2?

    我正在尝试将组件从 Delphi 7 转换为 Delphi Berlin 平面组件 https sourceforge net projects flatstyle https sourceforge net projects flatst
  • Delphi应用程序窗口z顺序和MainFormOnTaskBar属性

    我正在维护一个最初用 Delphi 7 编写并移植到 Delphi XE 的应用程序 使用 Windows 7 我们遇到了一些问题 例如模态窗口出现在主窗口下方 以及最终无法与程序交互 因为用户需要与模态窗体交互 而这是不可能的 因为它位于
  • 使用 Python 编辑 RTF 文件

    也许这是一个愚蠢的问题 但我不明白 所以道歉 我有一个 RTF 文档 我想更改它 例如 有一个表 我想复制一行并以面向对象的方式更改代码中第二行中的文本 我认为 pyparsing 应该是可行的方法 但我摆弄了几个小时但没有明白 我没有提供
  • 如何去除 XSL 中字符的重音符号?

    我一直在寻找 但找不到相当于字符 规范化空间 的 XSL 函数 也就是说 我的内容带有重音 UNICODE 字符 这很好 但是从该内容中 我正在创建一个文件名 但我不想要这些重音 那么 是否有一些我忽略的东西 或者没有正确地谷歌搜索来轻松处
  • Delphi 2010 - 从 XML 文档解码 Base64 编码图像

    我正在尝试从应用程序中的 XML 文档解码 base64 编码的 EMF 图像并将其呈现在屏幕上 但是 它似乎从未出现 如果我将 XML 文档中的数据复制 粘贴到 Notepad 中并使用Base64 Decode选项并将文件另存为 emf

随机推荐