Keeping the caret at the end of text in a rich edit
I am writing an editor in Delphi (2009) using a TRichEdit component. The editor is append-only, in the sense that the caret must be at the end at all times, while maintaining the ability to copy using the mouse from elsewhere in the component.
The way it is working at the moment is by moving the caret to the end whenever something is written, but is it possible to make the caret not follow the mouse when clicking at ot开发者_开发问答her parts of the text?
No, it is not possible. You have to move the caret to the end when the user types something.
No. The caret must move in order for the user to make selections with the mouse or keyboard. You will have to move the caret to the end each time you insert new text. You should probably keep and restore the user's current caret position during each insertion as well, eg:
procedure TForm.AppendText(const S: String);
var
OldCharRange, NewCharRange: TCharRange;
begin
SendMessage(RichEdit1.Handle, EM_EXGETSEL, 0, LParam(@OldCharRange));
try
NewCharRange.cpMin := RichEdit1.GetTextLen;
NewCharRange.cpMax := NewCharRange.cpMin;
SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, LParam(@NewCharRange));
RichEdit1.SelText := S;
finally
SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, LParam(@OldCharRange));
end;
end;
精彩评论