Hi Nigel,
Trying to paint a RichText Layer onto a Background TBitmap with transparency. Below are two procedures that implement this with Delphi's TRichEdit control and a bitmap. Don't want to use layers because this procedure is called many times per second.
I have tried to assign a RichText Layer to Delphi's TRichEdit control but I get text without formatting. The text looks like a Memo control with it's one font. Control characters are revealed
(e.g. {\rtf1\ansi\deff0\deflang1033{\fonttbl{\f0\fswiss Arial;}...).
How to paint RichText Layer onto a bitmap with formatting preserved ??
///////////////////////////////////////////
procedure DrawRTF(DestDCHandle: HDC; const R: TRect;
RichEdit: TRichEdit; PixelsPerInch: Integer);
var
TwipsPerPixel: Integer;
Range: TFormatRange;
begin
TwipsPerPixel := 1440 div PixelsPerInch;
with Range do
begin
hDC := DestDCHandle; {DC handle}
hdcTarget := DestDCHandle; {ditto}
{Convert the coordinates to twips (1/1440")}
rc.Left := R.Left * TwipsPerPixel;
rc.Top := R.Top * TwipsPerPixel;
rc.Right := R.Right * TwipsPerPixel;
rc.Bottom := R.Bottom * TwipsPerPixel;
rcPage := rc;
chrg.cpMin := 0;
chrg.cpMax := -1; {RichEdit.GetTextLen;}
{Free cached information}
RichEdit.Perform(EM_FORMATRANGE, 0, 0);
{First measure the text, to find out how high the format rectangle will be. The call sets fmtrange.rc.bottom to the actual height required, if all characters in the selected range will fit into a smaller rectangle}
RichEdit.Perform(EM_FORMATRANGE, 0, DWord(@Range));
{Now render the text}
RichEdit.Perform(EM_FORMATRANGE, 1, DWord(@Range));
{Free cached information}
RichEdit.Perform(EM_FORMATRANGE, 0, 0);
end;
end;
procedure Tfmain.Button1Click(Sender: TObject);
var
RichEdit: TRichEdit;
ExStyle: DWord;
bmp: TBitmap;
DestDCHandle: HDC;
begin
RichEdit := TRichEdit.Create(Self);
try
RichEdit.Visible := False;
RichEdit.Parent := Self;
{Win2k, WinXP}
ExStyle := GetWindowLong(RichEdit.Handle, GWL_EXSTYLE);
ExStyle := ExStyle or WS_EX_TRANSPARENT;
SetWindowLong(RichEdit.Handle, GWL_EXSTYLE, ExStyle);
RichEdit.Lines.LoadFromFile('c:\JDS\Readme.rtf');
bmp := TBitmap.Create;
try
bmp.LoadFromFile('c:\jds\TheBackGroundBmp.bmp');
DestDCHandle := bmp.Canvas.Handle;
{Win9x}
SetBkMode(DestDCHandle, TRANSPARENT);
DrawRTF(DestDCHandle, Rect(0, 0, bmp.Width, bmp.Height), RichEdit, 96);
Image1.Picture.Assign(bmp);
Image1.Refresh;
finally
bmp.Free;
end;
finally
RichEdit.Free;
end;
end;
//////////////////////////////////////