Mega Code Archive

 
Categories / Delphi / Examples
 

Print preview of trichedit

Painting a rich edit control onto a bitmap for preview 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 its contents 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. function PrintRTFToBitmap(ARichEdit : TRichEdit; ABitmap : TBitmap) : Longint; var range : TFormatRange; begin FillChar(Range, SizeOf(TFormatRange), 0); // Rendering to the same DC we are measuring. Range.hdc := ABitmap.Canvas.handle; Range.hdcTarget := ABitmap.Canvas.Handle; // Set up the page. Range.rc.left := 0; Range.rc.top := 0; Range.rc.right := ABitmap.Width * 1440 div Screen.PixelsPerInch; Range.rc.Bottom := ABitmap.Height * 1440 div Screen.PixelsPerInch; // Default the range of text to print as the entire document. Range.chrg.cpMax := -1; Range.chrg.cpMin := 0; // format the text Result := SendMessage(ARichedit.Handle, EM_FORMATRANGE, 1, Longint(@Range)); // Free cached information SendMessage(ARichEdit.handle, EM_FORMATRANGE, 0,0); end; The next example shows how to draw the Rich Edit not only to any canvas, but also how to draw only selected text range. function PrintToCanvas(ACanvas : TCanvas; FromChar, ToChar : integer; ARichEdit : TRichEdit; AWidth, AHeight : integer) : Longint; var Range : TFormatRange; begin FillChar(Range, SizeOf(TFormatRange), 0); Range.hdc := ACanvas.handle; Range.hdcTarget := ACanvas.Handle; Range.rc.left := 0; Range.rc.top := 0; Range.rc.right := AWidth * 1440 div Screen.PixelsPerInch; Range.rc.Bottom := AHeight * 1440 div Screen.PixelsPerInch; Range.chrg.cpMax := ToChar; Range.chrg.cpMin := FromChar; Result := SendMessage(ARichedit.Handle, EM_FORMATRANGE, 1, Longint(@Range)); SendMessage(ARichEdit.handle, EM_FORMATRANGE, 0,0); end; But how to draw a Rich Text with the background image? That is hopeless with the standard TRichedit control, because it based on the Windows control and have no provision to handle background bitmaps or transparency. In this case we can use two different bitmaps for background and drawing the Rich Text and after combine them togehter. procedure TForm1.Button2Click(Sender: TObject); var Bmp : TBitmap; begin Bmp := TBitmap.Create; bmp.Width := 300; bmp.Height := 300; PrintToCanvas(bmp.Canvas,2,5,RichEdit1,300,300); BitBlt(Image1.Picture.Bitmap.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, bmp.Canvas.Handle, 0, 0, srcAND); Image1.Repaint; bmp.Free; end; You can download sample source code from http://www.delphipages.com/edit/count.cfm?ID=2667