【问题标题】:How to loop through each visible char of a TRichEdit text?如何遍历 TRichEdit 文本的每个可见字符?
【发布时间】:2020-01-13 15:12:04
【问题描述】:

在 Delphi 10.3.3 中,这是循环遍历(多行)TRichEdit 的每个可见 Char(即排除不可打印字符,例如 #13)的最简单、最快和最有效的方法文本?然后我需要根据我的计算来获取和设置每个字符的颜色。

这个我试过了:

function GetCharByIndex(Index: Integer): Char;
begin
  RichEdit1.SelStart := Index;
  RichEdit1.SelLength := 1;
  Result := RichEdit1.SelText[1];
end;

RichLen := RichEdit1.GetTextLen - RichEdit1.Lines.Count;
for i := 0 to RichLen - 1 do
begin
  c := GetCharByIndex(i);
  if c = #13 then CONTINUE;
  // ... do my stuff here
end;

但我相信一定有更好的方法。

【问题讨论】:

  • 相关:Faster Rich Edit Syntax Highlighting。它是为 C++Builder 编写的,但它所说的一切也适用于 Delphi
  • 文本最初是如何进入编辑框的?在输入时而不是之后对其进行格式化怎么样?此时您应该拥有更多信息才能更智能地进行操作。如果不了解更大的问题,很难说更多......
  • RichEdit 通常比较慢。如果您正在使用语法高亮显示类型的控件,我建议您使用 SynEdit(或 Delphi 的 Scintilla);这些比 RichEdit 控件更快。
  • @TLama 我正在创造一些全新的东西:一种使用颜色显示两个文本之间差异的方法。
  • 这不是一个新的应用程序;我们通常使用它来比较源代码。无论如何,根据我的经验,我不会使用 RichEdit。我提到的控件(或至少是)相对更快。

标签: delphi delphi-10.3-rio trichedit


【解决方案1】:
var
  i: Integer;  
  c: Char;
  cord: Integer;
...
i := -1;
for c in RichEdit1.Text do
begin
  Inc(i);
  cord := ord(c);
  if (cord = 13) then
    Dec(i);
  if (cord >= 32) and (not ((cord > 126) and (cord < 161))) then
  begin
    // do your stuff here, for example exchanging red and green colors:
    RichEdit1.SelStart := i;
    RichEdit1.SelLength := 1;
    if RichEdit1.SelAttributes.Color = clGreen then
      RichEdit1.SelAttributes.Color := clRed
    else if RichEdit1.SelAttributes.Color = clRed then
      RichEdit1.SelAttributes.Color := clGreen;
  end;
end;

【讨论】:

  • 那很慢。对于这样一个基本的解决方案,写一个问题很可惜
  • 什么是“慢”?在我的电脑上,它非常快。测量它。
  • 如果够了,我想在几年前使用这个简化。但这取决于你。 MS Word 和 Write.exe 都使用 TOM。还有其他文本程序。
  • 实测:228 个字符为 0.025 秒。
  • 对于小任务,我们可以使用小解决方案。我同意。
猜你喜欢
  • 2017-10-13
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多