簡體   English   中英

如何遍歷 TRichEdit 文本的每個可見字符?

[英]How to loop through each visible char of a TRichEdit text?

在 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;

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

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM