Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
Print preview of TRichEdit 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
Painting a Rich Edit control onto a bitmap for preview 25-Oct-04
Reporting /Printing
Delphi All Versions
User Rating
# Votes
Perevoznyk, Serhiy
Reference URL:
			The Rich Edit control (we a talking about standard Windows control, not a Delphi 
component) contains built-in printing features that can be used to send formatted 
text to the printer or to paint it's content onto any canvas with minimal effort 
from the programmer. 
Of course, the standard Delphi TRichEdit component incapsulates this feature. 
We can use this posibility to make a fast print preview with a scaling or drawing 
Rich Text on any Delphi control.

Drawing from a Rich Edit control to any canvas involves the use of the standard 
Rich Edit control message EM_FORMATRANGE.

The lParam parameter for this message is a pointer to the TFormatRange record. 
This record have to be filled before sending the message to the RichEdit. 

The TFORMATRANGE record contains information that a rich edit control uses to 
format its output for a particular device, where

hdc Device to render to. 
hdcTarget Target device to format for. 
rc Area to render to. Units are measured in twips. Twips are screen-independent 
units to ensure that the proportion of screen elements are the same on all display 
systems. A twip is defined as being 1/1440 of an inch.
rcPage Entire area of rendering device. Units are measured in twips. 
chrg TCHARRANGE record that specifies the range of text to format.

This record usually is used with the EM_EXGETSEL and EM_EXSETSEL messages and 
includes two fields: cpMin and cpMax.

cpMin is a character position index immediately preceding the first character in 
the range. 
cpMax is a character position immediately following the last character in the range.
2   function  PrintRTFToBitmap(ARichEdit : TRichEdit; ABitmap : TBitmap) : Longint;
3   var
4    range    : TFormatRange;
5   begin
6      FillChar(Range, SizeOf(TFormatRange), 0);
7      // Rendering to the same DC we are measuring.
8      Range.hdc        := ABitmap.Canvas.handle;
9      Range.hdcTarget  := ABitmap.Canvas.Handle;
11     // Set up the page.
12     Range.rc.left    := 0;
13     := 0;
14     Range.rc.right   := ABitmap.Width * 1440 div Screen.PixelsPerInch;
15     Range.rc.Bottom  := ABitmap.Height * 1440 div Screen.PixelsPerInch;
17     // Default the range of text to print as the entire document.
18     Range.chrg.cpMax := -1;
19     Range.chrg.cpMin := 0;
21     // format the text
22     Result := SendMessage(ARichedit.Handle, EM_FORMATRANGE, 1, Longint(@Range));
24     // Free cached information
25     SendMessage(ARichEdit.handle, EM_FORMATRANGE, 0,0);
26  end;
28  {The next example shows how to draw the Rich Edit not only to any canvas, but also 
29  how to draw only selected text range.}
31  function PrintToCanvas(ACanvas : TCanvas; FromChar, ToChar : integer;
32                        ARichEdit : TRichEdit; AWidth, AHeight : integer) : Longint;
33  var
34    Range    : TFormatRange;
35  begin
36      FillChar(Range, SizeOf(TFormatRange), 0);
37      Range.hdc        := ACanvas.handle;
38      Range.hdcTarget  := ACanvas.Handle;
39      Range.rc.left    := 0;
40     := 0;
41      Range.rc.right   := AWidth * 1440 div Screen.PixelsPerInch;
42      Range.rc.Bottom  := AHeight * 1440 div Screen.PixelsPerInch;
43      Range.chrg.cpMax := ToChar;
44      Range.chrg.cpMin := FromChar;
45      Result := SendMessage(ARichedit.Handle, EM_FORMATRANGE, 1, Longint(@Range));
46     SendMessage(ARichEdit.handle, EM_FORMATRANGE, 0,0);
47  end;
49  {But how to draw a Rich Text with the background image?  
50  That is hopeless with the standard TRichedit control, because it based on the 
51  Windows
52  control and have no provision to handle background bitmaps or transparency.
54  In this case we can use two different bitmaps for background and drawing the Rich 
55  Text and after 
56  combine them togehter.}
58  procedure TForm1.Button2Click(Sender: TObject);
59  var Bmp : TBitmap;
60  begin
61      Bmp := TBitmap.Create;
62      bmp.Width := 300;
63      bmp.Height := 300;
64      PrintToCanvas(bmp.Canvas,2,5,RichEdit1,300,300);
65      BitBlt(Image1.Picture.Bitmap.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
66          bmp.Canvas.Handle, 0, 0, srcAND);
67     Image1.Repaint;
68     bmp.Free;
69  end;

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


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC