procedure TForm1.InsertStringAtcaret(MyMemo: TMemo; const MyString: string);
begin
MyMemo.Text :=
// copy the text before the caret
Copy(MyMemo.Text, 1, MyMemo.SelStart) +
// then add your string
MyString +
// and now ad the text from the memo that cames after the caret
// Note: if you did have selected text, this text will be replaced, in other words, we copy here the text from the memo that cames after the selection
Copy(MyMemo.Text, MyMemo.SelStart + MyMemo.SelLength + 1, length(MyMemo.Text));
// clear text selection
MyMemo.SelLength := 0;
// set the caret after the inserted string
MyMemo.SelStart := MyMemo.SelStart + length(MyString) + 1;
end;
4条答案
按热度按时间db2dz4w81#
您可以使用
EM_CHARFROMPOS
来确定鼠标光标指向的字符位置:7y4bm7vi2#
如果你想把你的字符串放在备忘录的插入符号位置,你可以使用下面的代码:
pbossiut3#
你可以在“.Lines”成员的底部添加一个字符串:
MyMemo.Lines.add('MyString')
.你也可以 * 替换 * 你想要的“Lines”中任何(已经存在的)位置的字符串:
MyMemo.Lines[2] := 'MyString'
。最后,你可以 * 插入 * 任何你想要的地方:
MyMemo.Lines.Insert(2, 'MyString')
x9ybnkn64#
这很简单(NGLN的答案):