Discussion:
How do I get the active "line" in a TRichEdit?
(too old to reply)
jodleren
2010-06-17 20:55:15 UTC
Permalink
As it says:

I want to get Lines[itemindex] - but in a TRichEdit;

eventually the cursor position in a document?

any ideas?
Jamie
2010-06-17 22:52:11 UTC
Permalink
Post by jodleren
I want to get Lines[itemindex] - but in a TRichEdit;
eventually the cursor position in a document?
any ideas?
What's wrong with using the existing "LINES" property ?
Maarten Wiltink
2010-06-18 07:19:50 UTC
Permalink
Post by Jamie
Post by jodleren
I want to get Lines[itemindex] - but in a TRichEdit;
eventually the cursor position in a document?
any ideas?
What's wrong with using the existing "LINES" property ?
That it misses the 'ItemIndex' bit. He wants the current line, not
all of them.

TRichEdit has a SelStart property which is like the cursor position,
but one-dimensional. Sending it the right Windows message might
return a line number also. A quick Google only yields EM_POSFROMCHAR
but more helpful should be nearby its definition.

Groetjes,
Maarten Wiltink
a***@aol.com
2010-06-18 10:53:20 UTC
Permalink
Post by jodleren
I want to get Lines[itemindex] - but in a TRichEdit;
eventually the cursor position in a document?
any ideas?
Selstart will give you the position in Chars from start. But the
cursor may not be at the start of a line.

So use TRichEdit.Perform(EM_POSFROMCHAR, set the X of the position to
0, use EM_CHARFROMPOSN and set SelStart to that value.

Set X of the position to the width of TRichEdit's ClientWidth. Then
use EM_CHARFROMPOS to get where the end of the line is and set
SelLength to the difference between the beginnind & end of the line.

Then use SelText to get the string of the line.

Pop all that in a function with the RichEdit & SelStart as parameters
and returning the SelText.

Robert is your mother's brother <g>.

Alan Lloyd
a***@aol.com
2010-06-18 19:00:53 UTC
Permalink
As a little light relief for me <g> here is my solution. It's over
commented for your benefit.

function GetRichEditLine(RE : TRichEdit) : string;
{gets text lne of cursor position or of selection start}
var
aSelPos, aSelLength, LineStart, LineEnd : integer;
LinePoint : TPoint;
begin
{stop display updating}
RE.Lines.BeginUpdate;
{get char poition of cursor}
aSelPos := RE.SelStart;
{get current selected length}
aSelLength := RE.SelLength;
RE.Perform(EM_POSFROMCHAR, integer(@LinePoint), aSelPos);
{set X to start of line & set SelStart there}
LinePoint.X := 0;
LineStart := RE.Perform(EM_CHARFROMPOS, 0, integer(@LinePoint));
RE.SelStart := LineStart;
{get char position of end of line}
LinePoint.X := RE.ClientWidth;
LineEnd := RE.Perform(EM_CHARFROMPOS, 0, integer(@LinePoint));
{set selected length}
RE.SelLength := LineEnd - RE.SelStart;
Result := RE.SelText;
{restore cursor position & selected length}
RE.SelStart := aSelPos;
RE.SelLength := aSelLength;
{restore display updating}
RE.Lines.EndUpdate;
end;

Alan Lloyd

Loading...