How to get the physical caret position in a TMemo, TEdit or TRichEdit #103

Question
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!

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.

TRichEdit:

var
  pt: TPoint;
begin
  with richedit1 do
  begin
    Perform(messages.EM_POSFROMCHAR, WPARAM(@pt), selstart);
    label1.caption := Format('(%d, %d)', [pt.x, pt.y]);
  end;
end;

TMemo and TEdit

var
  r: LongInt;
begin
  with memo1 do
  begin
    r := Perform(messages.EM_POSFROMCHAR, selstart, 0);
    if r >= 0 then
    begin
      label1.caption := IntToStr(HiWord(r));
      label2.caption := IntToStr(LoWord(r));
    end;  
  end;
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.

Original resource: The Delphi Pool
Author: Peter Below
Added: 2009/09/14
Last updated: 2009/09/14