如何遍历 TRichEdit 文本的每个可见字符?



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

我试过这个:

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;

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

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;

相关内容

  • 没有找到相关文章

最新更新