Recent

Author Topic: How to print a DrawGrid  (Read 1149 times)

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 610
    • Double Dummy Solver - free download
How to print a DrawGrid
« on: December 01, 2023, 06:42:08 pm »
The TDrawGrid looks good on my monitor but I haven't figured out how to print it on the printer canvas. I think my biggest problem is getting aRect, although there's a good chance I'm going about this all wrong.

Code: Pascal  [Select][+][-]
  1. procedure TMyForm.PrintClick(Sender: TObject);
  2. var
  3.   c, r: integer;
  4.   aRect: TRect;
  5. begin
  6.   if PrinterSetup.Execute then
  7.   begin
  8.     Printer.BeginDoc;
  9.     for c := 0 to Grid.ColCount - 1 do
  10.       for r := 0 to Grid.RowCount - 1 do
  11.       begin
  12.         aRect := Grid.CellRect(c,r);
  13.         DrawCelltoCanvas(Printer.Canvas, c, r, aRect, []);
  14.       end; //  for c & r
  15.     Printer.EndDoc;
  16.   end; // printer executed
  17. end;    
Lazarus 3.6 FPC 3.2.2 x86_64-win64-win32/win64

paweld

  • Hero Member
  • *****
  • Posts: 1268
Re: How to print a DrawGrid
« Reply #1 on: December 01, 2023, 07:08:06 pm »
Best regards / Pozdrawiam
paweld

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 610
    • Double Dummy Solver - free download
Re: How to print a DrawGrid
« Reply #2 on: December 04, 2023, 04:34:00 pm »
I haven't been able to figure out how to use the GridPrinter with a TDrawGrid. The (quite good) documentation doesn't mention use with a TDrawGrid. What am I missing?
Lazarus 3.6 FPC 3.2.2 x86_64-win64-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 12464
Re: How to print a DrawGrid
« Reply #3 on: December 04, 2023, 04:42:19 pm »
I cannot remember every detail of that component, but you probably must provide a handler for the OnGetCellText event in which you tell the printer the text to be printed in a specific cell.

Or is it non-text printing you are after? In this case provide a small demo project showing the grid to be printed.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 610
    • Double Dummy Solver - free download
Re: How to print a DrawGrid
« Reply #4 on: December 04, 2023, 05:37:26 pm »
I am printing text but I also need to control the font name and size in each cell.

In my DrawGrid onDrawCell I use my own FillRect and TextDraw, etc.  I discovered that the GridPrinter needs the OnPrintCell method. Thanks wp for getting me to look further under the hood.
Lazarus 3.6 FPC 3.2.2 x86_64-win64-win32/win64

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 610
    • Double Dummy Solver - free download
Re: How to print a DrawGrid
« Reply #5 on: December 04, 2023, 10:31:10 pm »
One last point.  How do I trigger a new page. Not needed when viewing the Grid but needed when printing. I can't use Printer.NewPage.
Lazarus 3.6 FPC 3.2.2 x86_64-win64-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 12464
Re: How to print a DrawGrid
« Reply #6 on: December 04, 2023, 11:29:20 pm »
Page breaks are determined by the component, when a row or column does not fit on the current page any more. IIRC, there is no way to manually trigger a page break. This does not mean that it's not possible, but it certainly will require some work.

 

TinyPortal © 2005-2018