Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to print the content of a TRichEdit centered on a page Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
07-Oct-02
Category
Reporting /Printing
Language
Delphi 4.x
Views
107
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			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   
2   procedure TForm1.Button2Click(Sender: TObject);
3   var
4     printarea: Trect;
5     x, y: Integer;
6     richedit_outputarea: TRect;
7     printresX, printresY: Integer;
8     fmtRange: TFormatRange;
9   begin
10    Printer.beginDoc;
11    try
12      with Printer.Canvas do
13      begin
14        printresX := GetDeviceCaps(handle, LOGPIXELSX);
15        printresY := GetDeviceCaps(handle, LOGPIXELSY);
16        Font.Name := 'Arial';
17        Font.Size := 14;
18        Font.Style := [fsBold];
19        {1 inch left margin / 1.5 inch top 
20  			margin / 1 inch right margin / 1.5 inch bottom margin}
21        printarea := Rect(printresX, printresY * 3 div 2, Printer.PageWidth - 
22  printresX,
23          Printer.PageHeight - printresY * 3 div 2);
24        x := printarea.left;
25        y := printarea.top;
26        TextOut(x, y, 'A TRichEdit print example');
27        y := y + TextHeight('Ag');
28        Moveto(x, y);
29        Pen.Width := printresY div 72; {1 point}
30        Pen.Style := psSolid;
31        Pen.Color := clBlack;
32        LineTo(printarea.Right, y);
33        Inc(y, printresY * 5 div 72);
34        {Define a rectangle for the rich edit text. 
35  			The height is set to the maximum. 
36  			But we need to convert from device units to twips,
37  		  1 twip = 1/1440 inch or 1/20 point.}
38        richedit_outputarea := Rect((printarea.left + 2) * 1440 div printresX, y * 
39  1440
40          div printresY,
41          (printarea.right - 4) * 1440 div printresX, (printarea.bottom) * 1440 div
42            printresY);
43        {Tell rich edit to format its text to the printer. 
44  				First set up data record for message:}
45        fmtRange.hDC := Handle; {printer handle}
46        fmtRange.hdcTarget := Handle; {printer handle}
47        fmtRange.rc := richedit_outputarea;
48        fmtRange.rcPage := Rect(0, 0, Printer.PageWidth * 1440 div printresX,
49          Printer.PageHeight * 1440 div printresY);
50        fmtRange.chrg.cpMin := 0;
51        fmtRange.chrg.cpMax := richedit1.GetTextLen - 1;
52        {First measure the text, to find out how high the format rectangle will be. 
53  			The call sets fmtrange.rc.bottom to the actual height required, 
54  			if all characters in the selected
55        range will fit into a smaller rectangle,}
56        richedit1.Perform(EM_FORMATRANGE, 0, Longint(@fmtRange));
57        {Draw a rectangle around the format rectangle}
58        Pen.Width := printresY div 144; {0.5 points}
59        Brush.Style := bsClear;
60        Rectangle(printarea.Left, y - 2, printarea.right, fmtrange.rc.bottom * 
61  printresY
62          div 1440 + 2);
63        {Now render the text}
64        richedit1.Perform(EM_FORMATRANGE, 1, Longint(@fmtRange));
65        {and print it}
66        richedit1.Perform(EM_DISPLAYBAND, 0, Longint(@fmtRange.rc));
67        y := fmtrange.rc.bottom * printresY div 1440 + printresY * 5 div 72;
68        {Free cached information}
69        richedit1.Perform(EM_FORMATRANGE, 0, 0);
70        TextOut(x, y, 'End of example.');
71      end;
72    finally
73      Printer.EndDoc;
74    end;
75  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC