1

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

我试过这个:

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;

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

4

1 回答 1

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;
于 2020-01-14T11:48:57.357 回答