我想把RichEdit的内容打印到BMP上?(50分)

  • 主题发起人 主题发起人 czx_china
  • 开始时间 开始时间
C

czx_china

Unregistered / Unconfirmed
GUEST, unregistred user!
我想把RichEdit的内容利用下面提供的程序打印到BMP上,但我怎么知道打印的内容是多宽多高?或者我设定BMP大小过,能知道已经打满,要求换页也好?
procedure TForm1.Button1Click(Sender: TObject);
var
Range : TFormatRange;
LastChar, MaxLen, LogX, LogY: Integer;
BMP:TBitmap;
begin
FillChar(Range, SizeOf(TFormatRange), 0);
BMP:=TBitmap.Create;
BMP.PixelFormat:=pf24bit;
BMP.Width:=200;
BMP.Height:=200;
with BMP.Canvas do
begin
Brush.Style:=bsSolid;
Brush.Color:=clBlack;
FillRect(ClipRect);
end;
with Range do
begin
LogX := GetDeviceCaps(BMP.Canvas.Handle, LOGPIXELSX);
LogY := GetDeviceCaps(BMP.Canvas.Handle, LOGPIXELSY);
hdc := BMP.Canvas.Handle;
hdcTarget := hdc;
rc.right := BMP.Width * 1440 div LogX;//Image1.ClientWidth * 1440 div LogX;
rc.bottom := BMP.Height * 1440 div LogY;//Image1.ClientHeight * 1440 div LogY;
rcPage := rc;
LastChar := 0;
MaxLen := Editor.GetTextLen;
chrg.cpMax := -1;
repeat
chrg.cpMin := LastChar;
LastChar := SendMessage(Editor.Handle, EM_FORMATRANGE, 1, Longint(@Range));
until (LastChar >= MaxLen) or (LastChar = -1);

end;
Image1.Picture.Assign(BMP);
BMP.free;
 
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.

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;
 
后退
顶部