Difference between revisions of "GridPrinter"

From Lazarus wiki
Jump to navigationJump to search
(Getting Started)
Line 12: Line 12:
  
 
==Download and Installation==
 
==Download and Installation==
====Release version====
+
===Release version===
The package is made available for installation by the Online-Package-Manager. Additionally, a zip file with the most recent release version can be found at [https://sourceforge.net/projects/lazarus-ccr/files/GridPrinter/ Lazarus CCR at SourceForge]; unzip the file into any folder.
+
The package is made available for installation by the Online-Package-Manager. Additionally, a zip file with the most recent release version can be found at [https://sourceforge.net/projects/lazarus-ccr/files/GridPrinter/ Lazarus CCR at SourceForge]; unzip the file into any folder; load the file ''gridprinterpkg.lpk'' into Lazarus and click ''Use'' > ''Install''.
  
====Development version====
+
===Development version===
The current development version is hosted at [https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/gridprinter/ Lazarus CCR]. Use SVN to check it out, or download the zipped snapshot from this page.
+
The current development version is hosted at [https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/gridprinter/ Lazarus CCR]. Use SVN to check it out, or download the zipped snapshot from this page. Install by loading the ''gridprinterpkg.lpk'' package file into Lazarus and clicking ''Use'' > ''Install''.
 +
 
 +
==Getting Started==
 +
====How can I print a <tt>StringGrid</tt>?====
 +
Nothing easier when you have the GridPrinter package...
 +
* Drop a <tt>TGridPrinter</tt> component on the form.
 +
* Link its <tt>Grid</tt> property to the StringGrid that you want to print.
 +
* In the <tt>OnClick</tt> handler of the button or menu item that is supposed to start the print-out, call the <tt>Print</tt> method of the <tt>GridPrinter</tt>. That's all.
 +
<syntaxhighlight lang=Pascal>
 +
procedure TForm1.PrintButtonClick(Sender: TObject);
 +
begin
 +
  GridPrinter1.Print;
 +
end;</syntaxhighlight>
 +
 
 +
====How can I see a preview of the print-out before the printer starts to waste paper?====
 +
* Drop also a <tt>TGridPrintPreviewDialog</tt> on the form.
 +
* Links its <tt>GridPrinter</tt> property to the GridPrinter instance that you had added in the previous exercise.
 +
* In the <tt>OnClick</tt> handler of the button or menu item that is supposed to show the preview, call the <tt>Execute</tt> method of the <tt>GridPrintPreviewDialog</tt>.
 +
* In the preview dialog you can scroll through the pages, zoom to various levels, change page orientation, adjust the page margins by dragging with the mouse, or add a header and/or footer. Of course, you can also print here, too.
 +
<syntaxhighlight lang=Pascal>
 +
procedure TForm1.PrintPreviewButtonClick(Sender: TObject);
 +
begin
 +
  GridPrintPreviewDialog1.Execute;
 +
end;</syntaxhighlight>
 +
 
 +
====I have several printers and want to make sure that the print-out goes to the right one====
 +
<tt>TGridPrinter</tt> has a property <tt>ShowPrintDialog</tt> which controls whether a print dialog should be displayed before printing starts. The kind of dialog is determined by the following options:
 +
* <tt>gpdNone</tt>: no print dialog
 +
* <tt>gpdPageSetup</tt>: shows a <tt>TPageSetupDialog</tt>. Here you can select the paper size and set page orientation as well as page margins.
 +
* <tt>gpdPrintDialog</tt>: shows a <tt>TPrintDialog</tt> where you can select the printer, the number of copies and the range of printed pages.
 +
* <tt>gpdPrinterSetup</tt>: shows <tt>TPrinterSetupDialog</tt>. Here you can select the printer, the paper size and the page orientation.
 +
 
 +
====Can individual cell formatting by the <tt>OnPrepareCanvas</tt> event be applied to the print-out?====
 +
Yes. The <tt>TGridPrinter</tt> fires an <tt>OnPrepareCanvas</tt> event, too. Both even can share the same event handler. You only should be careful to use the correct <tt>Canvas</tt> when handling the event: When the <tt>Sender</tt> of the event is the grid, you must refer to the grid's <tt>Canvas</tt>, and when the <tt>Sender</tt> is the <tt>GridPrinter</tt> you must use that <tt>Canvas</tt> exposed by the <tt>GridPrinter</tt>; during printing this is the printer's <tt>Canvas</tt>, and when the preview is painted it is the <tt>Canvas</tt> of a temporary <tt>TBitmap</tt>.
 +
 
 +
Let's assume that your <tt>OnPrepareCanvas</tt> handler is supposed to paint the column title in bold type face. This could be done by the following code. The procedure must be assigned to the <tt>OnPrepareCanvas</tt> of both grid and <tt>GridPrinter</tt>.
 +
<syntaxhighlight lang=Pascal>
 +
procedure TForm1.PrepareCanvasHandler(Sender: TObject; ACol, ARow: Integer; AState: TGridDrawState);
 +
var
 +
  lCanvas: TCanvas;
 +
begin
 +
  if Sender = StringGrid1 then
 +
    lCanvas := StringGrid1.Canvas
 +
  else
 +
  if Sender = GridPrinter1 then
 +
    lCanvas := GridPrinter1.Canvas
 +
  else
 +
    raise Exception.Create('Unknown sender of OnPrepareCanvas.');
 +
 
 +
  if ARow < StringGrid1.FixedRows then
 +
    lCanvas.Font.Style := [fsBold];
 +
end;
 +
</syntaxhighlight>

Revision as of 01:55, 10 November 2022

Template:GridPrinter

About

GridPrinter

TGridPrinter is a component to simplify printing of string grids or other descendants of TCustomGrid. It is bundled together with a ready-made print preview dialog, TGridPrintPreviewDialog, as well as standard actions to trigger printing or to show the preview dialog without writing a single line of code.

Author

Werner Pamler

License

Modified LGPL-2 (with linking exception, like Lazarus LCL)

Download and Installation

Release version

The package is made available for installation by the Online-Package-Manager. Additionally, a zip file with the most recent release version can be found at Lazarus CCR at SourceForge; unzip the file into any folder; load the file gridprinterpkg.lpk into Lazarus and click Use > Install.

Development version

The current development version is hosted at Lazarus CCR. Use SVN to check it out, or download the zipped snapshot from this page. Install by loading the gridprinterpkg.lpk package file into Lazarus and clicking Use > Install.

Getting Started

How can I print a StringGrid?

Nothing easier when you have the GridPrinter package...

  • Drop a TGridPrinter component on the form.
  • Link its Grid property to the StringGrid that you want to print.
  • In the OnClick handler of the button or menu item that is supposed to start the print-out, call the Print method of the GridPrinter. That's all.
procedure TForm1.PrintButtonClick(Sender: TObject);
begin
  GridPrinter1.Print;
end;

How can I see a preview of the print-out before the printer starts to waste paper?

  • Drop also a TGridPrintPreviewDialog on the form.
  • Links its GridPrinter property to the GridPrinter instance that you had added in the previous exercise.
  • In the OnClick handler of the button or menu item that is supposed to show the preview, call the Execute method of the GridPrintPreviewDialog.
  • In the preview dialog you can scroll through the pages, zoom to various levels, change page orientation, adjust the page margins by dragging with the mouse, or add a header and/or footer. Of course, you can also print here, too.
procedure TForm1.PrintPreviewButtonClick(Sender: TObject);
begin
  GridPrintPreviewDialog1.Execute;
end;

I have several printers and want to make sure that the print-out goes to the right one

TGridPrinter has a property ShowPrintDialog which controls whether a print dialog should be displayed before printing starts. The kind of dialog is determined by the following options:

  • gpdNone: no print dialog
  • gpdPageSetup: shows a TPageSetupDialog. Here you can select the paper size and set page orientation as well as page margins.
  • gpdPrintDialog: shows a TPrintDialog where you can select the printer, the number of copies and the range of printed pages.
  • gpdPrinterSetup: shows TPrinterSetupDialog. Here you can select the printer, the paper size and the page orientation.

Can individual cell formatting by the OnPrepareCanvas event be applied to the print-out?

Yes. The TGridPrinter fires an OnPrepareCanvas event, too. Both even can share the same event handler. You only should be careful to use the correct Canvas when handling the event: When the Sender of the event is the grid, you must refer to the grid's Canvas, and when the Sender is the GridPrinter you must use that Canvas exposed by the GridPrinter; during printing this is the printer's Canvas, and when the preview is painted it is the Canvas of a temporary TBitmap.

Let's assume that your OnPrepareCanvas handler is supposed to paint the column title in bold type face. This could be done by the following code. The procedure must be assigned to the OnPrepareCanvas of both grid and GridPrinter.

procedure TForm1.PrepareCanvasHandler(Sender: TObject; ACol, ARow: Integer; AState: TGridDrawState);
var
  lCanvas: TCanvas;
begin
  if Sender = StringGrid1 then
    lCanvas := StringGrid1.Canvas
  else
  if Sender = GridPrinter1 then
    lCanvas := GridPrinter1.Canvas
  else
    raise Exception.Create('Unknown sender of OnPrepareCanvas.');

  if ARow < StringGrid1.FixedRows then
    lCanvas.Font.Style := [fsBold];
end;