Here's a "Print Preview" unit which will let you
preview and print memo and stringlist data. As usual, the motivation
was the requirement to preview and print these two component types in
another program (the Crossword generator).
The unit is based on one among several found at programmer's heaven.com. It was simple enough to get a handle on and flexible enough to let me add the missing StringGrid preview function. The author is nearly anonymous (no name just an invalid email address, rpetersn@usit.net). So Ryan, if you ever run across this, drop me a line and I'll be glad to give you a better credit write-up.
The unit handles multiple pages, scaling of the preview, and the ability to save preview pages and reload them at a later date. There is much you can learn about metafiles and how they work by studying the code, but not too much that you must learn in order to to use it.
The unit uses metafiles, one per page, as the ideal mechanism for accurate scaling of the same data to different devices. A little about metafiles from Microsoft:
"Enhanced metafiles provide true device independence. You can think of the picture stored in an enhanced metafile as a "snapshot" of the video display taken at a particular moment. This "snapshot" maintains its dimensions no matter where it appears on a printer, a plotter, the desktop, or in the client area of any application.
You can use enhanced metafiles to store a picture created by using the GDI functions (including new path and transformation functions). Because the enhanced metafile format is standardized, pictures that are stored in this format can be copied from one application to another; and, because the pictures are truly device independent, they are guaranteed to maintain their shape and proportion on any output device."
When a metafile is being built, data is written to a special "device context", a structure that defines the characteristics of a particular device. Borland defines a TCanvas object type to represent a device context and a descendant TMetafileCanvas type to describe the special device context associated with metafiles. Data is written to either type using the same TCanvas methods. More significantly, when a TMetafileCanvas is freed, the data written to it is encoded in the associated metafile. The metafile can then it can be saved and/or "played-back" to any specific device type using the "Stretchdraw" command for that device's canvas.
The main methods from the PrintPreview unit are:
Parameters:
Parameters are:
The string grid in the demo illustrates use of an OnDrawCell exit to display any cell with a valid numeric value in red and
underlined. (The grid is initially filled with random numbers and letters. You can double click on any white cell
to modify the data.)
On page 1 of the output, the demo will preview or print the StringGrid followed by
the introduction memo. Page 2 has the order
reversed and prints the memo in narrow format to force some page breaks.
It will probably result in 4 pages for most devices.
Addendum June 10, 2007: Version 2 posted today adds the ability to automatically insert a page break when it reaches the end of a page. Fixed rows (column headers) are replicated at the top of each continues page. The fixed cells are now colored using the grid's FixedColor property.
I'll leave the download zip file link for the executable file in the links below because I put it there in 2004. But there's not much reason to download that I can think of.
Addendum January 23, 2008: A recent question from a viewer about right aligned text triggered a restudy of the Margin setting code added last year. A new button in the demo program prints a message in each corner of the page based on margin values set. New procedures SetInchMargin and SetPixelMargin make it easy to set margins for each border. Note for programmers - this change has replaced TSpinEdit controls with TDFFSpinEdit controls to achieve compatibility with Turbo Delphi Explorer, a free updated Delphi version which does not include TSpinEdit.
Click to download source code for Print Preview and the demo program.
Click to download executable demo program.