Delphi-在备忘录中插入符号的整个单词 [英] Delphi - Get the whole word where the caret is in a memo
本文介绍了Delphi-在备忘录中插入符号的整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果插入符号直接相邻或在备忘录中的单词中,我需要能够选择TMemo的整个单词。
I need to be able to select the whole word of a TMemo if the caret is directly adjacent or in a word in the memo.
请考虑以下内容(其中|是插入符号)
Consider the following (where | is a caret)
以下是一些文本|
=选择文本
这里是|我的文字
=选择 some
|这里是一些文本
=选择此处
这里是一些文字|
=选择''
推荐答案
使用注释检查此代码以解释其工作原理。
Check this code with comments to explain how works.
function SelectWordUnderCaret(AMemo:TMemo):string;
var
Line : Integer;
Column : Integer;
LineText: string;
InitPos : Integer;
EndPos : Integer;
begin
//Get the caret position
Line := AMemo.Perform(EM_LINEFROMCHAR,AMemo.SelStart, 0) ;
Column := AMemo.SelStart - AMemo.Perform(EM_LINEINDEX, Line, 0) ;
//Validate the line number
if AMemo.Lines.Count-1 < Line then Exit;
//Get the text of the line
LineText := AMemo.Lines[Line];
Inc(Column);
InitPos := Column;
//search the initial position using the space symbol as separator
while (InitPos > 0) and (LineText[InitPos] <> ' ') do Dec(InitPos);
Inc(Column);
EndPos := Column;
//search the final position using the space symbol as separator
while (EndPos <= Length(LineText)) and (LineText[EndPos] <> ' ') do Inc(EndPos);
//Get the text
Result := Trim(Copy(LineText, InitPos, EndPos - InitPos));
//Finally select the text in the Memo
AMemo.SelStart := AMemo.Perform(EM_LINEINDEX, Line, 0)+InitPos;
AMemo.SelLength := Length(Result);
end;
,您可以像这样使用
procedure TForm1.Button1Click(Sender: TObject);
begin
Caption := SelectWordUnderCaret(Memo1) ;
end;
这篇关于Delphi-在备忘录中插入符号的整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文