简体   繁体   English

如何从 TRichedit 按点或按索引获取字符

[英]How to get the Character by Point or by index from TRichedit

I have function that returns the index of a character GetCharFromPos(Pt: TPoint): Integer;我有 function 返回字符的索引 GetCharFromPos(Pt: TPoint): Integer;

now i wanted to get character of that position.现在我想得到那个 position 的字符。 like GetCharByIndex(Index: Integer): Char;像 GetCharByIndex(Index: Integer): Char;

The efficient way to do this using pure VCL is to use SelStart , SelLength and SelText .使用纯 VCL 执行此操作的有效方法是使用SelStartSelLengthSelText

function GetCharByIndex(Index: Integer): Char;
begin    
  RichEdit.SelStart := Index;
  RichEdit.SelLength := 1;
  Result := RichEdit.SelText[1];
end;

You'll likely want to save away the selection before modifying it, and then restore it once you have read the character.您可能希望在修改之前保存选择,然后在阅读字符后恢复它。


This is however a rather messy way to read a character.然而,这是读取字符的一种相当混乱的方式。 If you are prepared to use raw Win32 API then you can make use of EM_GETTEXTRANGE .如果您准备使用原始 Win32 API 那么您可以使用EM_GETTEXTRANGE

Here is how you return the character at a given index from a TRichEdit:以下是如何从 TRichEdit 返回给定索引处的字符:

Result := RichEdit1.Text[Index];

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM