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

XBa*_*000 1 delphi message position richedit char

我有函数返回一个字符的索引GetCharFromPos(Pt:TPoint):整数;

现在我想得到那个位置的特征.比如GetCharByIndex(Index:Integer):Char;

Dav*_*nan 6

高效的方式来做到这一点使用纯VCL是使用SelStart,SelLength和SelText.

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

您可能希望在修改之前保存选择,然后在读取字符后恢复它.


然而,这是阅读角色的一种相当混乱的方式.如果您准备使用原始Win32 API,那么您可以使用EM_GETTEXTRANGE.