|
Technical answers from the trenches |
|
QuickReports: Printing to the Selected Printer
| ||||
Posted: 2 January 2002 |
||||
  |
Applies to: Delphi 5 (and others) |
|||
  |
Audience: Intermediate |
|||
Question: How do I get QuickReports to print to a printer selected by the user?Answer: Combine Delphi's TPrinterSetupDialog component with the PRINTERS unit provided by the VCL and then add the following code to the BeforePrint event of your QuickRep object: procedure TrptTextFile.QuickRep1BeforePrint(Sender: TCustomQuickRep;
var PrintReport: Boolean);
begin
with QuickRep1.PrinterSettings do
if PrinterIndex <> printer.printerIndex then
PrinterIndex := printer.printerIndex;
end;Warning: You can only do this while printing a report. If you try to do it during a preview event, you'll run into runtime exceptions because QuickReport's Printer.PrinterSettings property is not initialized until you actually print the report. How it works:Delphi's TPrinterSetupDialog provides a convenient interface for selecting a printer. The Printers unit lets you determine which printer the user selected with the dialog and provides a Printer object that encapsulates the currently installed printer drivers. Like many other list objects, the Printer object provides a PrinterIndex property that indicates the selected printer. Unfortunately, QuickReports does not use this information to initialize itself. You need to do that yourself. The above code shows one way to do that. If you have several reports in your application, it may be preferable to create a separate function that accepts the selected report as a var parameter, typecasts it as a TCustomQuickRep object, and then sets the selected printer. This is left as an exercise for the reader. An Alternate ApproachThe FAQ on QuSoft's web site suggests a slightly different approach:
While this works, it seems more work than necessary. In addition, it runs the risk of later problems should something happen to the user's PC while your report is printing. If Step 4 does not properly occur, then you've changed the user's configuration--generally without their knowledge. This could lead to confusion as your user tries to figure out why their default printer keeps changing. Similar frustrations may occur if your user is printing from multiple applications at the same time. Because of these risks, we prefer our approach, which lets the user control the action and limits its effects to the print jobs generated by your application. ExampleTo illustrate this code, we've provided a sample application in our Download area. The ZIP file contains a sample text file printer. You can either load a small text file or type a few lines and then choose a different printer. When you choose File | Print, your text should go to the selected printer. You can also choose File | View Printers to verify that Delphi's Printers unit tracks the selected printer. |
|||
|
||||||||
|
Copyright © 2000-2004, techtricks.com; All Rights Reserved. Acknowledgements, Disclaimers, Terms and Conditions. |
||||||||
|
Article last updated on 01 June 2003
|
||
|
|
||
|
[- End -]
|