[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
Here is a PrintGrid procedure which prints a StringGrid and a test program for the procedure. Printgrid is easier to use than our PrintPreview demo which appears elsewhere on DFF.
Background & Techniques
A fellow senior (as in older) Delphi programmer from Belgium wrote recently for help in printing some specialized Bingo cards, multiple cards on a page. The TForm.Print procedure only prints the window that is displayed and prints everything on the form, and the PrintPreview was more complicated than necessary. The idea of PrintPreview was to recreate the grid in a metafile which could be redrawn to any scale without losing resolution. It works well, but is somewhat complicated to use.
PrintGrid included here is a compromise approach using the "StretchDraw" method of TCanvas to reproduce the grid screen image onto the printer canvas.
Call PrintGrid with parameters identifying the grid to print, plus left, top, width, and height of the image as it is to appear on the printed page. For example, to print in a 6 inch by 3 inch rectangle with one inch margin on a 600 dpi printer: PrintGrid(Stringgrid1, 600,600, 3600, 1800);
Some degradation will of text occur if a small grid is printed in a large rectangular area but the quality is quite acceptable for the test program grid using 24 point text in 50x50 pixel cells. The height to width aspect ratio of the grid on the screen is maintained for printing by using the smaller of the X or Y scaling factors.
The calling program may use the provided XOffset and YOffset
functions to get the physical offsets to the areas where printing is allowed.
Minimum horizontal coordinate is Xoffset and maximum horizontal coordinate
is Printer.Pagewidth - Xoffset. The minimum vertical coordinate is YOffset and
the maximum printable pixel value is Printer.Pageheight - YOffset. If
passed size parameters violate these boundaries, PrintGrid will adjust them so
the entire grid will be displayed.
The TStringGrid Canvas excludes the left and top borders, the top left corner of the top left cell is at (0,0). The grind image size is defined by properties GridWidth and GridHeight is shifted 2 pixels right and down into a temporary device independent bitmap (DIB). PrintGrid then inserts top and left border lines with color=ClGray and 2 pixel width into this bitmap which becomes the source for a Stretchdraw operation to the Printer canvas.
In order to save paper while getting the above to work, I added a Debug conditional define variable and used a TImage bitmap to view the placement of the grid and border instead of printing when Debug was defined. The conditional compiler directives $IFDEF, $IFNDEF, $ELSE, and $ENDIF control which version fo the code gets generated. The Project/Options/Conditionals menu can be used to define Debug. The Projects/Build option must be used to force recompiling both units (U_Printgridtest and UPrintgrid) when the Debug is defined or removed.
Suggestions for Further Explorations
Copyright © 2000-2017, Gary Darby All rights reserved.