[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
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, email@example.com). 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.
Print Preview Methods
The main methods from the PrintPreview unit are:
The Demo Program
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.)
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 in the demo program in order to achieve compatibility with Turbo Delphi Explorer, a free updated Delphi version which does not include TSpinEdit.
Addendum June 12, 2011: Another visit to the issue of setting margins. Version 2.3 changes the margin spin edit display text color to red if the margins extend into the non-printable area of the currently selected printer. It probably took longer than it should have to realize that there are some inconsistencies in how printer page size characteristics are reported. There are two ways to determine device characteristics for a printer:
It seems that modern printer drivers adjust for the non-printable area and reference their drawing from the physical offset coordinates rather than the top left corner of the paper. Screen drawing does not have this constraint, and it took me several days to realize that. The implication is that drawing from the MetaCanvasses, which should be device independent, are not quite so. I finally conceded that fact and adjust margins on the canvas to allow for non-printable offsets. Drawing the preview image on the screen subtracts the offsets back out for the on-screen displays. That appears to be working so I decided to quit while I'm ahead (for now).
Download and Explore Programs
Click to download source code for Print Preview and the demo program.
Click to download executable demo program.
[Feedback] [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby All rights reserved.