[英]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.