Author: Jonas Bilinkevicius
I have a TDBRichEdit component in D4 and I would like to allow the user to print
the selected record centered on a page.
Answer:
It boils down to measuring the text height required for a given width of the
printout. This can be done using the EM_FORMATRANGE message, which can also be used
to print the formatted text. Here is an example that you can use as a starting
point. It measures the text to be able to frame it on the page, you can use the
calculated height to vertically center the text by adding to the top border.
Printing rich edit contents using EM_FORMATRANGE and EM_DISPLAYBAND:
1 procedure TForm1.Button2Click(Sender: TObject);
2 var
3 printarea: Trect;
4 x, y: Integer;
5 richedit_outputarea: TRect;
6 printresX, printresY: Integer;
7 fmtRange: TFormatRange;
8 begin
9 Printer.beginDoc;
10 try
11 with Printer.Canvas do
12 begin
13 printresX := GetDeviceCaps(handle, LOGPIXELSX);
14 printresY := GetDeviceCaps(handle, LOGPIXELSY);
15 Font.Name := 'Arial';
16 Font.Size := 14;
17 Font.Style := [fsBold];
18 {1 inch left margin / 1.5 inch top
19 margin / 1 inch right margin / 1.5 inch bottom margin}
20 printarea := Rect(printresX, printresY * 3 div 2, Printer.PageWidth -
21 printresX,
22 Printer.PageHeight - printresY * 3 div 2);
23 x := printarea.left;
24 y := printarea.top;
25 TextOut(x, y, 'A TRichEdit print example');
26 y := y + TextHeight('Ag');
27 Moveto(x, y);
28 Pen.Width := printresY div 72; {1 point}
29 Pen.Style := psSolid;
30 Pen.Color := clBlack;
31 LineTo(printarea.Right, y);
32 Inc(y, printresY * 5 div 72);
33 {Define a rectangle for the rich edit text.
34 The height is set to the maximum.
35 But we need to convert from device units to twips,
36 1 twip = 1/1440 inch or 1/20 point.}
37 richedit_outputarea := Rect((printarea.left + 2) * 1440 div printresX, y *
38 1440
39 div printresY,
40 (printarea.right - 4) * 1440 div printresX, (printarea.bottom) * 1440 div
41 printresY);
42 {Tell rich edit to format its text to the printer.
43 First set up data record for message:}
44 fmtRange.hDC := Handle; {printer handle}
45 fmtRange.hdcTarget := Handle; {printer handle}
46 fmtRange.rc := richedit_outputarea;
47 fmtRange.rcPage := Rect(0, 0, Printer.PageWidth * 1440 div printresX,
48 Printer.PageHeight * 1440 div printresY);
49 fmtRange.chrg.cpMin := 0;
50 fmtRange.chrg.cpMax := richedit1.GetTextLen - 1;
51 {First measure the text, to find out how high the format rectangle will be.
52 The call sets fmtrange.rc.bottom to the actual height required,
53 if all characters in the selected
54 range will fit into a smaller rectangle,}
55 richedit1.Perform(EM_FORMATRANGE, 0, Longint(@fmtRange));
56 {Draw a rectangle around the format rectangle}
57 Pen.Width := printresY div 144; {0.5 points}
58 Brush.Style := bsClear;
59 Rectangle(printarea.Left, y - 2, printarea.right, fmtrange.rc.bottom *
60 printresY
61 div 1440 + 2);
62 {Now render the text}
63 richedit1.Perform(EM_FORMATRANGE, 1, Longint(@fmtRange));
64 {and print it}
65 richedit1.Perform(EM_DISPLAYBAND, 0, Longint(@fmtRange.rc));
66 y := fmtrange.rc.bottom * printresY div 1440 + printresY * 5 div 72;
67 {Free cached information}
68 richedit1.Perform(EM_FORMATRANGE, 0, 0);
69 TextOut(x, y, 'End of example.');
70 end;
71 finally
72 Printer.EndDoc;
73 end;
74 end;
|