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.
1
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;
10
11 // Set up the page.
12 Range.rc.left := 0;
13 Range.rc.top := 0;
14 Range.rc.right := ABitmap.Width * 1440 div Screen.PixelsPerInch;
15 Range.rc.Bottom := ABitmap.Height * 1440 div Screen.PixelsPerInch;
16
17 // Default the range of text to print as the entire document.
18 Range.chrg.cpMax := -1;
19 Range.chrg.cpMin := 0;
20
21 // format the text
22 Result := SendMessage(ARichedit.Handle, EM_FORMATRANGE, 1, Longint(@Range));
23
24 // Free cached information
25 SendMessage(ARichEdit.handle, EM_FORMATRANGE, 0,0);
26 end;
27
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.}
30
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 Range.rc.top := 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;
48
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.
53
54 In this case we can use two different bitmaps for background and drawing the Rich
55 Text and after
56 combine them togehter.}
57
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;
|