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

You can add a string to the bottom of the ".Lines" member: MyMemo.Lines.add('MyString').

You can also replace a string in any (already existing) position in "Lines" you want: MyMemo.Lines[2] := 'MyString'.

Finally, you can insert anywhere you want: MyMemo.Lines.Insert(2, 'MyString')

于 2014-05-17T07:15:48.967 に答える