Board index » delphi » Row-Col in TMemo

Row-Col in TMemo


2003-12-15 09:08:37 AM
delphi152
How can I find out the row and column the cursor is at on a TMemo in
real time?
Jim R
 
 

Re:Row-Col in TMemo

Jim Reiley writes:
Quote
How can I find out the row and column the cursor is at on a TMemo in
real time?
Use the CaretPos property.
--
Rob
 

Re:Row-Col in TMemo

Jim Reiley <XXXX@XXXXX.COM>writes:
Quote
How can I find out the row and column the cursor is at on a TMemo in
real time?
There's a caret (the insertion point) and a cursor (the mouse
position) - which are two different things.
The caret position (in characters, not row / columns) when no text is
selected can be obtained from the SelStart property. When text is
selected, SelLength will be non-zero and the caret can be at either
the SelStart value or SelStart + SelLength position, depending on the
direction in which the user selected the text. You can use the
EM_GETLINEFROMCHAR to get the actual line number based on the
character position. But note that soft line breaks, variable width
fonts make this sort of questionably useful.
The cursor (mouse) position can be convered to a row/column with an
EM_CHARPOS message. Again, this may or may not be useful.
If this doesn't help, please followup telling us exactly what you want
to do - the possibilities are too many to cover.
Good luck.
Kurt
 

Re:Row-Col in TMemo

Hi,
I need to know the line(row) and the position(col) within the line, and I
need to be able to set the cursor to these positions. This is in a
straight text memo, no formatting or font changes or anything like that.
If I can not get the row position, then I can live with the line number. The
memo is actually a richedit type memo. I'd use a regular memo except
I need to be able to do overwrites as well as inserts.
.
Thanks,
Jim R
"Kurt Barthelmess (TeamB)" writes:
Quote
Jim Reiley <XXXX@XXXXX.COM>writes:

>How can I find out the row and column the cursor is at on a TMemo in
>real time?

There's a caret (the insertion point) and a cursor (the mouse
position) - which are two different things.

The caret position (in characters, not row / columns) when no text is
selected can be obtained from the SelStart property. When text is
selected, SelLength will be non-zero and the caret can be at either
the SelStart value or SelStart + SelLength position, depending on the
direction in which the user selected the text. You can use the
EM_GETLINEFROMCHAR to get the actual line number based on the
character position. But note that soft line breaks, variable width
fonts make this sort of questionably useful.

The cursor (mouse) position can be convered to a row/column with an
EM_CHARPOS message. Again, this may or may not be useful.

If this doesn't help, please followup telling us exactly what you want
to do - the possibilities are too many to cover.

Good luck.

Kurt
 

Re:Row-Col in TMemo

Jim Reiley <XXXX@XXXXX.COM>writes:
Quote
I need to know the line(row) and the position(col) within the line, and I
need to be able to set the cursor to these positions. This is in a
straight text memo, no formatting or font changes or anything like that.
If I can not get the row position, then I can live with the line number. The
memo is actually a richedit type memo. I'd use a regular memo except
I need to be able to do overwrites as well as inserts.
ok, I am taking you literally here - convert the caret location into a
cursor position and move the cursor there:
var
Pt: TPoint;
...
{ Get the client relative caret coordinates }
SendMessage(RichEdit1.Handle, EM_POSFROMCHAR,
Longint(@Pt), RichEdit1.SelStart);
{ Convert to screen coordinates }
Pt := RichEdit1.ClientToScreen(Pt);
{ Move the cursor }
SetCursorPos(Pt.X, Pt.Y);
You'll notice that the cursor (probably an "I" bar) looks to be half
way above the top of the line - it is. that is because the coordinate
reported by the RichEdit is the top of the line and the "hot spot" of
the cursor is in the middle of the "I" bar. You can add half of the
font height to the Y coordinate (Pt.Y) in the call to SetCursorPos to
make it look nicer.
Good luck.
Kurt
 

Re:Row-Col in TMemo

Thanks.
Jim
"Kurt Barthelmess (TeamB)" writes:
Quote
Jim Reiley <XXXX@XXXXX.COM>writes:

>I need to know the line(row) and the position(col) within the line, and I
>need to be able to set the cursor to these positions. This is in a
>straight text memo, no formatting or font changes or anything like that.
>If I can not get the row position, then I can live with the line number. The
>memo is actually a richedit type memo. I'd use a regular memo except
>I need to be able to do overwrites as well as inserts.

ok, I am taking you literally here - convert the caret location into a
cursor position and move the cursor there:

var
Pt: TPoint;
...
{ Get the client relative caret coordinates }
SendMessage(RichEdit1.Handle, EM_POSFROMCHAR,
Longint(@Pt), RichEdit1.SelStart);
{ Convert to screen coordinates }
Pt := RichEdit1.ClientToScreen(Pt);
{ Move the cursor }
SetCursorPos(Pt.X, Pt.Y);

You'll notice that the cursor (probably an "I" bar) looks to be half
way above the top of the line - it is. that is because the coordinate
reported by the RichEdit is the top of the line and the "hot spot" of
the cursor is in the middle of the "I" bar. You can add half of the
font height to the Y coordinate (Pt.Y) in the call to SetCursorPos to
make it look nicer.

Good luck.

Kurt