简体   繁体   English

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

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

I have a TRichEdit with some RTF in it (text with formatting only) and I want to know if all the content of the TRichEdit is selected.我有一个带有一些RTF的 TRichEdit(仅带格式的文本),我想知道是否选择了 TRichEdit 的所有内容。 To do so, I do:为此,我这样做:

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

which works fine, except when the content has three lines or more.效果很好,除非内容有三行或更多行。 With zero to two lines, everything is fine.零到两行,一切都很好。 As soon as I reach three lines in my TRichEdit, the code above no longer works ( MyRichEdit.SelLength < MyRichEdit.GetTextLen ).一旦我在 TRichEdit 中达到三行,上面的代码就不再有效( MyRichEdit.SelLength < MyRichEdit.GetTextLen )。 Each line is terminated with CRLF ( #13#10 ).每行都以 CRLF ( #13#10 ) 结尾。

Is this a bug?这是一个错误吗? How can I reliably check if everything is selected in the TRichEdit?我怎样才能可靠地检查是否在 TRichEdit 中选择了所有内容?

I use Delphi 10.4, if it changes anything.我使用 Delphi 10.4,如果它有任何变化的话。

As mentioned in this topic , RichEdit 2.0 replaces CRLF pairs with CR internally, and retrieves LF's in some cases.本主题所述,RichEdit 2.0 在内部将 CRLF 对替换为 CR,并在某些情况下检索 LF。

As workaround - calculate number of lines in selected range to make correction ( SelText contains only CR's, GetTextLen works with text with retrieved CRLF, so counts both CR's and LF's).作为解决方法 - 计算选定范围内的行数以进行更正( SelText仅包含 CR, GetTextLen处理带有检索到的 CRLF 的文本,因此计算 CR 和 LF)。 Remy Lebeau proposal is used.使用 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在设计时将格式丰富的文本分配给 Delphi TRichEdit 或 TcxRichEdit(可能使用 Lines 属性)? - How to assign richly formatted text to Delphi TRichEdit or TcxRichEdit during design time (possibly, using Lines attribute)? TRichEdit查找受保护的文本 - TRichEdit finding protected text 在为所选文本设置背景颜色后,将字体分配给TRichEdit - Assigning a font to a TRichEdit after setting background colour for selected text Delphi彩色文本组件替换了TRichEdit以提高性能 - Delphi lines colored Text component replacing TRichEdit to improve performances 如何在更改某些非文本字符的字体时使TRichEdit在Windows 7上的行为类似于写字板? - How to make TRichEdit behave like WordPad on Windows 7 when changing font for certain non-text characters? 如何在TRichEdit的顶部插入新的,未格式化的行 - How do I insert new, unformatted, lines at the top of a TRichEdit Delphi 2010-TRichEdit上的文本转换为HTML - Delphi 2010 - Text on TRichEdit to HTML TRichEdit 中同一行中的彩色文本 - Colorful text in the same line in TRichEdit 如何遍历 TRichEdit 文本的每个可见字符? - How to loop through each visible char of a TRichEdit text? 如何使用Delphi在TRichEdit中添加HyperLink - How can I add an HyperLink in TRichEdit using Delphi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM