当文本具有三行或更多行时,我如何知道是否在 TRichEdit 中选择了所有文本?

How can I know if all the text is selected in a TRichEdit when the text has three or more lines?

我有一个 TRichEdit,其中包含一些 RTF(仅带格式的文本),我想知道是否选择了 TRichEdit 的所有内容。为此,我这样做:

var AllSelectd: Boolean;
//...
AllSelectd := MyRichEdit.SelLength = MyRichEdit.GetTextLen;

效果很好,除非内容包含三行或更多行。零到两行,一切都很好。一旦我在 TRichEdit 中达到三行,上面的代码就不再有效 (MyRichEdit.SelLength < MyRichEdit.GetTextLen)。每行以 CRLF (#13#10).

结尾

这是一个错误吗?我如何才能可靠地检查是否在 TRichEdit 中选择了所有内容?

我使用 Delphi 10.4,如果它有任何变化的话。

this topic 中所述,RichEdit 2.0 在内部将 CRLF 对替换为 CR,并在某些情况下检索 LF。

作为解决方法 - 计算选定范围内的行数以进行更正(SelText 仅包含 CR,GetTextLen 处理带有检索到的 CRLF 的文本,因此计算 CR 和 LF。使用 Remy Lebeau 提案。

var
  sel, getl, crcnt, i: integer;
  tx: string;
begin
  sel := RichEdit1.SelLength;
  getl := RichEdit1.GetTextLen;
  crcnt := SendMessage(Richedit1.Handle, EM_EXLINEFROMCHAR, 0, sel);
  Memo1.Lines.Add(Format('%d %d',[sel, getl - crcnt + 1]));
end;