Printing in Delphi text and graphics For each Windows application developer, the implementation of printing features is extremely tricky, because it is often quite complicated, but the text is implemented in Delphi. The image print function is very easy. 1. Text printing function. The print function of Delphi is like the print function of the present textbook in DOS, and one of the needs to be done is only to obtain the print parameters, open the printer, and then send each line of the text. A Printers program unit is provided in Delphi, which illustrates a TPrinter object, encapsulates the interface between Windows printing work and output printers, and provides common properties and methods, where canvas canvas is a very useful property, it represents The surface of the current print file is working in a graphical way. The entire printout work is just outputting the content you print to the property of TPrinter. When all of the output works, print objects (tprinter) to put Canvas The attribute value is sent to the printer.
In the case of the case, if you do this, you will print the printing of the Delphi. Provides PrintDialog in Delphi, printersetupDialog two controls allow us to make printers and other options that affect printouts, and the most important point is to implement printing printed must add Printers to Interface or Implementation's UESE statement prior Because the Printer unit includes an assignprn process of controlling printers.
First add MEMO, PrintDialog, PrinterSetupDialog and two Button controls in Form, and two Button's CAPTION is "Print Settings" and "Print". Then, then write the Button's event driver (the code is below), this simple example can only click the "Print" button to output the document 0s2.txt on the printer, the printing event is as follows:
IMPLEMentation
Uses printers;
{$ R * .dfm}
Procedure TFORM1.BITBTN1CLICK (Sender: TOBJECT);
Begin
PrinterSetupDialog1.execute; file: // Select the output printer and other print control options
END;
Procedure TFORM1.BITBTN2CLICK (Sender: TOBJECT);
VAR
LINES: Integer;
PRNTEXT: system.text;
File: // Repeated the prnText as a text file defined in the System program unit
Begin
If printdialog1.execute1nessIGN (prNText); file: // Assign PrNTest to the printer
ReWrite (prnText); file: // calls the REWRITE function, open the assigned file for the output
Printer.canvas.font: = MEMO1.FONT;
File: // Specify the font of the current MEMO1 to the font attribute of the Canvas of the print object
For lines: = 0 to memo1.lines.count-1 do
Writeln (prNText, Memo1.Lines [lines]);
File: // Write MEMO's content to the printer object
System.close (prNText); File: // Close the print file
END;
Procedure TFORM1.FormCreate (Sender: TOBJECT);
Begin
Memo1.Lines.LoadFromfile ('c: /dos/os2.txt');
File: // Read when Form is established, C: /DOS/OS2.txt file
END;
End. 2, the printing function of the graphics, simple graphic printing function can also be printed by printing text, just tell the printer's object to start printing, simply researchers into the printer, and finally tell the printer's end bundle printing work. For example, the MEMO control of the above-faced example is changed into an Image control, and then a simple modification is passed, the code printed by the figure is as follows:
Procedure TFORM1.BITBTN1CLICK (Sender: TOBJECT);
Begin
IF printDialog1.execute the begin
Printer.begindoc;
Printer.canvas.draw (0, 0, Image1.Picture.graphic);
Printer.Enddoc;
END;
End; In this case, the resolution of the printer is used. The graphic starts print output in the upper left corner of the page, and the graphics are small. In many cases, it cannot meet the requirements, but the printer draws Canvas's StretHDRAW method, We can make our flexible treatment, canvas's StretchDraw method claims: Procedure StretchDraw (Const Rect: TRECT; GRAPHIC: TGRAPHIC);
The RECT parameter represents the size of the graphic output area, the TRECT type is named:
TRECT = Record
Case Integer of
0: (Left, Top, Right, Bottom: Integer);
1: (Topleft, Bottomright: TPOIN);
END; so we only need to adjust the size of the RECT and its position on the print page, which further achieve its satisfaction, the following code is constantly enlarged graphics, full of the rectangular area we define, and position it in the printer drawing (Canvas The center is output. The code is as follows: Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT); VAR
STRECT: TRECT; File: / / Define the size of the printout rectangle box
Temhi, Temwd: Integer;
Begin
IF printDialog1.execute the
Begin
Temhi: = image1.picture.height;
Temwd: = image1.picture.width;
While (Temhi Printer.pageHeight Div 2) and
FILE: // Enlarge the graph to 1/2 size of the print page
(Temwd Printer.Pagewidth Div 2) DO
Begin
Temhi: = Temhi Temhi;
Temwd: = temwd temwd;
END;
WITH STRECT Do File: / / Define the center position output of the graph on the page
Begin
Left: = (printer.pagewidth -temwd) DIV 2;
Top: = (printer.pageheight-temhi) DIV 2;
Right: = Left Temwd;
Bottom: = TOP TEMHI;
END;
With printer do
Begin
Begindoc;
Canvas.stretchDraw (Strect, Image1.Picture.graphic);
File: // Put the zoomed graphic to the printer
Enddoc;
END;
END;
END; The above code is compiled by Delphi 2.0. Through these small examples, I believe that everyone has a certain understanding of printing graphics and text in Delphi, and can also achieve the printing of database forms through the printer's canvas attribute. It will not be described here, if you are interested, you may wish