How to find first visible item in editor?

General TRichView support forum. Please post your questions here
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How to find first visible item in editor?

Post 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;
dakota
Posts: 35
Joined: Tue Jan 13, 2009 11:25 pm

Re: How to find first visible item in editor?

Post by dakota »

Excellent! Thank you Sergey.

Dale
Post Reply