0

我想在delphi中将字符串添加到备注编辑的指定位置,我该怎么做?我的意思是我想知道鼠标光标在 TMemo 中的位置,然后在这个位置添加一个字符串。那可能吗?

4

3 回答 3

8

您可以使用EM_CHARFROMPOS来确定鼠标光标指向的字符位置:

var
  Pt: TPoint;
  Pos: Integer;
begin
 Pt := Memo1.ScreenToClient(Mouse.CursorPos);
 if (Pt.X >= 0) and (Pt.Y >= 0) then begin
   Pos := LoWord(Memo1.Perform(EM_CHARFROMPOS, 0, MakeLong(Pt.x, Pt.Y)));
   Memo1.SelLength := 0;
   Memo1.SelStart := Pos;
   Memo1.SelText := '.. insert here ..';
 end;
end;
于 2014-05-17T10:16:57.710 回答
4

如果要将字符串放在备忘录的插入符号位置,可以使用以下代码:

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;
于 2014-05-17T09:32:04.080 回答
0

您可以在“.Lines”成员的底部添加MyMemo.Lines.add('MyString')一个字符串: 。

您还可以替换您想要的“行”中任何(已经存在的)位置的字符串MyMemo.Lines[2] := 'MyString'

最后,你可以在任何你想要的地方插入:MyMemo.Lines.Insert(2, 'MyString')

于 2014-05-17T07:15:48.967 回答