DELPHI SOURCE CODE
delphi source code sample collection
|
Wednesday, November 16, 2011
How to show printing a Rich Edit control onto a bitmap for preview
How to show printing 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 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;
This tutorial was kindly provided by serge perevoznyk, thank you Serge
Mengubah Background Toolbar Windows Explorer dgn Gambar Bitmap
Mengubah Background Toolbar Windows Explorer dgn Gambar Bitmap
Pada Fom rancang 2 Button dan poperty Dialog dengan mengambil OpenPictureDialog, dan juga EditText
Ini Listing program selengkapnya
Procedure Tform1.Button1Click(Sender:TObject);
begin
if OpenPictureDialog1.Execute then
Edit1.Text:=OpenPicutreDialog1.FileName;
end;
Procedure Tfrom1.Button2Click(Sender:TObject);
var Register:Tregister;
begin
Register:=Tregister.create;
Register.RootKey:=HKEY_CURRENT_USER;
try
try
Register.OpenKey('\Software\Microsoft\Internet Explorer\'+ 'Toolbar',True);
Register.WriteString('BackBitmapShell',Edit1.Text);
ShowMessage('Berhasil mengubah background toolbar'+#13+'Untuk melihat hasilnya,buka'+
' "Windows Explorer" baru.');
Except on ERegistryException do
ShowMessage('Gagal mengubah background toolbar explorer');
end;
Finally
Register.CloseKey;
Register.Free;
end;
Selamat mencoba>>>
Subscribe to:
Posts (Atom)