Board index » delphi » Caret position to correct pixel position

Caret position to correct pixel position

Hi

In a TRichEdit/TMemo the caret position is the position in characters and
rows. How can I convert this into real pixel position?

Thanks
Jnos

 

Re:Caret position to correct pixel position


Thus spake Jnos Schmidt:

Quote
> Hi

> In a TRichEdit/TMemo the caret position is the position in characters and
> rows. How can I convert this into real pixel position?

> Thanks
> Jnos

Read the Win32 help that comes with Delphi, under Edit Controls.

var
  aPoint: POINT;

SendMessage( Memo.Handle, EM_POSFROMCHAR, aPoint, CharPos );

--- from the help

An application sends the EM_POSFROMCHAR message to retrieve the
coordinates of the specified character in an edit control.

EM_POSFROMCHAR  
wParam = (LPPOINT) lpPoint;        // address of structure
                                   // receiving character position
lParam = (LPARAM) wCharIndex;      // zero-based index of character

--
aaronl at consultant dot com
http://homepages.visp.co.nz/~aaronlawrence
..Gross Ignorance: 144 times worse than ordinary ignorance.

Re:Caret position to correct pixel position


Quote
In article <3dcfbed...@newsgroups.borland.com>, Jnos Schmidt wrote:
> I have D6 and I have looked at the Win32 help. I have only seen that it
> needs a POINT structure, which is not compatible with the Delphi TPoint.

TPoint *is* the Delphi alias for the API POINT structure.

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 richedit and tmemo/tedit.

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;

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.

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Re:Caret position to correct pixel position


Hi Ken

That was it! I didn't set the typecast wParam(...). I have only tried by
using @Pt. That was naturally not compatible.

Thanks again
Jnos

Quote
"Ken White" <kwh...@adpsi.com> wrote in message

news:3dd00633$1@newsgroups.borland.com...
Quote
> Janos,

> > I have D6 and I have looked at the Win32 help. I have only seen that
> it
> > needs a POINT structure, which is not compatible with the Delphi
> TPoint. It
> > is an integer and I don't know how to convert an integer into a
> TPoint. I
> > didn't found a type 'POINT' in Delphi. Can you give a hint?

> Wrong. :-) What it needs is a pointer to a Point structure (record
> in Delphi); that Point structure is FULLY compatible with Delphi's
> TPoint.

> var
>   Pt: TPoint;

> SendMessage( Memo1.Handle, EM_POSFROMCHAR,
>   wParam( @Pt ),   CharPos );

> Ken
> ---
> Ken White
> kwh...@adpsi.com

> Clipper Functions for Delphi and C++ Builder
> http://www.adpsi.com

Re:Caret position to correct pixel position


Thanks Peter, it's working well.

Jnos

Other Threads