Author: Tomas Rutkauskas
How to get the caret position of a Memo or RichEdit control? I don't mean any
character position expressed in row and column, I mean it in pixels!
Answer:
You get the caret position in pixels (client relative) from an edit, memo or
richedit control by sending it a EM_POSFROMCHAR message. The message parameters are
different for a TRichEdit and TMemo/ TEdit.
1 {TRichEdit}2 3 var4 pt: TPoint;
5 begin6 with richedit1 do7 begin8 Perform(messages.EM_POSFROMCHAR, WPARAM(@pt), selstart);
9 label1.caption := Format('(%d, %d)', [pt.x, pt.y]);
10 end;
11 end;
12 13 {TMemo and TEdit}14 15 var16 r: LongInt;
17 begin18 with memo1 do19 begin20 r := Perform(messages.EM_POSFROMCHAR, selstart, 0);
21 if r >= 0 then22 begin23 label1.caption := IntToStr(HiWord(r));
24 label2.caption := IntToStr(LoWord(r));
25 end;
26 end;
27 end;
The win32.hlp entries for this message are really messed up, on older versions they only showed the memo variant, on newer (e.g. the one that comes with D5) they show only the richedit variant.