Page 2 of 2

Re: How to find first visible item in editor?

Posted: Sat Jul 22, 2023 12:49 pm
by Sergey Tkachenko
Here is my solution.
It draws line numbers in the editor itself, in OnPaint event.
But you can easily modify it to draw next to the editor.

This code supports text items placed on multiple lines (they are enumerated using ItemPart variable).

Code: Select all

procedure TForm3.RichViewEdit1Paint(Sender: TCustomRichView; ACanvas: TCanvas;
  Prepaint: Boolean);
var
  ItemNo, ItemPart, LineNo, TextHeight: Integer;
  IsFromNewLine: Boolean;
  R: TRect;
  Pt: TPoint;
begin
  if Prepaint then
    exit;
  ItemNo := Sender.FirstItemVisible;
  if ItemNo < 0 then
    exit;
  ACanvas.Font.Name := 'Tahoma';
  ACanvas.Font.Size := 8;
  ACanvas.Font.Style := [];
  ACanvas.Font.Color := clNavy;
  ACanvas.Brush.Style := bsSolid;
  ACanvas.Brush.Color := clWhite;
  TextHeight := ACanvas.TextHeight('0');
  SetTextAlign(ACanvas.Handle, TA_LEFT or TA_TOP);
  LineNo := Sender.GetLineNo(ItemNo, Sender.GetOffsBeforeItem(ItemNo));
  for ItemNo := ItemNo to Sender.LastItemVisible do
  begin
    ItemPart := 0;
    IsFromNewLine := Sender.IsFromNewLine(ItemNo);
    while Sender.GetItemCoordsEx(Sender.RVData, ItemNo, ItemPart, False, R) do
    begin
      if IsFromNewLine then
      begin
        Pt.X := 0;
        Pt.Y := (R.Top + R.Bottom - TextHeight) div 2;
        Pt := Sender.DocumentToClient(Pt);
        ACanvas.TextOut(Pt.X, Pt.Y, IntToStr(LineNo));
        inc(LineNo);
      end;
      IsFromNewLine := True;
      inc(ItemPart);
    end;
  end;

end;

Re: How to find first visible item in editor?

Posted: Mon Jul 24, 2023 3:25 pm
by dakota
Excellent! Thank you Sergey.

Dale