[Example][ScaleRichView] How to draw line numbers
Posted: Sun Feb 01, 2015 9:28 pm
Update: this code is not needed in new version of ScaleRichView, because this is a standard feature, see TSRichViewEdit.LineNumberProperty
Assign this code to TSRichViewEdit's OnPaintPage event.
The code draws line numbers at the left margin.
Numbering is restarted on each page.
Screenshot:
Assign this code to TSRichViewEdit's OnPaintPage event.
The code draws line numbers at the left margin.
Numbering is restarted on each page.
Code: Select all
procedure TForm1.SRichViewEdit1PaintPage(Sender: TObject; PageNo: Integer;
PageRect, R: TRect; Canvas: TCanvas; Prepaint, Printing: Boolean);
const
PageNumberDX = 5; // distance between line numbers and text
var
i: Integer;
FirstItemNo, LastItemNo, Offs, ItemPart: Integer;
FirstPageNo, LastPageNo : Integer;
ItemRect: TRect;
srv: TSRichViewEdit;
TextHeight, LineNumber, Left, Top, LeftMargin: Integer;
begin
// line number font
Canvas.Font.Name := 'Tahoma';
Canvas.Font.Size := 6;
Canvas.Font.Style := [];
Canvas.Font.Color := clBtnShadow;
Canvas.Brush.Style := bsClear;
TextHeight := Canvas.TextHeight('0');
srv := Sender as TSRichViewEdit;
srv.GetPageStartItemNo(PageNo, FirstItemNo, Offs);
srv.GetPageLastItemNo(PageNo, LastItemNo, Offs);
LineNumber := 1;
LeftMargin := srv.GetLeftMargin100Pix(PageNo);
for i := FirstItemNo to LastItemNo do begin
if srv.RichViewEdit.GetItemStyle(i)=rvsTable then
continue;
ItemPart := 0;
while srv.GetItemBounds100(
srv.RichViewEdit.RVData, i, ItemRect,
FirstPageNo, LastPageNo, ItemPart) do begin
if (FirstPageNo = PageNo) and
((ItemPart>0) or srv.RichViewEdit.IsFromNewLine(i)) then begin
Left := PageRect.Left + LeftMargin - PageNumberDX -
Canvas.TextWidth(IntToStr(LineNumber));
Top := PageRect.Top + (ItemRect.Top+ItemRect.Bottom - TextHeight) div 2;
Canvas.TextOut(Left, Top, IntToStr(LineNumber));
inc(LineNumber);
end;
inc(ItemPart);
end;
end;
end;