PrintGrid - StringGrid Printing

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

Problem Description

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.

It is also the callers responsibility to establish any printer setup parameters and to issue BeginDoc before calling and EndDoc calls after all calls to PrintGrid.
 

Programmer Notes

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.      

Running/Exploring the Program 

bulletDownload source
bulletDownload  executable

Suggestions for Further Explorations

????
   
   
Original:  August 10, 2008

Modified:  May 15, 2018

 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.