GET CURRENT CARAT POSITION
in Memo/RichEdit component
in order to paste something from the Clipboard at the current cursor
position we need to know the character pos of the cursor. Simple as it
sounds you can scour the Windows API 'Help' (in quotes) looking for this
for a LONG TIME before you suss it. (Qv next line of code). So we take
the start position of our newly inserted (and SELECTED) text to be the
current cursor position, and we take the endPos of this newly-inserted
(and SELECTED) text to be the length of the input text BUT finding the
length of that 'insert' text is not straightforward if we want to keep
modules loosely coupled and ONLY use the clipboard fro transferring
data from one module to another. One solution, as shown below, is to
use a 'hidden memo' component
SendMessage(Editor.Handle, EM_GETSEL, LongInt(@startPos), LongInt(@endPos));
Note also that the way we set things up here is that IF TEXT IS SELECTED
IN THE MEMO/RICHEDIT we are dealing with here then that selection gets
REPLACED by whatever
we insert...}
procedure GetCaratPos;
var
startPos, endPos: LongInt;
begin
SendMessage(Editor.Handle, EM_GETSEL, LongInt(@startPos), LongInt(@endPos));
end;
example code to show use of a hidden memo to ascertain the length of the
text we are inserting...
Editor.PasteFromClipboard;
HiddenMemo.PasteFromClipboard;
lastLine := (HiddenMemo.Lines.Count - 1);
lastLineLen := Length(HiddenMemo.Lines[lastLine]);
insertLength := SendMessage(HiddenMemo.Handle, EM_LINEINDEX, lastLine, 0);
insertLength := insertLength + lastLineLen;
Editor.SelStart := startPosition;
Editor.SelLength := insertLength;